I'm trying to log to the Event Viewer in an ASP.NET Core 2.1 Web API, hosted on Windows Server 2016 Standard.
I've got this in my controller:
private readonly ILogger<MyController> _logger;
private readonly MyContext _context;
public TestController(MyContext context, ILogger<MyController> logger)
{
_context = context;
_logger = logger;
}
But I think I'm doing something wrong in my CreateWebHostBuilder() method in Program.cs because it's not working:
I had this:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
and I modified it to this:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddEventSourceLogger();
});
But I must be doing something wrong... Any ideas? I read here that apparently EVent Viewer logging is now baked into .Net Core 2.1 Write to EventLog in .Net Core