Stanislas
Stanislas

Reputation: 2020

EventLog Source name for .NET Core 3.1 Windows Service

Context

I have a .NET Core 3.1 Console Application that is being run as a Windows service. It's set up using:

Host.CreateDefaultBuilder(args)
    .UseWindowsService()

And installed as a Windows service using sc.exe create.

Question

Some users want to install multiple versions of this same console application on the same machine, which works fine! However, when viewing the logs in the Event Viewer, you can't tell which Windows service each record belongs to, as they all have the same source name.

Is there a standard way to set up a source name at installation time?

Research

// Inside .ConfigureServices((hostContext, services) =>
services.Configure<EventLogSettings>(settings =>
{
    settings.SourceName = "My chosen name";
});

Upvotes: 2

Views: 2870

Answers (2)

Bj&#246;rn Jarisch
Bj&#246;rn Jarisch

Reputation: 345

When the host builder registers the EventLog logging provider, it does not seem to pick up the "Logging:EventLog" configuration section, or at least not the SourceName.

Unfortunately, trying to add it again like this won't work because the underlying code uses services.TryAddEnumerable, and the EventLog is already registered:

// This does nothing!
logging.AddEventLog(hostContext.Configuration.GetSection("Logging:EventLog").Get<EventLogSettings>());

What does work, however, is simply re-configuring the existing logger using services.Configure:

// This works, inside .ConfigureServices((hostContext, services) =>
services.Configure<EventLogSettings>(hostContext.Configuration.GetSection("Logging:EventLog"));

Upvotes: 0

kogonidze
kogonidze

Reputation: 157

As I know classical way to solution this problem is to configure several files appsettings.json like this:

appsettings.First.json

{
  "Logging": {
    "EventLog": {
      "LogName": "Test app",
      "SourceName": "FirstInstance"
    }
  }
}

appsettings.Second.json

    {
      "Logging": {
        "EventLog": {
          "LogName": "Test app",
          "SourceName": "SecondInstance"
        }
      }
    }

Then to set it almost as you show it in your example but to reference to appsettings.json section:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .UseWindowsService()
            .ConfigureLogging((hostingContext, logging) =>
{
    logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
    logging.AddConsole();
    logging.AddEventLog(hostingContext.Configuration.GetSection("Logging:EventLog").Get<EventLogSettings>());
})
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });

And then you build your service with both configurations and install both instances. So, your logging records will be reсorded at the same event folder but will be differ in the value in the column "Source name".

Upvotes: 1

Related Questions