using LoggingDemo; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using NLog.Fluent; //DI ServiceCollection services = new ServiceCollection(); services.AddLogging(logBuilder => { logBuilder.AddConsole(); //最低输出日志级别 logBuilder.SetMinimumLevel(LogLevel.Trace); });
services.AddScoped<Recording>();
using (var sp=services.BuildServiceProvider()) { var recording = sp.GetRequiredService<Recording>(); recording.Record(); }
using LoggingDemo; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using NLog.Extensions.Logging; using NLog.Fluent; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; using NLog.Web; //DI ServiceCollection services = new ServiceCollection(); services.AddLogging(logBuilder => { logBuilder.AddConsole(); logBuilder.AddEventLog(); logBuilder.AddNLog(); //最低输出日志级别 logBuilder.SetMinimumLevel(LogLevel.Trace); });
services.AddScoped<Recording>();
using (var sp=services.BuildServiceProvider()) { var recording = sp.GetRequiredService<Recording>(); recording.Record(); }
<!--Skip Microsoft logs and so log only own logs--> <!--以Microsoft打头的日志将进入此路由,由于此路由没有writeTo属性,所有会被忽略--> <!--且此路由设置了final,所以当此路由被匹配到时。不会再匹配此路由下面的路由。未匹配到此路由时才会继续匹配下一个路由--> <logger name="Microsoft.*" minlevel="Trace" final="true" /> <!--上方已经过滤了所有Microsoft.*的日志,所以此处的日志只会打印除Microsoft.*外的日志--> <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> </rules> </nlog>
<!-- the targets to write to --> <targets> <!-- File Target for all log messages with basic details --> <target xsi:type="File" name="LoggingDemo" fileName="logs/log-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}" />
<!-- File Target for own log messages with extra web details using some ASP.NET core renderers --> <target xsi:type="File" name="SytemServices" fileName="logs/sysFile-${shortdate}.log" archiveAboveSize="1000" maxArchiveFiles="3" layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection --> <target xsi:type="Console" name="logconsole" layout="${MicrosoftConsoleLayout}" /> </targets>
<!-- rules to map from logger name to target --> <rules> <!-- All logs, including from Microsoft --> <logger name="*" minlevel="Warn" maxlevel="Fatal" writeTo="logconsole" />
<!-- Suppress output from Microsoft framework when non-critical --> <logger name="SytemServices.*" minlevel="Trace" writeTo="SytemServices" final="true" /> <!-- Keep output from Microsoft.Hosting.Lifetime to console for fast startup detection --> <logger name="*" minLevel="Trace" writeTo="LoggingDemo" /> </rules> </nlog>
using (var sp=services.BuildServiceProvider()) { var recording = sp.GetRequiredService<Recording>(); var test1 = sp.GetRequiredService<Test1>(); for(int i = 0; i < 10; i++) { recording.Record(); test1.Record(); }
}
集中化日志
NLog也可以配置结构化日志,不过配置麻烦,推荐用Serilog。
NuGet安装:
1 2 3
using Serilog.Formatting.Json; using Serilog; Serilog.AspNetCore
配置
1 2 3 4 5 6
Serilog.Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .Enrich.FromLogContext() .WriteTo.Console(new JsonFormatter()) .CreateLogger(); logBuilder.AddSerilog();