ASP.Net Core 1 Logging Error - The description for Event ID xxxx from source Application cannot be found
Asked Answered
B

1

31

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:

Bradski answered 24/8, 2017 at 12:25 Comment(4)
I was about to suggest you need to create the source which always used to fix this but see you tried that, I have used Serilog with dotnet core to write to the event log (not upgraded to 2.0 yet) so you could try it if all else fails.Phlegethon
EventViewer looks in binary files with compiled Windows MESSAGETABLE resources in them for the dictionary of id/strings. How are you creating that binary file?Bedbug
Hi @PeterRitchie, whilst this is a very old topic (over 5 years), I'm still failing to understand your question .This was an ASP.Net Core application. It's compiled code. Does that answer you?Bradski
Does your application have enough permissions to create the event source? Have you tried running as admin? (ouch, I see it's an old question, but still, my comment came from very old experience so it may apply ;-) )Desiree
L
0

I have not been able to use the loggerFactory.AddEventLog() in .Net Core 2.0. As the Microsoft.Extensions.Logger.EventLog only lives in .net 4.5.1 although the Nuget package is available for 2.0.0 here , so looks like it needs to be ported to Core.

enter image description here

but maybe this will work for you. Add your CustomEventIds

 public static class CustomEventIds
{
    public const int Debug = 100;
    public const int Information = 101;
    public const int Warning = 102;
    public const int Error = 103;
    public const int Critical = 104;
}

and:

_logger.LogInformation(CustomEventIds.Debug, "Starting to do something with input: {0}", "test");
Lorenzetti answered 30/8, 2017 at 15:59 Comment(4)
Thanks for your answer, but I do already specify an Event Id.You can refer to the code.Bradski
If the OP was able to install the NuGet package, then it is available. NuGet doesn't let you install packages not available to your platform.Ambient
Here the link that says it is not available in core. You may consider calling an assembly at runtime that can do EventLogging for you see hereLorenzetti
Generally, you would not want you API to create Windows event log messages as a caller could force enough event log messages to be created to cause the Windows server machine to run exceptionally slow. Also, as others have pointed out, mix .NET core/.net 5 libraries with .net framework 4.7x/net standard libraries can cause problems.Hage

© 2022 - 2024 — McMap. All rights reserved.