Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log events to the Event Viewer in an ASP.NET Core Web API?

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

like image 240
Fabricio Rodriguez Avatar asked Sep 13 '18 08:09

Fabricio Rodriguez


People also ask

How do I add logs to Event Viewer?

Open "Event Viewer" by clicking the "Start" button. Click "Control Panel" > "System and Security" > "Administrative Tools", and then double-click "Event Viewer" Click to expand "Windows Logs" in the left pane, and then select "Application". Click the "Action" menu and select "Save All Events As".

What tool allows you view event logs?

SolarWinds Log Analyzer (FREE TRIAL) SolarWinds Log Analyzer is an event log monitoring tool for Windows that collects event log data. You can monitor event log data in real-time through syslog, SNMP traps, and system event logs. Data can be collected and monitored through one user interface.

How do I run event log?

Open Run dialog by pressing Windows+R. Type eventvwr. msc (or eventvwr.exe) and click OK. Run Event Viewer in Microsoft Management Console.


1 Answers

For anyone who stumbles upon this using .NET Core 2.2, the Event Log logger (from Microsoft.Extensions.Logging.EventLog) should now be added in the Program.cs:

public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging((context, logging) =>
        {
            logging.AddEventLog();
        })
        .UseStartup<Startup>();

This is noted at the bottom of the .NET Core Logging docs.

UPDATE: In case someone still lands here post .NET Core 2.2, this is still relevant for .NET Core 3.1 logging.

like image 128
Tom Faltesek Avatar answered Oct 31 '22 13:10

Tom Faltesek