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

走远了吗. 提交于 2019-12-07 04:08:25

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/56941898/how-to-setup-event-log-for-net-core-3-0-worker-service

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