Serilog Event Sink Description for even XXX cannot be found 错误

Serilog Event Sink Description for even XXX cannot be found error

嗨,我正在使用 Serilog 事件接收器来写入事件日志。

我正在使用

手动创建事件源

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

在我的程序中,我使用

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

因为这是服务器上的一个 exe 和 运行s,所以我没有 运行 exe 作为管理员的管理员权限,以便拥有 manageEventSource: true。

这是问题所在:

我的事件已写入,但还附有此错误消息:

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.

我怎样才能让这条消息消失?我不能 运行 managedEventSource:true 因为我不能 运行 这个 exe 作为管理员。还有其他方法可以消除此错误吗?

有问题的事件 ID 超出了非托管 Serilog 事件源的范围。要消除此错误,您可以通过 eventIdProvider 公开此接收器的 ComputeEventID 方法;

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);
    }        
  }
}