ASP.NET Core EventLog provider

做~自己de王妃 提交于 2020-03-18 21:39:43

问题


I have a project using .NET Framework ASP.NET Core 2.0, and want to implement logging to windows event log, like i read here

Add log providers

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();
    }

Controller

[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";
        }
    }

And it works for console, I see my log messages. But i can't find that messages in event log using event viewer. Why?


回答1:


logging.AddEventSourceLogger()

is for Event Tracing.

For the Event Log, you want to use:

logging.AddEventLog()



回答2:


Install Microsoft.Extensions.Logging.EventLog from Nuget.

Include the Microsoft.Extensions.Logging.EventLog on the program.cs file

Then logging.AddEventLog() will be possible and consequently you will be able to achieve your goal



来源:https://stackoverflow.com/questions/47773058/asp-net-core-eventlog-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!