我正在尝试将我的应用程序输出的任何内容(包括信息消息)记录到事件日志中,但 ILogger 接口只在那里写入警告和上面的内容。
这是我的程序.cs:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((context, logging) =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddEventLog(context.Configuration.GetSection("Logging:EventLog").Get<EventLogSettings>());
logging.SetMinimumLevel(LogLevel.Information);
});
Run Code Online (Sandbox Code Playgroud)
我的 appsettings.json:
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
},
"EventLog": {
"LogName": "Application",
"SourceName": "My cool App"
}
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我执行以下操作:
_logger.LogInformation("test information");
_logger.LogWarning("test warning");
_logger.LogDebug("test debug");
_logger.LogError("test error");
_logger.LogTrace("test trace");
_logger.LogCritical("test critical");
Run Code Online (Sandbox Code Playgroud)
在控制台输出中,我收到了所有测试消息。但是在事件日志中我只得到警告、错误和严重。
我在这里做错了什么?
在事件日志中,我只收到警告、错误和严重。
从这个文件,你可以找到的事件将被记录为警告或更高级别时,我们使用Microsoft.Extensions.Logging.EventLog。
要记录低于 的事件LogLevel.Warning,请明确设置日志级别,如下所示。
"EventLog": {
"LogLevel": {
"Default": "Trace"
}
}
Run Code Online (Sandbox Code Playgroud)
此外,请注意跟踪日志消息可能包含敏感的应用程序数据,因此不应在生产环境中启用。
并且从下面的EventLogLogger.cs的源代码中,我们可以发现它会使用Information、Warning、Error作为基于LogLevel的EventLog EntryType来写EventLog(s)。
private EventLogEntryType GetEventLogEntryType(LogLevel level)
{
switch (level)
{
case LogLevel.Information:
case LogLevel.Debug:
case LogLevel.Trace:
return EventLogEntryType.Information;
case LogLevel.Warning:
return EventLogEntryType.Warning;
case LogLevel.Critical:
case LogLevel.Error:
return EventLogEntryType.Error;
default:
return EventLogEntryType.Information;
}
}
Run Code Online (Sandbox Code Playgroud)
测试结果
_logger.LogTrace("test trace1");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
604 次 |
| 最近记录: |