10

I have a project using .NET Framework ASP.NET Core 2.0, and want to implement logging to windows event log, like i read here

Add log providers

public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureLogging((hostingContext, logging) => { logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); logging.AddEventSourceLogger(); logging.AddConsole(); }) .UseStartup<Startup>() .Build(); } 

Controller

[Route("api/[controller]")] public class ShortCodeController : Controller { private readonly ILogger _logger; public ShortCodeController(ILogger<ShortCodeController> logger) { _logger = logger; _logger.LogInformation("INIT"); } [HttpGet("{letters}/{digits}/{length}")] public string Get(bool letters, bool digits, int length) { _logger.LogError("TEST"); return "value"; } } 

And it works for console, I see my log messages. But i can't find that messages in event log using event viewer. Why?

2

3 Answers 3

8
logging.AddEventSourceLogger() 

is for Event Tracing.

For the Event Log, you want to use:

logging.AddEventLog() 
Sign up to request clarification or add additional context in comments.

9 Comments

Its kind of related to this specific question and answer. Anyway, I've ran this from Package Manager Console "Install-Package Microsoft.Extensions.Logging.EventLog -Version 2.0.0" and i can see it now.
The learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/… link in my answer has a link to the NuGet package needed.
For AddEventSourceLogger, install nuget package Microsoft.Extensions.Logging.EventSource.
@thepirat000 The context of this question is explicitly not wanting AddEventSourceLogger so I am confused why you may have added that comment?
Updated link for the necessary NuGet package: learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/…
|
4

Install Microsoft.Extensions.Logging.EventLog from Nuget.

Include the Microsoft.Extensions.Logging.EventLog on the program.cs file

Then logging.AddEventLog() will be possible and consequently you will be able to achieve your goal

1 Comment

I still can't find any log message in EventLog Viewer. Do you know what's the default EventSource name?
3

"But i can't find that messages in event log using event viewer. Why?"

Unless you use a LogName and SourceName that your system recognizes, the event log will give you a meaningless error message.

This works for me against net5.0:

 public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => { logging.ClearProviders(); logging.AddEventLog(new EventLogSettings() { SourceName = ".NET Runtime", LogName = "Application", }); } ) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); 

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.