C# ILOGER调试消息未显示在NLog目标中
我有一个问题,ILogger消息没有发送到任何NLog目标。 例如,如果我有以下代码C# ILOGER调试消息未显示在NLog目标中,c#,nlog,asp.net-core-3.1,C#,Nlog,Asp.net Core 3.1,我有一个问题,ILogger消息没有发送到任何NLog目标。 例如,如果我有以下代码 public class TestConnectionController : ControllerBase { private readonly ILogger<TestConnectionController> _logger; /// <summary> /// Initializes a new instance of the <see cref=
public class TestConnectionController : ControllerBase
{
private readonly ILogger<TestConnectionController> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="TestConnectionController"/> class.
/// </summary>
public TestConnectionController(ILogger<TestConnectionController> logger)
{
_logger = logger;
}
public string TestLog()
{
_logger.LogDebug("This is an test"); <- Should show up in the logging targets
}
}
在ConfigureServices方法期间,将以以下方式加载到应用程序中
public static void AddNLog(this IServiceCollection service, IConfiguration config)
{
var nLogConfig = new NLogLoggingConfiguration(config.GetSection("NLog"));
service.AddLogging(builder =>
{
builder.ClearProviders();
builder.AddNLog(nLogConfig);
});
}
我是否遗漏了一些明显的内容?这可能是Microsoft日志记录扩展的默认过滤器。默认情况下,它会过滤调试和跟踪日志 在appsettings.json中,您(也)需要类似的内容:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
这也是建议的设置:
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>
{
webBuilder.UseStartup();
})
.ConfigureLogging(日志=>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.logging.LogLevel.Trace);
})
.UseNLog();//NLog
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}