【问题标题】:ASP.NET Core EventLog providerASP.NET Core 事件日志提供程序
【发布时间】:2018-05-26 03:56:44
【问题描述】:

我有一个使用 .NET Framework ASP.NET Core 2.0 的项目,并希望实现对 Windows 事件日志的日志记录,就像我读到的 here

添加日志提供者

public class Program
{
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddEventSourceLogger();
                logging.AddConsole();
            })
            .UseStartup<Startup>()
            .Build();
    }

控制器

[Route("api/[controller]")]
public class ShortCodeController : Controller
{
        private readonly ILogger _logger;

        public ShortCodeController(ILogger<ShortCodeController> logger)
        {
            _logger = logger;

            _logger.LogInformation("INIT");
        }

        [HttpGet("{letters}/{digits}/{length}")]
        public string Get(bool letters, bool digits, int length)
        {
            _logger.LogError("TEST");

            return "value";
        }
    }

它适用于控制台,我看到了我的日志消息。但我无法使用事件查看器在事件日志中找到该消息。为什么?

【问题讨论】:

标签: c# asp.net asp.net-web-api logging asp.net-core


【解决方案1】:
logging.AddEventSourceLogger()

用于Event Tracing

对于Event Log,您要使用:

logging.AddEventLog()

【讨论】:

  • 它与这个特定的问题和答案有关。无论如何,我已经从包管理器控制台“Install-Package Microsoft.Extensions.Logging.EventLog -Version 2.0.0”运行了它,我现在可以看到它了。
  • 我的答案中的docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/… 链接有一个指向所需 NuGet 包的链接。
  • 对于AddEventSourceLogger,安装nuget包Microsoft.Extensions.Logging.EventSource
  • @thepirat000 这个问题的上下文明确想要AddEventSourceLogger,所以我很困惑为什么您可能添加了该评论?
【解决方案2】:

从 Nuget 安装 Microsoft.Extensions.Logging.EventLog。

在 program.cs 文件中包含 Microsoft.Extensions.Logging.EventLog

然后logging.AddEventLog() 将成为可能,因此您将能够实现您的目标

【讨论】:

  • 我仍然无法在 EventLog Viewer 中找到任何日志消息。你知道默认的 EventSource 名称是什么吗?
【解决方案3】:

“但我无法使用事件查看器在事件日志中找到该消息。为什么?”

除非您使用系统识别的 LogName 和 SourceName,否则事件日志会给您一个无意义的错误消息。

这对我来说适用于 net5.0:

 public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
                            {
                                logging.ClearProviders();
                                logging.AddEventLog(new EventLogSettings()
                                        {
                                            SourceName = ".NET Runtime",
                                            LogName = "Application",
                                        });
                            }
                         )
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
  • 2012-03-08
  • 1970-01-01
  • 2018-06-06
  • 2023-04-08
  • 2022-01-22
相关资源
最近更新 更多