ASP.NET Core EventLog提供程序

Hag*_*rje 4 c# asp.net logging asp.net-web-api asp.net-core

我有一个使用.NET Framework ASP.NET Core 2.0的项目,并且想要实现对Windows事件日志的记录,就像我在这里阅读的一样

添加日志提供者

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();
    }
Run Code Online (Sandbox Code Playgroud)

控制者

[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";
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

mjw*_*lls 5

logging.AddEventSourceLogger()
Run Code Online (Sandbox Code Playgroud)

用于事件跟踪

对于事件日志,您要使用:

logging.AddEventLog()
Run Code Online (Sandbox Code Playgroud)