serilog-sinks-eventlog icon indicating copy to clipboard operation
serilog-sinks-eventlog copied to clipboard

Serilog adding EventLog as Sink

Open ckvckv opened this issue 5 years ago • 1 comments

We are usingSerilog and we are adding EventLog as Sink from configuration. This is how our code looks.

public static ILoggingBuilder AddSerilog(this ILoggingBuilder logging, IConfiguration configuration)
        {
            if (logging == null)
            {
                throw new ArgumentNullException(nameof(logging));
            }

            var log = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                // .WriteTo.EventLog("Exception Definition", manageEventSource: true)
                .CreateLogger();

            logging.AddSerilog(log, dispose: true);
            return logging;
        }

And this our config file.

<add key="Serilog:Using:0" value="Serilog.Sinks.EventLog" />
    <add key="Serilog:MinimumLevel" value="Verbose" />

For some reason the log doesnt get set with the EventLog sink. However If I change the config file Serilog:using:0 key value to Serilog.Sinks.Console or Serilog.Sinks.RollingFile the log gets created properly.

Also If I change code to directly invoke WriteTo.EventLog It works. Can someone please tell what I am doing wrong.

ckvckv avatar Mar 03 '20 08:03 ckvckv

    <add key="serilog:using:Console" value="Serilog.Sinks.Console"/>
    <add key="serilog:using:EventLog" value="Serilog.Sinks.EventLog"/>
    <add key="serilog:minimum-level" value="Debug"/>
    <add key="serilog:minimum-level:override:Microsoft" value="Warning"/>
    <add key="serilog:write-to:Console"/>
    <add key="serilog:write-to:EventLog.source" value="MySource"/>
    <add key="serilog:write-to:EventLog.logName" value="MyName"/>
    <add key="serilog:write-to:EventLog.manageEventSource" value="true"/>

The above works for me.

DiskCrasher avatar Aug 21 '20 01:08 DiskCrasher