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


    1、根目录新建nlog.config配置文件

    <?xml version="1.0"?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          autoReload="true"
          internalLogLevel="Warn"
          internalLogFile="${basedir}logsinternal-nlog.txt">
    
      <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
      </extensions>
    
      <targets>
        <target name="allfile" xsi:type="File"
                fileName="${basedir}logsGDStationaryNetCore${shortdate}.log"
                encoding="utf-8"
                layout="[${longdate}][${machinename}][${level}] ${message} ${exception}" />
      </targets>
      <rules>
        <!--All logs, including from Microsoft-->
        <logger name="*" minlevel="Trace" writeTo="allfile" />
    
        <!--Skip Microsoft logs and so log only own logs-->
        <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
        <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
      </rules>
    </nlog>

    2、添加NLog包

    Install-Package NLog.Web.AspNetCore

    3、Configure配置

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                // 
                env.ConfigureNLog("nlog.config");
                //安装System.Text.Encoding.CodePages
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                //add NLog to ASP.NET Core
                loggerFactory.AddNLog();
                //add NLog.Web
                // 修改为 CreateWebHostBuilder(args).UseNLog().Build().Run();
                //app.AddNLogWeb();
    
                app.UseMvc();
            }

    Main方法里配置使用

     public static void Main(string[] args)
            {
                CreateWebHostBuilder(args).UseNLog().Build().Run();
            }

    4、使用

    public class ValuesController : ControllerBase
        {
            private readonly ILogger<ValuesController> _logger;
    
            public ValuesController(ILogger<ValuesController> logger = null)
            {
                if (null != logger)
                {
                    _logger = logger;
                }
            }
    
            // GET api/values
            [HttpGet]
            public ActionResult<IEnumerable<string>> Get()
            {
                _logger.LogInformation($"测试一条日志.");
                return new string[] { "value1", "value2" };
            }
            
        }
  • 相关阅读:
    windows下 CodeBlock13-12 实验 C++11 测试
    用矩阵运算实现最小二乘法曲线拟合算法
    winXP 系统下ubuntu-12.04 硬盘安装
    TCP服务器并发编程构架:完成端口IOCP模式
    TCP服务器并发编程构架:完成例程IRP模式
    续:双缓存队列_模板类
    双缓存静态循环队列(三)
    如何在只知道SQL_ID时,查询到此sql语句的执行计算机名称(是两三天前的SQL语句)
    RMAN Catalog 和 Nocatalog 的区别
    异机恢复后ORA-01152错误解决
  • 原文地址:https://www.cnblogs.com/zhouxiaoyun/p/10765427.html
Copyright © 2020-2023  润新知