ASP.NET Core EventLog provider
14,895
Solution 1
logging.AddEventSourceLogger()
is for Event Tracing.
For the Event Log, you want to use:
logging.AddEventLog()
Solution 2
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
Author by
Hagakurje
Updated on July 24, 2022Comments
-
Hagakurje almost 2 years
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?
-
JsonStatham about 6 yearsIts 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.
-
mjwills about 6 yearsThe docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/… link in my answer has a link to the NuGet package needed.
-
thepirat000 almost 6 yearsFor
AddEventSourceLogger
, install nuget packageMicrosoft.Extensions.Logging.EventSource
. -
mjwills almost 6 years@thepirat000 The context of this question is explicitly not wanting
AddEventSourceLogger
so I am confused why you may have added that comment? -
Derek Foulk over 5 yearsUpdated link for the necessary NuGet package: docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/…
-
Will Huang over 4 yearsI still can't find any log message in EventLog Viewer. Do you know what's the default EventSource name?
-
joym8 about 4 yearslink for dotnet core 3.1 docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/…
-
joym8 about 4 yearsDoes the app running under IIS (unmanaged) need any special permission to add event log entry?
-
Admin over 2 yearsAs 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.