It's easy to use Serilog to log events to the Windows Event Log, however the same sink appears not to offer the ability to write a custom event ID with the log. So if we decide to use Event ID 4000 for our app, how do we configure Serilog to write that Event ID to the event log with the source, description etc?
Asked
Active
Viewed 2,440 times
1 Answers
0
To customize the EventID
, you have to implement your own IEventIdProvider
and tell the sink to use your provider instead of the default one.
.WriteTo(..., eventIdProvider: yourProvider)
public static LoggerConfiguration EventLog(
this LoggerSinkConfiguration loggerConfiguration,
string source,
string logName = null,
string machineName = ".",
bool manageEventSource = false,
string outputTemplate = DefaultOutputTemplate,
IFormatProvider formatProvider = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IEventIdProvider eventIdProvider = null) // <#<#<#<#<#<#<#<#<#<#<#<#

C. Augusto Proiete
- 24,684
- 2
- 63
- 91
-
Thanks for the quick reply. Where do you specify the Event number? i.e. 4000? – Marcus T-M Nov 01 '19 at 15:34
-
I confess I'm quite new to .NET, do you have an example I could follow? – Marcus T-M Nov 01 '19 at 15:54
-
@MarcusT-M I linked the implementation of the default event provider above: [EventIdHashProvider.cs](https://github.com/serilog/serilog-sinks-eventlog/blob/dev/src/Serilog.Sinks.EventLog/Sinks/EventLog/EventIdHashProvider.cs#L39)... Just replace the code in the `Compute` method. – C. Augusto Proiete Nov 01 '19 at 17:38