I would like to write to the Windows Event Log from an ASP.Net Core application's Controller method.
The issue I have is that, where I expect log information to be written I keep getting the error/information log:
The description for Event ID xxxx from source Application cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
If the event originated on another computer, the display information had to be saved with the event.
The following information was included with the event:
My.Fully.Qualified.Namespace.WebApi.Controllers.MyController Error occured in method 'MyMethod'
the message resource is present but the message is not found in the string/message table
As a quick note, prior to .Net Core, I had always resolved a similar error by creating the Event Source using Powershell or Command or properly configuring Microsoft.Practices.EnterpriseLibrary.Logging Application block or another third-party library
Approach I used was:
After installing the Nuget Packages: Microsoft.Extensions.Logging.Abstractions and Microsoft.Extensions.Logging, I specify the EventLog Provider in the Start Configure block of code.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory
.AddDebug()
.AddEventLog();
//... rest of the code here
}
For each Controller, use the ILogger<T>
approach:
public class MyController : Controller
{
private readonly IMyRepository _myRepository;
private readonly ILogger _logger;
public MyController(IMyRepository myRepository,
ILogger<MyController> logger)
{
_myRepository = myRepository;
_logger = logger;
}
public bool MyMethod(MyRequestObject request)
{
try
{
var response = privateDoSomethingMethod(request);
return response.Success;
}
catch (Exception ex)
{
_logger.LogError(6666,ex, "Error occured when doing stuff.");
return false;
}
}
This is based on the official documentation on Logging
What I have read and tried:
- Description for event id from source cannot be found
- IIS error -2147024809 when trying to run .NET CORE application
- When launching an ASP.NET 5 MVC 6 Web App in IIS 8.5 I get an HTTP500 and an Event 1001 against the HttpPlatformHandler
- ILogger Interface
- ASP.NET Core Logging Tutorial – What Still Works and What Changed?