本文主要内容为.NET Core的日志记录程序和常使用的日志记录框架的简单使用
首先,打开VS2019新建一个ASP.NET Core Web Api项目,项目创建好后会有一个集成好的天气预报的类和控制器,接下来,我们的方法就在天气控制器里完成。
1、配置提供程序替换默认提供程序 在启动项中添加Log配置替换默认的日志提供程序,在控制器中编写具体输出内容
1)Program.cs 1 2 3 4 5 6 7 8 9 10 11 12 public static IHostBuilder CreateHostBuilder (string [] args ) => Host.CreateDefaultBuilder(args).ConfigureLogging(logging => { logging.ClearProviders(); logging.AddConsole(); logging.AddDebug(); logging.AddEventSourceLogger(); logging.AddEventLog(); }).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup < Startup > (); });
2)控制器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [ApiController ] [Route("[controller]/[action]" ) ] public class WeatherForecastController : ControllerBase { private readonly ILogger <WeatherForecastController> _logger; public WeatherForecastController (ILogger <WeatherForecastController> logger ) { _logger = logger; } [HttpGet ] public void GetLogOutUniversalHost () { _logger.LogTrace(1000 , "log Trace msg" ); _logger.LogDebug(1001 , "log Debug msg" ); _logger.LogInformation(1002 , "log Information msg" ); _logger.LogWarning(1003 , "log Warning msg" ); _logger.LogError(1004 , "log Error msg" ); _logger.LogCritical(1005 , "log Critical msg" ); } }
3)运行结果
2、创建LoggerFactory时调用提供程序的扩展方法 直接在控制器中创建LoggerFactory时调用提供程序的Add方法,使用LoggerFactory来创建 ILogger
1)控制器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [ApiController ] [Route("[controller]/[action]" ) ] public class WeatherForecastController : ControllerBase { [HttpGet ] public void GetLogOutNonHost () { var loggerFactory = LoggerFactory.Create(builder => { builder.AddFilter("Microsoft" , LogLevel.Warning).AddFilter("System" , LogLevel.Warning).AddFilter("LoggingConsoleApp.Program" , LogLevel.Debug).AddConsole().AddEventLog(); }); ILogger logger = loggerFactory.CreateLogger<Program>(); logger.LogInformation("非主机模式输出log msg" ); } }
2)运行结果
3、Log4Net 1)使用NuGet安装log4net包
2)新建log4net.config配置文件,配置日志输出格式
3)右键新添加的log4net.config配置文件,更改文件属性->复制到输出目录选项:始终复制
或者在项目的.csproj文件添加如下代码:
1 2 3 4 5 <ItemGroup > <Content Update ="log4net.config" > <CopyToOutputDirectory > Always</CopyToOutputDirectory > </Content > </ItemGroup >
4)log4net.config配置文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 <?xml version="1.0" ?> <configuration > <configSections > <section name ="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections > <appSettings > <add key ="WebSocketPort" value ="45154" /> </appSettings > <log4net > <appender name ="logInfoToFile" type ="log4net.Appender.RollingFileAppender,log4net" > <file value ="logs/log4net/info" /> <AppendToFile value ="true" /> <RollingStyle value ="Date" /> <DatePattern value ="'.'yyyy-MM-dd'.txt'" /> <StaticLogFileName value ="false" /> <layout type ="log4net.Layout.PatternLayout,log4net" > <ConversionPattern value ="%d %-5p [%c] %m%n" /> </layout > </appender > <appender name ="logErrorToFile" type ="log4net.Appender.RollingFileAppender,log4net" > <file value ="logs/log4net/error" /> <AppendToFile value ="true" /> <RollingStyle value ="Date" /> <DatePattern value ="'.'yyyy-MM-dd'.txt'" /> <StaticLogFileName value ="false" /> <layout type ="log4net.Layout.PatternLayout,log4net" > <ConversionPattern value ="%d %-5p [%c] %m%n" /> </layout > </appender > <root > <level value ="ALL" additivity ="false" /> <appender-ref ref ="logInfoToFile" /> </root > <logger name ="ApplicationInfoLog" additivity ="false" > <level value ="ALL" /> <appender-ref ref ="logInfoToFile" /> </logger > <logger name ="ApplicationErrorLog" additivity ="false" > <level value ="ALL" /> <appender-ref ref ="logErrorToFile" /> </logger > </log4net > </configuration >
关于log4net.config配置的详细说明,可参考log4net配置文件详解
5)控制器
5.1 Log4Net 简单配置,输出至控制台
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [HttpGet ] public void GetLogOutWithLog4NetSimpleConfiguration (){ ILoggerRepository repository = LogManager.CreateRepository(".NET Core RepositoryRepository" ); BasicConfigurator.Configure(repository); ILog log = LogManager.GetLogger(repository.Name, ".NET Core Log4net" ); log.Info(".NET Core Log4net log" ); log.Info("test log" ); log.Error("error" ); log.Info("linezero" ); }
5.1 运行结果
5.2 Log4Net 增加配置,输出至文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [HttpGet ] public void GetLogOutWithLog4NetComplexConfigurations (){ ILoggerRepository repository = LogManager.CreateRepository(".NET Core RepositoryRepository" ); XmlConfigurator.Configure(repository, new FileInfo("log4net.config" )); ILog log = LogManager.GetLogger(repository.Name, ".NET Core Log4net" ); log.Info(".NET Core Log4net log" ); log.Info("test log" ); log.Error("error" ); log.Info("linezero" ); }
5.2 运行结果
4、NLog 1)使用NuGet安装NLog和NLog.Web.AspNetCore包
2)新建nlog.config配置文件,配置日志输出格式 3)右键新添加的log4net.config配置文件,更改文件属性->复制到输出目录选项:始终复制
或者在项目的.csproj文件添加如下代码:
1 2 3 4 5 <ItemGroup > <Content Update ="nlog.config" > <CopyToOutputDirectory > Always</CopyToOutputDirectory > </Content > </ItemGroup >
4)nlog.config配置文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <?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" throwConfigExceptions ="true" internalLogLevel ="info" internalLogFile ="${basedir}/logs/nlog/internal-nlog.txt" > <extensions > <add assembly ="NLog.Web.AspNetCore" /> </extensions > <targets > <target xsi:type ="File" name ="allfile" fileName ="${basedir}/logs/nlog/all/nlog-all-${shortdate}.log" layout ="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /> <target xsi:type ="File" name ="ownFile-web" fileName ="${basedir}/logs/nlog/my/nlog-own-${shortdate}.log" layout ="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|" /> <target xsi:type ="Console" name ="lifetimeConsole" layout ="${MicrosoftConsoleLayout}" /> </targets > <rules > <logger name ="*" minlevel ="Trace" writeTo ="allfile" /> <logger name ="Microsoft.Hosting.Lifetime" minlevel ="Info" writeTo ="lifetimeConsole, ownFile-web" final ="true" /> <logger name ="Microsoft.*" maxlevel ="Info" final ="true" /> <logger name ="*" minlevel ="Trace" writeTo ="ownFile-web" /> </rules > </nlog >
5)配置并启用NLog 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class Program { public static void Main (string [] args ) { var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger(); try { logger.Debug("init main" ); CreateHostBuilder(args).Build().Run(); } catch (Exception exception) { logger.Error(exception, "Stopped program because of exception" ); throw ; } finally { NLog.LogManager.Shutdown(); } } public static IHostBuilder CreateHostBuilder (string [] args ) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup < Startup > (); }).ConfigureLogging(logging => { logging.ClearProviders(); logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); }).UseNLog(); }
6)配置appsettings.json appsettings.json中指定的日志配置覆盖了对SetMinimumLevel的任何调用。因此,要么删除 “Default”:要么根据你的需要正确调整。
上述logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
所以配置文件需要更改
1 2 3 4 5 6 7 8 9 10 { "Logging" : { "LogLevel" : { "Default" : "Trace" , "Microsoft" : "Warning" , "Microsoft.Hosting.Lifetime" : "Information" } }, "AllowedHosts" : "*" }
记得也要测试环境的配置,appsettings.Development.json,以免出现异常
1 2 3 4 5 6 7 8 9 { "Logging" : { "LogLevel" : { "Default" : "Trace" , "Microsoft" : "Warning" , "Microsoft.Hosting.Lifetime" : "Information" } } }
7)控制器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [HttpGet ] public void GetLogOutWithNLog (){ _logger.LogTrace(1000 , "log Trace msg" ); _logger.LogDebug(1001 , "log Debug msg" ); _logger.LogInformation(1002 , "log Information msg" ); _logger.LogWarning(1003 , "log Warning msg" ); _logger.LogError(1004 , "log Error msg" ); _logger.LogCritical(1005 , "log Critical msg" ); }
8)运行结果
关于配置文件的说明可查看官网:NLog Configuration-file
还有一篇翻译文章方便查看:NLog类库使用探索——详解配置
5、Serilog 1)使用NuGet安装Serilog.AspNetCore包 2)配置Serilog输出格式等信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public class Program { public static void Main (string [] args ) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft" , Serilog.Events.LogEventLevel.Information) .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"logs/serilog/serilog.txt" ), rollingInterval: RollingInterval.Day) .CreateLogger(); try { Log.Information("Starting up" ); CreateHostBuilder(args).Build().Run(); } catch (Exception ex) { Log.Fatal(ex, "Application start-up failed" ); } finally { Log.CloseAndFlush(); } } public static IHostBuilder CreateHostBuilder (string [] args ) => Host.CreateDefaultBuilder(args).ConfigureLogging(logging => { logging.ClearProviders(); }).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup < Startup > (); }).UseSerilog(); }
这里为直接按照配置格式输出,当然也可以通过读取配置文件的形式读取配置的具体信息,本文不做具体阐述。
3)控制器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [HttpGet ] public void GetLogOutWithSerilog (){ _logger.LogTrace(1000 , "log Trace msg" ); _logger.LogDebug(1001 , "log Debug msg" ); _logger.LogInformation(1002 , "log Information msg" ); _logger.LogWarning(1003 , "log Warning msg" ); _logger.LogError(1004 , "log Error msg" ); _logger.LogCritical(1005 , "log Critical msg" ); }
4)运行结果
6、附录 1)Log4Net GitHub仓库地址
2)NLog GitHub仓库地址
3)Serilog GitHub仓库地址
4)NLog vs log4net vs Serilog: Compare .NET Logging Frameworks
5)LogLevel 枚举
6)日志级别
以上就是.NET Core 日志记录程序和常用日志记录框架的简单使用的介绍,做此记录,如有帮助,欢迎点赞关注收藏!