Pau*_*her 9 logging event-log asp.net-core asp.net5
我在将 **信息** 日志记录到 Windows 事件日志时遇到了一些问题。
从空白的 ASP.NET Core-Web-API .Net 5 开始
我将以下内容编辑到 Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddEventLog(eventLogSettings =>
{
eventLogSettings.SourceName = "MyTestLog";
});
});
webBuilder.UseStartup<Startup>();
});
Run Code Online (Sandbox Code Playgroud)
并将一些示例日志写入控制器
public IEnumerable<WeatherForecast> Get()
{
_logger.LogCritical("Critical Log");
_logger.LogError("Error Log");
_logger.LogWarning("Warning Log");
_logger.LogInformation("Info Log");
_logger.LogDebug("Debug Log");
_logger.LogTrace("Trace Log");
...
}
Run Code Online (Sandbox Code Playgroud)
如果我运行它,它会在控制台中显示“严重”-“错误”-“警告”-“信息”的日志。与 AppSettings 中配置的内容匹配。
应用程序设置.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 Windows 事件日志中,它仅显示严重、错误和警告日志,而不关心我的应用程序设置。我确信这是一个简单的配置问题,但我没有找到任何相关文档。
如何获取 Windows 事件日志中包含 Loglevel-Information 的日志?
Bra*_*ang 16
根据这篇文章,您可以发现ASP.NET Core提供了事件日志记录功能。与其他提供程序不同,EventLog 提供程序不会继承默认的非提供程序设置。
如果未指定 EventLog 日志设置,则它们默认为LogLevel.Warning.
要记录低于 的事件LogLevel.Warning,请显式设置日志级别。以下示例将事件日志默认日志级别设置为LogLevel.Information:
"Logging": {
"EventLog": {
"LogLevel": {
"Default": "Information"
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3941 次 |
| 最近记录: |