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

Share:
14,895
Hagakurje
Author by

Hagakurje

Updated on July 24, 2022

Comments

  • Hagakurje
    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
    JsonStatham about 6 years
    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.
  • mjwills
    mjwills about 6 years
    The docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/… link in my answer has a link to the NuGet package needed.
  • thepirat000
    thepirat000 almost 6 years
    For AddEventSourceLogger, install nuget package Microsoft.Extensions.Logging.EventSource.
  • mjwills
    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
    Derek Foulk over 5 years
    Updated link for the necessary NuGet package: docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/…
  • Will Huang
    Will Huang over 4 years
    I still can't find any log message in EventLog Viewer. Do you know what's the default EventSource name?
  • joym8
    joym8 about 4 years
  • joym8
    joym8 about 4 years
    Does the app running under IIS (unmanaged) need any special permission to add event log entry?
  • Admin
    Admin over 2 years
    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.