Skip to content

EventLogger ignores provided EventLogSettings.SourceName #91418

Open
@GenetecMan

Description

@GenetecMan

Description

The Microsoft.Extensions.Logging.AddEventLog(this ILoggingBuilder builder, EventLogSettings settings) extension method allows one to redefine various pamaterers, such as SourceName. However, redefining this does nothing except stop the proper logging of information into the Windows Event Viewer.

Reproduction Steps

There is an example in this repository.

Using the default Worker Service C# template:
In appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "Microsoft.Hosting.Lifetime": "Debug"
    }
  }
}

In Program.cs

            .ConfigureLogging(logging =>
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    logging.AddEventLog(new Microsoft.Extensions.Logging.EventLog.EventLogSettings()
                    {
                        LogName = "Application",
                        SourceName = "Monitor"
                    });
                }
            })

In Worker.cs

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            // This logs correctly.
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var log = new EventLog("Application")
                {
                    Source = "Test123"
                };

                log.WriteEntry("Error 123");
            }

            // This line simply does not show up!
            _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

            // This line shows up in the event log under SourceName ".NET Runtime".
            // The same happens for LogError.
            _logger.LogWarning("Worker running at: {time}", DateTimeOffset.Now);

            await Task.Delay(1000, stoppingToken);
        }
    }

Check in event viewer, you should have something like this:
image

Expected behavior

  1. The LogInformation line should be displayed.
  2. The LogWarning/LogError lines should have the correct Source Name.

Actual behavior

  1. The LogInformation line is not displayed (so most likely not logged).
  2. The LogWarning/LogError lines do not have the correct Source Name.

Regression?

No response

Known Workarounds

No response

Configuration

  • .NET 7
  • Microsoft.Extensions.Logging
  • Windows 10 Entreprise, x64
  • Most likely not specific to this configuration

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions