0
votes

Goal:

To create my own custom log under Applications and Services Logs under the Event Viewer:

Code snippet:

.Net CORE 3.1

Host.CreateDefaultBuilder(args)
            .ConfigureLogging((hostContext, logging) =>
            {
              logging.ClearProviders();
              logging.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
              logging.AddEventLog(
               eventLogSettings =>
                {
                  eventLogSettings.LogName = "My Log";
                  eventLogSettings.SourceName = "Dummy";
                });
              logging.AddConsole();
            });
// somewhere in my code, I use ILogger to write to the log

Configuration

    "Logging": {
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Grpc": "Information",
        "Microsoft": "Information"
     }
   }

Problem:

While testin my developer machine, I see the log entry on the console, but I don't see the log in the Event Viewer. If I dropped the LogName, the log shows up in the Application Log on the Event Viewer. What am I missing?

Thanks!

3

3 Answers

1
votes

I picked up this in the Application Log:

The description for Event ID 0 from source Application cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer. If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: Unable to log .NET application events. The source was not found, but some or all event logs could not be searched. To create the source, you need permission to read all event logs to make sure that the new source name is unique. Inaccessible logs: Security. the message resource is present but the message is not found in the string/message table

Resolution:

Close the VS and re-open it "as Administrator"

0
votes

Another way to quickly add a new application as an event source, if you are getting "Unable to log .NET application events", is just to run the following PowerShell cmdlet with elevated permissions.

New-EventLog -Source NAME_OF_APPLICATION -LogName NAME_OF_LOG

With NAME_OF_APPLICATION being the name of your application that you want to be a data source, and NAME_OF_LOG being the name of the log you want your events to appear in. If NAME_OF_LOG is an existing log it'll just add your application as an event source to that log, if it's a new log it'll create the log the first time you try to write an event to it.

0
votes

I have a dotnet app created with "worker" template using .NET Core 3.1 (3.1.404 SDK)

This way to add Event Logs does not work (see below)

hostBuilder.ConfigureLogging(logging =>
    logging.AddEventLog(new EventLogSettings()
    {
        SourceName = "MySourceName",
        LogName = "MyLogName"
    }));

While this one works:

hostBuilder.ConfigureServices((hostContext, services) => //DI
            {
                ConfigureTheServices(hostContext, services);
                //This way to add Event Logs does work - (first create the SourceName inside LogName for WindowsEventLog using "admin" credentials
                services.Configure<EventLogSettings>(settings =>
                {
                    settings.SourceName = "MySourceName";
                    settings.LogName = "MyLogName";
                });
            });