2020-09-23

.NET Core 下使用 NLog 记录日志

NLog is a flexible and free logging platform for various .NET platforms, including .NET standard. NLog makes it easy to write to several targets. (database, file, console) and change the logging configuration on-the-fly.

  • 官网:https://nlog-project.org
  • 开源地址:https://github.com/NLog/NLog

最佳实践

控制台项目

在项目中添加组件包

Install-Package Microsoft.Extensions.Configuration.JsonInstall-Package NLogInstall-Package NLog.Extensions.Logging
{ "Logging": { "LogLevel": {  "Default": "Information",  "Microsoft": "Warning",  "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*"}
<?

添加一个类Runner.cs

using Microsoft.Extensions.Logging;namespace ConsoleDemo{ public class Runner {  private readonly ILogger<Runner> _logger;  public Runner(ILogger<Runner> logger)  {   _logger = logger;  }  public void DoAction(string name)  {   _logger.LogDebug(20, "Doing hard work! {Action}", name);  } }}

通过注入的方式调用。

using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;using NLog;using NLog.Extensions.Logging;using System;namespace ConsoleDemo{ class Program {  static void Main(string[] args)  {   var logger = LogManager.GetCurrentClassLogger();   try   {    var config = new ConfigurationBuilder().SetBasePath(System.IO.Directory.GetCurrentDirectory())              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)              .Build();    var servicesProvider = BuildDi(config);    using (servicesProvider as IDisposable)    {     var runner = servicesProvider.GetRequiredService<Runner>();     runner.DoAction("Action1");     Console.WriteLine("Press ANY key to exit");     Console.ReadKey();    }   }   catch (Exception ex)   {    // NLog: catch any exception and log it.    logger.Error(ex, "Stopped program because of exception");    throw;   }   finally   {    LogManager.Shutdown();   }  }  private static IServiceProvider BuildDi(IConfiguration config)  {   return new ServiceCollection()    .AddTransient<Runner>() // Runner is the custom class    .AddLogging(loggingBuilder =>    {     // configure Logging with NLog     loggingBuilder.ClearProviders();     loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);     loggingBuilder.AddNLog(config);    })    .BuildServiceProvider();  } }}

运行项目,项目根目录下会多出两个日志文件。

AspNetCore项目

在项目中添加组件包

Install-Package NLogInstall-Package NLog.Web.AspNetCore
{ "Logging": { "LogLevel": {  "Default": "Trace",  "Microsoft": "Warning",  "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*"}
<?

Program.cs中使用NLog

using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Hosting;using Microsoft.Extensions.Logging;using NLog.Web;using System;namespace WebDemo{ public class Program {  public static void Main(string[] args)  {   var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();   try   {    logger.Debug("init main");    CreateHostBuilder(args).Build().Run();   }   catch (Exception exception)   {    //NLog: catch setup errors    logger.Error(exception, "Stopped program because of exception");    throw;   }   finally   {    // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)    NLog.LogManager.Shutdown();   }  }  public static IHostBuilder CreateHostBuilder(string[] args) =>   Host.CreateDefaultBuilder(args)    .ConfigureWebHostDefaults(webBuilder =>    {     webBuilder.UseStartup<Startup>();    })    .ConfigureLogging(logging =>    {     logging.ClearProviders();     logging.SetMinimumLevel(LogLevel.Trace);    }).UseNLog(); // NLog: Setup NLog for Dependency injection }}

一切准备就绪,在任意地方写日志。

using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Logging;using System;using System.Collections.Generic;using System.Linq;namespace WebDemo.Controllers{ [ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase {  private static readonly string[] Summaries = new[]  {   "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"  };  private readonly ILogger<WeatherForecastController> _logger;  public WeatherForecastController(ILogger<WeatherForecastController> logger)  {   _logger = logger;  }  [HttpGet]  public IEnumerable<WeatherForecast> Get()  {   _logger.LogInformation("Hello, this is a Weather api!");   var rng = new Random();   return Enumerable.Range(1, 5).Select(index => new WeatherForecast   {    Date = DateTime.Now.AddDays(index),    TemperatureC = rng.Next(-20, 55),    Summary = Summaries[rng.Next(Summaries.Length)]   }).ToArray();  } }}

调用上面api,查看日志文件。

更多用法,请查看官方wiki文档:https://github.com/NLog/NLog/wiki

原文转载:http://www.shaoqun.com/a/477544.html

阿里巴巴 批发:https://www.ikjzd.com/w/1084

bap:https://www.ikjzd.com/w/1492

haofang:https://www.ikjzd.com/w/1046


NLogisaflexibleandfreeloggingplatformforvarious.NETplatforms,including.NETstandard.NLogmakesiteasytowritetoseveraltargets.(database,file,console)andchangetheloggingconfigurationon-the-fly.官网:https://n
c88:https://www.ikjzd.com/w/1017.html
易联通:https://www.ikjzd.com/w/1854.html
转运四方:https://www.ikjzd.com/w/1342
卖家求助:热卖的Listing遭遇断货,我该怎么办?:https://www.ikjzd.com/home/115361
日本站旺季节点有哪些?怎样才能在日本站达到卖疯了的效果?:https://www.ikjzd.com/home/7807

No comments:

Post a Comment