配置 nLog .NET Core 3.1,缺少调试消息
Config nLog .NET Core 3.1, missing debug messages
我查看了 .net core 中 nLog 的大量配置,一切正常,只是我无法获取要记录的调试消息。如果我写一条信息消息或错误消息,它们就可以正常工作。所以这似乎是一个日志级别问题,但尽管我可能会尝试,但我似乎无法将级别低到足以写出调试消息。
我也看过这个:Missing trace\debug logs in ASP.NET Core 3?
Nlog.Web.AspNetCore 4.11
.Net 核心 3.1
这是我的配置:
startup.cs
中没有特定的日志记录
program.sc
public static void Main(string[] args)
{
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
CreateHostBuilder(args).Build().Run();
}
catch (Exception exception)
{
//NLog: catch setup errors
logger.Error(exception, "Stopped program because of exception");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
}
}
public 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();
appsettings.json
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
nlog.config
<?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"
throwConfigExceptions="true"
internalLogLevel="info"
internalLogFile="c:\temp\Nlog.log">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target name="database" xsi:type="Database" connectionString="${appsettings:name=ConnectionStrings.applog}" >
<commandText>
INSERT INTO [NLog] (
[ApplicationName],
[AppUserIdentity],
[LogDate] ,
[LogLevel] ,
[LogSource],
[LogAssembly] ,
[LogMessage] ,
[MachineName] ,
[DomainUser],
[CallSite],
[LogThread] ,
[LogException] ,
[ClientIp]
)
VALUES (
@ApplicationName,
@AppUserIdentity,
@LogDate ,
@LogLevel ,
@LogSource,
@LogAssembly,
@LogMessage ,
@MachineName ,
@DomainUser ,
@CallSite ,
@LogThread ,
@LogException,
@ClientIp
);
</commandText>
<parameter name="@ApplicationName" layout="${appsettings:name=AppName:default=Missing-Config}" />
<parameter name="@AppUserIdentity" layout="${aspnet-user-identity}" />
<parameter name="@LogDate" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}" />
<parameter name="@LogLevel" layout="${level}" />
<parameter name="@LogSource" layout="${logger} " />
<parameter name="@LogAssembly" layout="${assembly-version}" />
<parameter name="@LogMessage" layout="${message}" />
<parameter name="@MachineName" layout="${machinename}" />
<parameter name="@DomainUser" layout="${windows-identity:domain=true}" />
<parameter name="@CallSite" layout="${callsite}" />
<parameter name="@LogThread" layout="${threadid}" />
<parameter name="@LogException" layout="${exception}" />
<parameter name="@ClientIp" layout="${aspnet-request-ip}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" maxlevel="Error" final="true" writeTo="database" />
</rules>
</nlog>
已注入服务。此调用不会记录,但如果它是 _logger.LogInformation("xx"),则会记录。这个 ILogger 引用是 Microsoft.Extensions.Logging.ILogger
public TaxCalculationService(IUnderwritingRepository repository, ILogger<TaxCalculationService> logger)
{
_repository = repository;
_logger = logger;
}
public TaxCalculationResponseDto CalculateTaxes(TaxCalculationRequestDto request)
{
_logger.LogDebug("calculateTaxes request: " + JsonConvert.SerializeObject(request));
请尝试升级到 NLog.Web.AspNetCore v4.12.0,然后将 UseNLog()
更改为使用 RemoveLoggerFactoryFilter = true
,如下所示:
public 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(new NLogAspNetCoreOptions() { RemoveLoggerFactoryFilter = true });
另请参阅:https://github.com/NLog/NLog.Extensions.Logging/pull/482
您可能想要更新 NLog-LoggingRules 以忽略来自 Microsoft-Loggers 的噪音:
<rules>
<!-- Block output from noisy loggers -->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<logger name="System.Net.Http.*" maxlevel="Info" final="true" />
<!-- Write the good stuff to the databse -->
<logger name="*" minlevel="Debug" writeTo="database" />
</rules>
我查看了 .net core 中 nLog 的大量配置,一切正常,只是我无法获取要记录的调试消息。如果我写一条信息消息或错误消息,它们就可以正常工作。所以这似乎是一个日志级别问题,但尽管我可能会尝试,但我似乎无法将级别低到足以写出调试消息。
我也看过这个:Missing trace\debug logs in ASP.NET Core 3?
Nlog.Web.AspNetCore 4.11 .Net 核心 3.1
这是我的配置:
startup.cs
中没有特定的日志记录program.sc
public static void Main(string[] args)
{
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
CreateHostBuilder(args).Build().Run();
}
catch (Exception exception)
{
//NLog: catch setup errors
logger.Error(exception, "Stopped program because of exception");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
}
}
public 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();
appsettings.json
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
nlog.config
<?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"
throwConfigExceptions="true"
internalLogLevel="info"
internalLogFile="c:\temp\Nlog.log">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target name="database" xsi:type="Database" connectionString="${appsettings:name=ConnectionStrings.applog}" >
<commandText>
INSERT INTO [NLog] (
[ApplicationName],
[AppUserIdentity],
[LogDate] ,
[LogLevel] ,
[LogSource],
[LogAssembly] ,
[LogMessage] ,
[MachineName] ,
[DomainUser],
[CallSite],
[LogThread] ,
[LogException] ,
[ClientIp]
)
VALUES (
@ApplicationName,
@AppUserIdentity,
@LogDate ,
@LogLevel ,
@LogSource,
@LogAssembly,
@LogMessage ,
@MachineName ,
@DomainUser ,
@CallSite ,
@LogThread ,
@LogException,
@ClientIp
);
</commandText>
<parameter name="@ApplicationName" layout="${appsettings:name=AppName:default=Missing-Config}" />
<parameter name="@AppUserIdentity" layout="${aspnet-user-identity}" />
<parameter name="@LogDate" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}" />
<parameter name="@LogLevel" layout="${level}" />
<parameter name="@LogSource" layout="${logger} " />
<parameter name="@LogAssembly" layout="${assembly-version}" />
<parameter name="@LogMessage" layout="${message}" />
<parameter name="@MachineName" layout="${machinename}" />
<parameter name="@DomainUser" layout="${windows-identity:domain=true}" />
<parameter name="@CallSite" layout="${callsite}" />
<parameter name="@LogThread" layout="${threadid}" />
<parameter name="@LogException" layout="${exception}" />
<parameter name="@ClientIp" layout="${aspnet-request-ip}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" maxlevel="Error" final="true" writeTo="database" />
</rules>
</nlog>
已注入服务。此调用不会记录,但如果它是 _logger.LogInformation("xx"),则会记录。这个 ILogger 引用是 Microsoft.Extensions.Logging.ILogger
public TaxCalculationService(IUnderwritingRepository repository, ILogger<TaxCalculationService> logger)
{
_repository = repository;
_logger = logger;
}
public TaxCalculationResponseDto CalculateTaxes(TaxCalculationRequestDto request)
{
_logger.LogDebug("calculateTaxes request: " + JsonConvert.SerializeObject(request));
请尝试升级到 NLog.Web.AspNetCore v4.12.0,然后将 UseNLog()
更改为使用 RemoveLoggerFactoryFilter = true
,如下所示:
public 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(new NLogAspNetCoreOptions() { RemoveLoggerFactoryFilter = true });
另请参阅:https://github.com/NLog/NLog.Extensions.Logging/pull/482
您可能想要更新 NLog-LoggingRules 以忽略来自 Microsoft-Loggers 的噪音:
<rules>
<!-- Block output from noisy loggers -->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<logger name="System.Net.Http.*" maxlevel="Info" final="true" />
<!-- Write the good stuff to the databse -->
<logger name="*" minlevel="Debug" writeTo="database" />
</rules>