• NetCore使用Nlog记录日志


    当前环境: win10+vs2019+netcore3.1

    添加Nuget包引用:NLog.Web.AspNetCore

     2. 添加 nlog.config配置文件 或者安装包 nlog.confg

    <?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="c:	empinternal-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" name="allfile" fileName="logs
    log-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" name="ownFile-web" fileName="logs
    log-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 name="*" minlevel="Trace" writeTo="allfile" />
    
            <!--Skip non-critical Microsoft logs and so log only own logs-->
            <logger name="Microsoft.*" maxlevel="Info" final="true" />
            <!-- BlackHole without writeTo -->
            <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
        </rules>
    </nlog>
    View Code

    3. 在CreateHostBuilder 中添加使用Nlog ( ConfigureLogging)

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
           return Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    }).ConfigureLogging(
                        logging =>
                        {
                            // 清除系统默认的日志
                            //logging.ClearProviders();
                            logging.SetMinimumLevel(LogLevel.Trace);
                        }).UseNLog();
    }

    4. 配置文件路径

    NLog.Web.NLogBuilder.ConfigureNLog("nlog.config")

    5. 调用

        public class HomeController : Controller
        {
            private readonly ILogger<HomeController> _logger;
    
            public HomeController(ILogger<HomeController> logger)
            {
                _logger = logger;
                _logger.LogDebug(1, "NLog injected into HomeController");
            }
    
            public IActionResult Index()
            {
                _logger.LogInformation("Hello, this is the index!");
                return View();
            }
    }

    6. 效果

    在binDebug etcoreapp3.1 目录下生成了logs文件夹

  • 相关阅读:
    openresty + gor+minio 集成
    openresty docker 镜像集成gor
    goreplay v1.1.0 支持pro特性docker 镜像
    编译goreplay v1.1.0 minio 集成支持
    编译goreplay v1.1.0 支持二进制协议捕捉
    goreplay v1.1.0 发布
    super-expressive 可以基于js 直接编写正则
    monio 的一些安全实践
    minio+ nginx rewrite 实现saas租户的个性化管理
    LDAP概念和原理介绍
  • 原文地址:https://www.cnblogs.com/wtujvk/p/13222233.html
Copyright © 2020-2023  润新知