.Net Core Service writing to wrong event log
Asked Answered
S

1

1

I've got a .net 3.1 core service and trying to write to a specific log file. Instead of going to new log, the loginformations are attempting to write to the application log. Getting: "The process was terminated due to an unhandled exception. Exception Info: System.AggregateException: An error occurred while writing to logger(s). (The source is not registered in log 'Application'. (It is registered in log .) "

The code to config in the createhostbuilder is

    Host.CreateDefaultBuilder(args)
        .UseWindowsService()
        .ConfigureLogging((context, logging) =>
        {
            logging.AddEventLog(new EventLogSettings()
            {
                SourceName = <my service name>,
                LogName = <custom log name>,
                Filter = (x, y) => y >= LogLevel.Information
            });
            logging.AddConsole();
        })

        .ConfigureServices((hostContext, services) =>
        {
            services.AddHostedService<Worker>();
        });

Using ilogger and it's just a simple _logger.LogInformation("my message");

Sisely answered 3/2, 2020 at 18:1 Comment(1)
Definitely appreciate feedback on this one!Sisely
F
1

"trying to write to a specific log file"

No, you're definitely not - the code you have shown is trying to write to the Windows Event Log using Microsoft.Extensions.Logging.EventLog, writing to a source <my service name>.

Assuming that's what you want to do, rather than log to a file, the error message is explicit - the Event Log source you are specifying does not yet exist.

You have 2 options:

  1. Create the Event Log source first. This requires admin permissions, and is typically done in the application installer (you can use System.Diagnostics.EventLog.CreateEventSource)
  2. Don't specify a custom source and log name, using the defaults instead - that will mean your logs get written to the Application log, using the ".NET Runtime source (which AFAIK is created when you install .NET`
Fusible answered 3/2, 2020 at 23:7 Comment(7)
Thanks Cocowalla... that's the rub. I wanted them in the custom log I created. Opened PS in admin and ran New-EventLog -LogName <customeventlog> -Source <service name in services> WIthout the specifying my log in the setup I log fine to the Application Log, I really want to log to my customeventlogSisely
OK, so it works as expected if you create the source first?Fusible
no. when I default to the appl log.. it does write there. However, I want to write to a specific event log that I created (above). By the error message it appears that the addeventlog() and log creation are working, but the ILogger is not using that specific log (I think...)Sisely
Ah, I don't think New-EventLog lets you create custom logs - only custom sources within one of the standard logs (Application, System...)Fusible
Instead, you could use eventcreate2 to create the custom logFusible
I'll give that a try... much appreciated!Sisely
np, please do report back with the result!Fusible

© 2022 - 2024 — McMap. All rights reserved.