Write this serilog filter in config file

148 views Asked by At

Usually, I set the configuration of .NET applications inside the config file (appsettings.json or *.config file, it depends if it's .NET Core or .NET Full Framework).

Now for the first time, I had to use a filter, and I'm not able to write such logic inside the config file.

Here's the logic I wish to bring to the config file

var logConfiguration = new LoggerConfiguration()
    //.ReadFrom.AppSettings()
    .WriteTo.Logger(lc => lc.WriteTo.EventLog(environmentSettings.ServiceNameInstanceForMonitor, manageEventSource: true)
        .Filter.ByIncludingOnly(pred =>
        {
            if (!pred.Properties.ContainsKey("Destination")) return false;
            return pred.Properties["Destination"].ToString().Contains("EventLog");
        }))
    .Enrich.WithExceptionDetails()
    .Enrich.WithAssemblyVersion(true)
    .WriteTo.Console()
    .WriteTo.Logger(lc2 => lc2.WriteTo
        .File($"{AppDomain.CurrentDomain.BaseDirectory}\\logs\\{environmentSettings.ServiceNameInstanceForMonitor}-.log",
            rollingInterval: RollingInterval.Day)
        .Filter.ByExcluding((pred =>
        {
            var res = pred.Properties.ContainsKey("Destination") &&
                      pred.Properties["Destination"].ToString().Contains("EventLog");

            return res;
        })));

Is it possible?

0

There are 0 answers