.NET Core3.0 日志 logging的实现(2)

第三方日志框架

log4net,NLog和Serilog这3种日志记录框架几乎在.NET空间中占主导地位,不需要太多介绍。看看最近6周的下载排名就知道了。如图

.NET Core3.0 日志 logging的实现

log4net 在NET Core 3.0使用

.NET Core3.0 日志 logging的实现

引用Log4net到项目中,安装NuGet包

Install-Package log4net -Version 2.0.8

在项目中添加log4net.config文件,右键改文件属性-》复制到输出目录选择-》始终复制

<log4net> <appender type="log4net.Appender.RollingFileAppender"> <file value="loglog4net.log" /> <appendToFile value="true" /> <maximumFileSize value="50KB" /> <maxSizeRollBackups value="2" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level %message%newline" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="RollingFile" /> </root> </log4net>

在Startup文件中添加log4net配置的文件

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseLog4net(); /// <summary> /// 使用log4net配置 /// </summary> /// <param></param> /// <returns></returns> public static IApplicationBuilder UseLog4net(this IApplicationBuilder app) { var logRepository = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy)); log4net.Config.XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config")); return app; } 上面两步的准备完成之后,在Controllers中使用 private readonly ILog log; public WeatherForecastController(ILogger<WeatherForecastController> logger) { this.log = LogManager.GetLogger(typeof(WeatherForecastController)); } [HttpGet] public IEnumerable<WeatherForecast> Get() { //第三方日志 log.Error("log4net erro msg"); //log4net

运行查看日志文件

.NET Core3.0 日志 logging的实现

NLog 在NET Core 3.0使用

.NET Core3.0 日志 logging的实现

引用NLog到项目中,安装NuGet包

Install-Package NLog.Web.AspNetCore -Version 4.9.0 Install-Package NLog -Version 4.6.7

添加配置文件nlog.config

<?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" internalLogLevel="Info" internalLogFile="internal-nlog.txt"> <!-- enable asp.net core layout renderers --> <extensions> <add assembly="NLog.Web.AspNetCore"/> </extensions> <!-- the targets to write to --> <targets> <!-- write logs to file --> <target xsi:type="File" fileName="nlog-all-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /> <!-- another file log, only own logs. Uses some ASP.NET core renderers --> <target xsi:type="File" fileName="nlog-own-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> </targets> <!-- rules to map from logger name to target --> <rules> <!--All logs, including from Microsoft--> <logger minlevel="Trace" writeTo="allfile" /> <!--Skip non-critical Microsoft logs and so log only own logs--> <logger maxlevel="Info" final="true" /> <!-- BlackHole without writeTo --> <logger minlevel="Trace" writeTo="ownFile-web" /> </rules> </nlog>

在Program.cs文件配置 Main方法添加

NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

CreateHostBuilder方法添加

.UseNLog() //NLog: Setup NLog for Dependency injection

使用

[HttpGet] public IEnumerable<WeatherForecast> Get() { //内置日志 _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"); //

Serilog 在NET Core 3.0使用

.NET Core3.0 日志 logging的实现

引用Serilog到项目中,安装NuGet包

Install-Package Serilog.AspNetCore -Version 3.1.0

在Program.cs文件进行Logger初始化

Main方法添加

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1bcb3cc3c3a0e418d08998fa3be8f5c5.html