Using Microsoft.Extensions.Logging can you filter for a single EventID?

Viewed 1176

I'd like to produce a log file that contains only log output from a particular EventId (or EventIds). Is such functionality supported?

2 Answers

If you want to config Serilog using the appsettings file you can use something like this:

"Serilog": {
    "Using": ["Serilog.Sinks.File", "Serilog.Settings.Configuration", "Serilog.Expressions"],
    "MinimumLevel": {
        "Default": "Debug"
    },
    "WriteTo": [{
            "Name": "File",
            "Args": {
                "path": "log.txt",
                "rollingInterval": "Day",
                "retainedFileCountLimit": "120",
                "rollOnFileSizeLimit": "true",
                "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
            }
        }
    ],
    "Filter": [{
            "Name": "ByIncludingOnly",
            "Args": {
                "expression": "EventId.Id = 101"
            }
        }
    ]
}

In the using field you have to put all Serilog package that you are importing, then config a sink for writing the logs and finally define a filter.

I almost spent a whole morning trying to achive that so I hope it helps you

Related