ASP.NET 6 Core API does not write to Windows EventLog

AG 456 Reputation points
2022-10-30T08:02:40.993+00:00

Hi,

I have started a new ASP.NET 6 Core Web API with latest NuGet packages updated.
Repo is here LogTest

Add the necessary code according to this doc logging

At appsettings.json

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

At Program.cs

var builder = WebApplication.CreateBuilder(args);  
builder.Logging.ClearProviders();  
builder.Logging.AddConsole();  
  
builder.Logging.AddEventLog(eventLogSettings =>  
{  
    eventLogSettings.LogName = "LogTest";  
    eventLogSettings.SourceName = "Test1";  
});  

At WeatherForecastController.cs I have added _logger.LogInformation("aaa"); at public IEnumerable<WeatherForecast> Get()

Register LogTest with PowerShell

New-EventLog -LogName LogTest -Source Test1  

Ruining the app but nothing is written to Windows EventLog at LogTest.

Any help please?

Regards,
AG

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
3,160 questions
{count} votes

4 answers

Sort by: Most helpful
  1. AG 456 Reputation points
    2022-11-01T16:36:02.69+00:00

    Hi @Zhi Lv - MSFT

    Thanks, it is working now but I have anther problem.
    For every call to the API for example public IEnumerable<WeatherForecast> Get() a lot is written to the EventLog not just _logger.LogInformation("aaa"); so I have tried to implement a filter like this eventLogSettings.Filter = (x, y) => y >= LogLevel.Error; so only the logs I am writing and also Error's from the API will be written to the EventLog but I have no luck of doing so as when applying this filter nothing is written to the EventLog.

    Thanks,
    AG

    0 comments No comments

  2. AG 456 Reputation points
    2022-11-02T09:04:07.877+00:00

    Hi @Zhi Lv - MSFT

    Sorry but it does not work.
    I am using IIS Express for Debug and also tried to change to Host and Debug but still many events are written to EventLog when app is starting.

    var builder = WebApplication.CreateBuilder(args);  
      
    builder.Logging.AddFilter((provider, category, logLevel) =>  
    {  
        if (provider.Contains("ConsoleLoggerProvider")  
            && category.Contains("Host")  
            && logLevel >= LogLevel.Debug)  
        {  
            return true;  
        }  
        else  
        {  
            return false;  
        }  
    });  
      
    builder.Logging.ClearProviders();  
    builder.Logging.AddConsole();  
      
    builder.Logging.AddEventLog(eventLogSettings =>  
    {  
        eventLogSettings.LogName = "LogTest";  
        eventLogSettings.SourceName = "Test1";  
    });  
    

    Regards,
    AG


  3. AG 456 Reputation points
    2022-11-03T10:32:35.053+00:00

    Hi @Zhi Lv - MSFT

    _logger.LogError() is working but I would like to be to write _logger.LogInformation() to EventLog so if I will put the filter on LogLevel.Information again I am receiving a lot of events when the app is starting and when calling public IEnumerable<WeatherForecast> Get()

    Regards,
    AG

    0 comments No comments

  4. AG 456 Reputation points
    2022-11-15T12:36:07.07+00:00

    Apparently, only thing needs to do is set "Microsoft": "Warning" at appsettings.json so it will looks like this

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

    Regards,
    AG

    0 comments No comments