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");

David Specht
  • 7,784
  • 1
  • 22
  • 30

1 Answers1

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`
Cocowalla
  • 13,822
  • 6
  • 66
  • 112
  • Thanks Cocowalla... that's the rub. I wanted them in the custom log I created. Opened PS in admin and ran New-EventLog -LogName -Source WIthout the specifying my log in the setup I log fine to the Application Log, I really want to log to my customeventlog – user3590468 Feb 04 '20 at 01:55
  • OK, so it works as expected if you create the source first? – Cocowalla Feb 04 '20 at 09:47
  • 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...) – user3590468 Feb 04 '20 at 11:51
  • 1
    Ah, I don't think `New-EventLog` lets you create custom logs - only custom *sources* within one of the standard logs (Application, System...) – Cocowalla Feb 04 '20 at 19:42
  • Instead, you could use [eventcreate2](https://github.com/nbolton/eventcreate2) to create the custom log – Cocowalla Feb 04 '20 at 19:44
  • I'll give that a try... much appreciated! – user3590468 Feb 04 '20 at 22:48
  • np, please do report back with the result! – Cocowalla Feb 04 '20 at 22:50