小编Pau*_*her的帖子

记录 LogLevel - Windows 事件日志中未显示信息


我在将 **信息** 日志记录到 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 …

logging event-log asp.net-core asp.net5

9
推荐指数
1
解决办法
3941
查看次数

标签 统计

asp.net-core ×1

asp.net5 ×1

event-log ×1

logging ×1