locked
EventLogSettings not functioning RRS feed

  • Question

  • User1560815697 posted

    Hi,

    I am trying to set EventLogSettings but the log continues to write to the default Windows Logs -> Application

    Using .net 3.1 to build Worker Service

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .UseWindowsService()
            .ConfigureLogging((context, logging) =>
            {
                logging.AddEventLog(new EventLogSettings()
                {
                    SourceName = "TestSource",
                    LogName = "TestLog"
                });
            })
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });

    I have also tried to add a log with powershell    New-EventLog -LogName TestLog -Source TestSource

    AG

    Monday, April 20, 2020 7:15 PM

All replies

  • User-474980206 posted

    did you give the app pool account write access to the new event log?

    Monday, April 20, 2020 8:15 PM
  • User1560815697 posted

    Hi,

    Sorry I did not understood...

    Do I first still need to add the LogName by power shell ?

    What is "App Pool"?

    How do I do that please?

    Tuesday, April 21, 2020 6:41 AM
  • User1560815697 posted

    Hi,

    I have managed to solve the problem...

    Step 1 - Adding EventLog with Power Shell that will appear at Windows Event Viewer under Applications and Services Logs tree

    New-EventLog -LogName TestLog -Source TestSource

    Step 2 - add EventLog section to appsettings.json so the overall json is

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        },
        "EventLog": {
          "LogLevel": {
            "Default": "Information",
            "Microsoft.Hosting.Lifetime": "Information"
          }
        }
      }
    }

    Step 3 - the overall code at Program.cs

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .UseWindowsService()
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.ClearProviders();
            logging.AddConfiguration(hostingContext.Configuration.GetSection("EventLog"));
            logging.AddEventLog(new EventLogSettings()
            {
                SourceName = "TestSource",
                LogName = "TestLog"
                //Filter = (x, y) => y >= LogLevel.Warning
            });
            logging.AddConsole();
        })
        .ConfigureServices((hostContext, services) =>
        {
        services.AddHostedService<Worker>();
        });

    Tuesday, April 21, 2020 11:37 AM