Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup event log for .NET Core 3.0 Worker Service

I'm working with the new Worker Service app template with .NET Core 3.0 Preview and am trying to add event logging using the AddEventLog method. However, I cannot see any of my logs via the Event Viewer in Windows.

I have a very simple Worker app setup and have configured logging in the Program.cs file as follows:

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
    .UseWindowsService()
    .ConfigureLogging((context, logging) =>
    {
        logging.AddEventLog(new EventLogSettings()
        {
            SourceName = "MyTestSource",
            LogName = "MyTestLog"
        });
    })
    .ConfigureServices((hostContext, services) =>
    {
        services.AddHostedService<Worker>();
    });

I then have some logging statements in the Worker.cs file as follows:

private readonly ILogger<Worker> _logger;

public Worker(ILogger<Worker> logger)
{
    _logger = logger;
}

public override async Task StartAsync(CancellationToken cancellationToken)
{
    _logger.LogInformation($"Worker started at: {DateTime.Now}");
    await base.StartAsync(cancellationToken);
}

public override async Task StopAsync(CancellationToken cancellationToken)
{
    _logger.LogInformation($"Worker stopped at: {DateTime.Now}");
    await base.StopAsync(cancellationToken);
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    while (!stoppingToken.IsCancellationRequested)
    {
        _logger.LogInformation( $"Worker running at: {DateTime.Now}");
        await Task.Delay(1000, stoppingToken);
    }
}

To setup the event logs, I ran the following from an elevated Powershell prompt:

New-EventLog -LogName MyTestLog -Source MyTestSource

If I open the Event Viewer I can see "MyTestLog" listed below "Applications and Services Logs".

Then, to set up my Worker as a Windows service, I ran the following commands from an elevated command prompt:

dotnet publish -o publish (Publishes project and outputs to publish directory)

sc create MyTestService binPath=<path to exe in publish directory>

The service is created successfully, and I can see it in the Services viewer application. From there, I manually start the service and then check back in the Event Viewer and no logs are displayed.

I was expecting there to be some logs. However, the "MyTestLog" section remains empty in the Event Viewer.

like image 633
Jack Kinsey Avatar asked Jul 08 '19 20:07

Jack Kinsey


People also ask

How do I enable event logs?

Select and hold (or right-click) Verbose and then select Properties from the pop-up context menu. Select the General tab on the Properties dialog box, and then select the Enable Logging option near the middle of the property page. This will enable verbose logging. Restart the computer for the changes to take effect.


1 Answers

So I was able to find the issue reported here.

Essentially it boils down to a change in the latest .NET Core 3.0 Preview where Event Logs are filtered at the warning level by default now. Thus, you cannot see any information logs.

The fix was to simply edit the appsettings.json file to look like the following:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    }
  }
}

This overrides the default set and allows information logs to be viewed again.

like image 123
Jack Kinsey Avatar answered Sep 29 '22 21:09

Jack Kinsey