asp.net-core - 记录 LogLevel - Windows 事件日志中未显示信息

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

<分区>


我在将 **Information** Logs 记录到 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>();
    });

并将一些示例日志写入 Controller

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 中的配置。
应用设置.json:

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

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

如何在 Windows 事件日志中获取具有日志级别信息的日志?

最佳答案

根据 this article ,您会发现 ASP.NET Core 提供了事件日志记录功能。与其他提供者不同,EventLog 提供者不继承默认的非提供者设置。

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

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

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

关于asp.net-core - 记录 LogLevel - Windows 事件日志中未显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71291962/

相关文章:

asp.net-core - 缺少客户端密码或客户端证书

asp.net-core - UI 元素仅在焦点移离原始按钮/输入时更新

php - 在 Zend 框架应用程序中记录设计模式

c# - 授予我的代码事件日志权限

ubuntu - 无法访问部署到 ubuntu 的 .Net Core 2.1 Web 应用程序

asp.net-core - 如何向 ASP.NET Core Identity 添加更多字段?

c# - 写入应用程序事件源是否可以接受?

c# - NLog 未写入 EventLog

java - 在 React Native 的桥接 Java 方法中使用 Logback

python - 如何动态地将变量传递给自定义 python 日志记录类中的类 __init__ 方法