美文网首页
在.NetCore2.2中使用NLog和NLog.Mongo将日

在.NetCore2.2中使用NLog和NLog.Mongo将日

作者: GH_12d8 | 来源:发表于2019-08-07 16:54 被阅读0次

    1. NuGet包管理器或包管理器控制台命令安装NLog和NLog.Mongo

    Install-Package NLog.Web.AspNetCore
    Install-Package NLog.Mongo
    

    2. 配置appsettings.json文件NLog项

    "NLog": {
        "throwConfigExceptions": true,
        "targets": {
          "logfile": {
            "type": "File",
            "fileName": "./log/nlog-${shortdate}.log"
          },
          "logconsole": {
            "type": "Console"
          },
          "logmongo":{
            "type":"Mongo",
            "connectionString":"mongodb://192.168.111.2/NLOG",
            "collectionName":"SysErrorLog",
            "cappedCollectionSize":"26214400"
          }
        },
        "extensions": {
          "NLog.Mongo": {
              "assembly": "NLog.Mongo"
          }
        },
        "rules": [
          {
            "logger": "*",
            "minLevel": "Debug",
            "writeTo": "logmongo"
          },
          {
            "logger": "*",
            "minLevel": "Warn",
            "writeTo": "logfile"
          },
          {
            "logger": "*",
            "minLevel": "Debug",
            "writeTo": "logconsole"
          }
        ]
      }
    

    3. 修改Startup.cs文件ConfigureServices,添加如下代码

                NLog.LogManager.Configuration = new NLogLoggingConfiguration(Configuration.GetSection("NLog"));
                services.AddLogging(loggingBuilder =>
                {
                    loggingBuilder.ClearProviders();
                    loggingBuilder.SetMinimumLevel(LogLevel.Trace);
                    loggingBuilder.AddNLog(Configuration);
                });
    

    相关文章

      网友评论

          本文标题:在.NetCore2.2中使用NLog和NLog.Mongo将日

          本文链接:https://www.haomeiwen.com/subject/nbcadctx.html