Extensions: [Worker Service] After moving to Preview 6, logging doesn't work anymore in a Windows Service

Created on 13 Jun 2019  路  9Comments  路  Source: dotnet/extensions

Describe the bug

After moving to .NET Core 3.0 Preview 6, my Worker Service which is installed as a Windows Service doesn't output anymore logs in Event Viewer.

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core: 3.0.100-preview6-012264
  2. Create a new Worker Service project in Visual Studio 2019
  3. Install the following NuGet package: Microsoft.Extensions.Hosting.WindowsServices, version 3.0.0-preview6.19304.6
  4. Add .UseWindowsService() in the CreateHostBuilder() method
  5. From the command line, publish the project with dotnet publish to a folder (like C:\WorkerService)
  6. Register it as a Windows Service by using the command sc create worker1 binPath=c:\WorkerService\WorkerService.exe
  7. Run the service with sc start worker1

Expected behavior

All the logging is displayed in the Application section of the Windows logs inside Event Viewer

Additional context

With .NET Core 3.0 Preview 5, I was able to see the logging in Event Viewer just fine.

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100-preview6-012264
 Commit:    be3f0c1a03

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18362
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.0.100-preview6-012264\

Host (useful for support):
  Version: 3.0.0-preview6-27804-01
  Commit:  fdf81c6faf

.NET Core SDKs installed:
  2.1.600 [C:\Program Files\dotnet\sdk]
  2.1.601 [C:\Program Files\dotnet\sdk]
  2.1.602 [C:\Program Files\dotnet\sdk]
  2.1.604 [C:\Program Files\dotnet\sdk]
  2.1.700-preview-009618 [C:\Program Files\dotnet\sdk]
  2.1.700 [C:\Program Files\dotnet\sdk]
  2.1.800-preview-009677 [C:\Program Files\dotnet\sdk]
  2.1.800-preview-009696 [C:\Program Files\dotnet\sdk]
  2.2.100 [C:\Program Files\dotnet\sdk]
  3.0.100-preview6-012264 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview6.19307.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview6-27804-01 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0-preview6-27804-01 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] 
area-hosting bug

Most helpful comment

This is an unintentional side effect of fact that we add the event log provider by default now at the warning level.
https://github.com/aspnet/Extensions/blob/f12d709976e382672ce100bc00381a8847f06489/src/Hosting/Hosting/src/Host.cs#L102

You can work around this by adding this entry to your configuration:

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

This turns the level for the event log provider to Information for those categories.

We may need to re-think this or possibly remove the filter when running in a windows service.

cc @Tratcher

All 9 comments

This is an unintentional side effect of fact that we add the event log provider by default now at the warning level.
https://github.com/aspnet/Extensions/blob/f12d709976e382672ce100bc00381a8847f06489/src/Hosting/Hosting/src/Host.cs#L102

You can work around this by adding this entry to your configuration:

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

This turns the level for the event log provider to Information for those categories.

We may need to re-think this or possibly remove the filter when running in a windows service.

cc @Tratcher

Removing the filter would mean we'd log Information by default to the EventLog right? That's likely to be pretty noisy.

Removing the filter would mean we'd log Information by default to the EventLog right? That's likely to be pretty noisy.

Right only for the windows service case I mean where everything is logged to the event log.

The change in filter level is by-design (logging only Warning and up by default to the Event Log), and the workaround @davidfowl posted should work. Closing as we don't plan to change this in 3.0.

I have a question is there a way to move the logs from Application section of the Windows logs inside Event Viewer and create a custom LogName as it possible if application is not calling .UseWindowsService()

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseWindowsService()
            //.ConfigureLogging(loggerFactory => loggerFactory.AddEventLog( new EventLogSettings() { SourceName = "CallerTestCacheServiceCoreAPI" , LogName = "CallerTestCacheServiceCoreAPILog" }))
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddEventLog(new EventLogSettings()
                {
                    SourceName = "CallerTestCacheServiceCoreAPI",
                    LogName = "CallerTestCacheServiceCoreAPILog",

                });
            })
        .ConfigureServices(services =>
        {

            services.AddTransient<CallCache>();
            services.AddTransient<CallCacheEverySecond>();
            services.AddHttpClient();
            services.AddHostedService<WorkerEverySecond>();
            services.AddHostedService<WorkerEveryMinute>();
        });

No matter what im doing it i cannot overwrite the default logName in my example WorkerService1(my sample application has this name) Adding EventLogSettings() SourceName LogName just do not work

@Tratcher it`s work thank you very much

@nikolay-dimitrov i don't undestand the solution of @Tratcher . Please explain more better the solution? Thanks

@nikolay-dimitrov i don't undestand the solution of @Tratcher . Please explain more better the solution? Thanks

The solution is to add something like this:

services.Configure<EventLogSettings>(options =>
{
    options.SourceName = "MySourceName";
    options.LogName = "MyLogName";
});

in ConfigureServices() after you call .UseWindowsService().

Was this page helpful?
0 / 5 - 0 ratings