I would like to write to the Windows event log from the Application Application ASP.NET Core method.
The problem is that when I expect the log information to be written, I keep getting the error / information log:
Description for event ID xxxx from source Application could not be found. Either the component that raises this event is not installed on your local computer or the installation is damaged. You can install or repair the component on the local computer.
If the event occurred on another computer, the displayed information should have been saved with the event.
The following information was included in the event:
My.Fully.Qualified.Namespace.WebApi.Controllers.MyController Error in the method "MyMethod"
message resource present but message not found in string / message
Be that as it may, before .Net Core, I always resolved a similar error by creating an event source using Powershell or Command or setting up Microsoft.Practices.EnterpriseLibrary.Logging app block or another third-party library
The 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 code block.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory .AddDebug() .AddEventLog();
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 official logging documentation.
What I read and tried:
c # asp.net-core event-log
user919426
source share