Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core EventLog provider

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?

like image 878
Hagakurje Avatar asked Dec 12 '17 12:12

Hagakurje


People also ask

Which of the following logging providers are available in ASP.NET Core?

The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog (on Windows).

How do I check logs in NET Core?

NET Core agent logs information to the logs folder within C:\ProgramData\Contrast\dotnet-core\ on Windows or /var/tmp/contrast/dotnet-core/ on Linux, by default. Notes: Depending on the setup of the Windows profile and folder view settings, the ProgramData folder may be hidden.

What is a logging provider?

Logging providers persist logs, except for the Console provider, which only displays logs as standard output. For example, the Azure Application Insights provider stores logs in Azure Application Insights. Multiple providers can be enabled. The default .


1 Answers

logging.AddEventSourceLogger()

is for Event Tracing.

For the Event Log, you want to use:

logging.AddEventLog()
like image 99
mjwills Avatar answered Oct 21 '22 04:10

mjwills