ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NLog使用

2021-08-19 10:04:23  阅读:157  来源: 互联网

标签:args System NLog 使用 using logger public


一、NLog 简介

​ NLog 是一个基于.NET 平台编写的类库,我们可以适应NLog在应用程序中添加极为完善的跟踪代码。

​ NLog是一个简单灵活的.NET日志记录类库。通过使用NLog,我们可以在任何一种.NET语言中输出带有上下文的(contextual information)调试诊断信息,根据喜好配置其表现样式之后发送到一个或多个输出目标(target)中。

二、NLog安装配置

​ 安装NuGet包:NLog和NLog.Web.AspNetCore

​ 并新增 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">
  <!-- 启用.net core的核心布局渲染器 -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore" />
  </extensions>
  <!-- 写入日志的目标配置 -->
  <targets>
    <!-- 调试  -->
    <target xsi:type="File" name="debug" fileName="logs/debug-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
    <!-- 警告  -->
    <target xsi:type="File" name="warn" fileName="logs/warn-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
    <!-- 错误  -->
    <target xsi:type="File" name="error" fileName="logs/error-${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>
    <!-- 调试  -->
    <logger name="*" minlevel="Trace" maxlevel="Debug" writeTo="debug" />
    <!--跳过不重要的微软日志-->
    <logger name="Microsoft.*" maxlevel="Info" final="true" />
    <!-- 警告  -->
    <logger name="*" minlevel="Info" maxlevel="Warn" writeTo="warn" />
    <!-- 错误  -->
    <logger name="*" minlevel="Error" maxlevel="Fatal" writeTo="error" />
  </rules>
</nlog>

​ 在Program.cs类中注入

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NLog.Demo
{
    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<Startup>();
                }).UseNLog();
    }
}

三、NLog基本使用

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
    _logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
 {
    _logger.LogError("错误信息!");
    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();
        }

日志文件输出地址:bin\Debug\netcoreapp3.1\logs\日志文件

标签:args,System,NLog,使用,using,logger,public
来源: https://www.cnblogs.com/netsd/p/15160279.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有