Search code examples
c#serilogevent-viewer

Serilog Event Sink Description for even XXX cannot be found error


HI I am using the Serilog Event Sink to hopefully write to the event log.

I am creating my EventSource manually using

eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO MYEVENTSOURCE /D "MyApplication"

In my program I am initializing serilog using

    Logger = new LoggerConfiguration()
    .WriteTo.EventLog(new RenderedCompactJsonFormatter(null), "MyApplication",
    manageEventSource: false, 
    restrictedToMinimumLevel: LogEventLevel.Information)
    .CreateLogger();

Since this is an exe and runs on a server I do not have Admin permissions to run the exe as Admin in order to have manageEventSource: true.

This is the problem:

My Events are written but also have this error message attached:

The Description for Event ID 6800 from source MyApplication cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted.

How can I get this message to go away? I cannot run managedEventSource:true since I cant run this exe as Admin. Is there another way to make this error go away?


Solution

  • The Event ID in question is outside the range of the unmanaged Serilog Event Source. What you can do to get rid of this error is expose the ComputeEventID method for this sink through the eventIdProvider;

    Logger = new LoggerConfiguration()
        .WriteTo.EventLog(new RenderedCompactJsonFormatter(null), "MyApplication",
        manageEventSource: false, 
        ****eventIdProvider: new EventIdProvider(),****
        restrictedToMinimumLevel: LogEventLevel.Information)
        .CreateLogger();
    
    public class EventIdProvider : IEventIdProvider {
        public ushort ComputeEventId(LogEvent logEvent) {
            var val = (Serilog.Events.ScalarValue)logEvent.Properties.FirstOrDefault(f => f.Key == "eventId").Value;
            if (val?.Value != null && val.Value is ushort) 
            {
                return (ushort)val.Value;
            }
     else
       {
          //Return any random EventID within the range of the Source Table
          return (ushort)new Random().Next(500);
        }        
      }
    }