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

发布于 2025-01-10 23:12:13 字数 1468 浏览 0 评论 0原文


I ran into some problems with logging **Information** Logs to Windows event log.
Starting from a blank 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>();
    });

并将一些示例日志写入控制器中。

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");
    ...
}

如果我运行它,它会在控制台中显示“严重”-“错误”-“警告”-“信息”的日志。与 AppSettings 中配置的内容匹配。
appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

在 Windows 事件日志中,它仅显示严重、错误和警告日志,而不关心我的应用程序设置。 我确信这是一个简单的配置问题,但我没有找到任何相关文档。

如何获取 Windows 事件日志中包含 Loglevel-Information 的日志?

I ran into some problems with logging **Information** Logs to Windows event log.
Starting from a blank ASP.NET Core-Web-API .Net 5

I edited the following to 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>();
    });

and wrote some sample logs into a Controler

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");
    ...
}

If I Run it, it shows logs for Critical - Error - Warning - Information in Console. Matching what is configured in AppSettings.
appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

In Windows event log it only shows Critical, Error and Warning -Logs, not caring for my appsettings whatsoever.
I'm sure this is an easy configuration problem, but i don't find any documentation for this.

How do I get Logs with Loglevel-Information in Windows event log ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春夜浅 2025-01-17 23:12:13

根据这篇文章,你会发现ASP.NET Core提供了事件日志记录功能。与其他提供程序不同,EventLog 提供程序不会继承默认的非提供程序设置。

如果未指定 EventLog 日志设置,则默认为 LogLevel.Warning

要记录低于 LogLevel.Warning 的事件,请显式设置日志级别。以下示例将事件日志默认日志级别设置为 LogLevel.Information

"Logging": {
  "EventLog": {
    "LogLevel": {
      "Default": "Information"
    }
  }
}

According to this article, you could find the ASP.NET Core provide the event logging feature. Unlike the other providers, the EventLog provider does not inherit the default non-provider settings.

If EventLog log settings aren't specified, they default to LogLevel.Warning.

To log events lower than LogLevel.Warning, explicitly set the log level. The following example sets the Event Log default log level to LogLevel.Information:

"Logging": {
  "EventLog": {
    "LogLevel": {
      "Default": "Information"
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文