ASP.NET Core中使用NLog记录日志

2021-05-29 21:03

阅读:517

标签:oar   log   oge   range   读取   row   evel   core   xmlns   

1:添加Nlog的组件

直接在NuGet里面搜索NLog.Web.AspNetCore

2:然后就是配置服务

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        //public static IHostBuilder CreateHostBuilder(string[] args) =>
        //   Host.CreateDefaultBuilder(args)
        //       .ConfigureWebHostDefaults(webBuilder =>
        //       {
        //           webBuilder.UseStartup();
        //           webBuilder.UseNLog();
        //       });

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup();
                }).UseNLog();
        //鼠标放在UseNLog() 会显示是IHostBuilder的拓展方法IHostBuilder.UseNlog()
    }

3:配置文件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"
      throwConfigExceptions="true"
      internalLogLevel="info"
      internalLogFile="E:\log\internal-nlog.txt">
  
  
  
  
  
  targets>
    
    
    target xsi:type="File" name="allfile" fileName="D:\Log\nlog-all-${shortdate}.log"
             layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
     
    target xsi:type="File" name="ownFile-web" fileName="D:\Log\nlog-my-${shortdate}.log"
             layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
    target xsi:type="Null" name="blackhole" />
  targets>
  rules>
    
    
    logger name="*" minlevel="Trace" writeTo="allfile" />

    
    
    
    logger name="Microsoft.*" minlevel="Trace"  final="true" />
    
    logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  rules>
nlog>

4:记录输出

[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 _logger;
        private readonly IConfiguration Configuration;

        public WeatherForecastController(ILogger logger, IConfiguration configuration)
        {
            _logger = logger;
            Configuration = configuration;
        }

        [HttpGet]
        public IEnumerable Get()
        {
            _logger.LogError("这是错误信息");
            _logger.LogDebug("这是调试信息");
            _logger.LogInformation("这是提示信息");
            
            try
            {
                throw new Exception("BOBOBOBOBBBBBBBBBBexection");
            }
            catch (Exception ex)
            {
                _logger.LogInformation("这是提示信息"+ ex.Message);
               
            }
            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();
        }
    }

5:效果

技术图片

 

ASP.NET Core中使用NLog记录日志

标签:oar   log   oge   range   读取   row   evel   core   xmlns   

原文地址:https://www.cnblogs.com/ZkbFighting/p/14703489.html

上一篇:文件上传漏洞测试

下一篇:.NET身份验证


评论


亲,登录后才可以留言!