使用nlogsettings.json文件时NLog不写入

问题描述 投票:0回答:1
I am trying to write logs using ILogger from Microsoft.Extensions.Logging and with NLog as a provider. I added a separate nlogsettings.json file to define configuration because to maintain appsettings file as minimum as possible and also to separate the config files during deployments to higher environments.

Here is my nlosettings.json where all my nlog configuration setup is defined. 





{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "NLog": {
    "internalLogFile": "c:\\Logs\\internal-nlog.txt",
    "LogLevel": {
      "Default": "Info",
      "Microsoft": "Warn",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "extensions": [
      { "assembly": "NLog.Extensions.Logging" },
      { "assembly": "NLog.Web.AspNetCore" }
    ],    
    "Targets": {
      "File": {
        "Type": "File",
        "FileName": "c:\\Logs\\service.log",
        "Layout": "${message}"
      }
    },
    "Rules": [
      {
        "Logger": "*",
        "MinLevel": "Debug",
        "WriteTo": "File"
      }
    ]
  }
}


Here is my Program.cs which is the main entry



public class Program
    {
    public static void Main(string[] args)
    {
       var logger = LogManager.Setup().LoadConfigurationFromFile("nlogsettings.json").GetCurrentClassLogger();
        logger.Info("Application starts");
        CreateHostBuilder(args).Build().Run();           
    }

    private static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        }).ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
        }).UseNLog();
}

Here is my Controller . it is not a fancy controller. Just a GET method is returning OK with a message

公共类 InventoryController : ControllerBase { 私有只读 ILogger _logger;
公共 InventoryController(ILogger 记录器, ILoggerFactory loggerFactory){_logger = logger;} [HttpGet]
公共异步任务 GetInventory() { 等待任务.Delay(100);
_logger.LogInformation("测试nlog日志写入"); 返回Ok(“成功”); } }

But when I debug it is hitting the breakpoint at logger.LogInformation but is not writing to the folder that is specified in nlogsettings.json. I tried different ways changing nlogsettings file. Can someone check and see if I am missing anything in the above set up?
.net-6.0 nlog ilogger
1个回答
0
投票

LogManager.Setup().LoadConfigurationFromFile
仅支持 XML 文件,不支持 JSON 文件。当对 NLog 进行故障排除时,启用NLog InternalLogger始终是一个好主意。

如果您安装了 NLog.Extensions.Logging-nuget-package,那么您可以执行以下操作:

var config = new ConfigurationBuilder()
                .SetBasePath(basePath ?? Directory.GetCurrentDirectory())
                .AddJsonFile("nlogsettings.json", optional: false, reloadOnChange: true)
                .Build();
var logger = LogManager.Setup()
                       .LoadConfigurationFromSection(config)
                       .GetCurrentClassLogger();

另请参阅 https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-configuration-with-appsettings.json

© www.soinside.com 2019 - 2024. All rights reserved.