【发布时间】:2020-07-18 05:05:32
【问题描述】:
我遇到了 ILogger 消息未发送到任何 NLog 目标的问题。 因此,例如,如果我有以下代码
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
}
}
它没有出现在任何预期的目标中。
我正在为 NLog 使用以下配置
"NLog": {
"autoReload": true,
"throwConfigExceptions": true,
"internalLogLevel": "Info",
"internalLogFile": "log/internal-nlog.log",
"extensions": [
{ "assembly": "NLog.Extensions.Logging" },
{ "assembly": "NLog.Web.AspNetCore" },
{ "assembly": "Microsoft.ApplicationInsights.NLogTarget" }
],
"targets": {
"console": {
"type": "Console"
},
"aiTarget": {
"type": "ApplicationInsightsTarget"
},
"logFile": {
"type": "File",
"fileName": "log/nlog-${shortdate}.log"
}
},
"rules": [
{
"logger": "*",
"minLevel": "Info",
"writeTo": "logFile"
},
{
"logger": "*",
"minLevel": "Debug",
"writeTo": "console"
},
{
"logger": "*",
"minLevel": "Trace",
"writeTo": "aiTarget"
},
{
"logger": "Microsoft.*",
"maxLevel": "Info",
"final": "true"
}
]
}
在 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);
});
}
我是否遗漏了一些明显的东西?
【问题讨论】:
标签: c# nlog asp.net-core-3.1