修改日期 | 修改人 | 备注 |
2020-06-19 14:08:22[当前版本] | 钱钟书 | 增加注意事项 |
2020-05-25 15:41:41 | 钱钟书 | 依赖包 |
2020-05-25 10:43:10 | 钱钟书 | 命名空间 |
2020-05-25 10:39:53 | 钱钟书 | 引用命名空间 |
通常我们会在项目中使用nlog、log4net等日志组件,它们用于记录日志的功能非常强大和完整,常见情况会将日志写到txt或数据库中, 但通过记事本和sql查看日志并不简单方便. LogDashboard提供了一个可以简单快速查看日志的面板。
NLog可以轻松地为您的应用程序生成和管理高品质的日志,而不管其大小或复杂性。日志类型可分类信息日志,调试日志,错误日志,异常日志,警告日志。
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <variable name="myvar" value="myvalue"/> <targets> <target xsi:type="file" name="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate}||${level}||${logger}||${message}||${exception:format=ToString:innerFormat=ToString:maxInnerExceptionLevel=10:separator=\r\n}||end" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="file" /> </rules> </nlog>
需注意:NLog.config的属性配置为始终复制
public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseUrls("http://*:5000;http://*:5001") .UseNLog() .Build(); host.Run(); }
1. 打开appsettings.json添加连接字符串配置
"ConnectionStrings": { "DefaultConnection": "Server=.;Database=log;User Id=sa;Password=ok;Trusted_Connection=False;" }
2. NLog.config的配置更改,先创建数据库和表,以及 SQL的数据插入
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true"> <targets> <target name="database" xsi:type="Database"> <connectionString>${var:ConnectionString}</connectionString> <commandText> insert into dbo.Log ( MachineName, LongDate, Level, Message, Logger, Callsite, Exception, TraceIdentifier ) values ( @MachineName, @LongDate, @Level, @Message, @Logger, @Callsite, @Exception, @TraceIdentifier ); </commandText> <parameter name="@MachineName" layout="${machinename}" /> <parameter name="@LongDate" layout="${longDate}" /> <parameter name="@Level" layout="${level}" /> <parameter name="@Message" layout="${message}" /> <parameter name="@Logger" layout="${logger}" /> <parameter name="@Callsite" layout="${callsite}" /> <parameter name="@Exception" layout="${exception:tostring}" /> <parameter name="@TraceIdentifier" layout="${aspnet-traceidentifier}" /> </target> </targets> <rules> <logger name="*" minlevel="Info" writeTo="database" /> </rules> </nlog>
3. Program.cs代码配置
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .ConfigureLogging(logging => { logging.ClearProviders(); logging.SetMinimumLevel(LogLevel.Information); }) .UseNLog(); }
5. StartUp.cs代码如下
public IServiceProvider ConfigureServices(IServiceCollection services) { var connection = _appConfiguration.GetConnectionString("default"); NLog.LogManager.Configuration.Variables["connectionString"] = connection; services.AddLogDashboard(opt => { opt.UseDataBase(() => new SqlConnection(connection)); }); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseLogDashboard(); }
6.大功告成,这时运行项目,在浏览器中导航到 /logdashboard