TotPeRo
TotPeRo

Reputation: 6781

Serilog LoggerConfiguration write to different Sink by context

I have web asp.net MVC application using IdentityServer3. I want to split logging to different Sink. I want to write IdentityServer3 logging to Trace, EventLog("IdentityServer3") and SerilogWeb.Classic.Enrichers to database MSSqlServer.

My configuration is:

Log.Logger = new LoggerConfiguration()
                .WriteTo.Trace()
                .WriteTo.EventLog("IdentityServer3")
                .Enrich.FromLogContext()
                //all after this i want to write in database
                .WriteTo.MSSqlServer("connectionString", "Log")
                .Enrich.With(new HttpRequestIdEnricher())
                .Enrich.With(new HttpRequestRawUrlEnricher())
                .Enrich.With(new HttpRequestUserAgentEnricher())
                .Enrich.With(new UserNameEnricher())
                .CreateLogger();

Upvotes: 2

Views: 2360

Answers (1)

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31767

To write a subset of events to a sink, use WriteTo.Logger() and the Filter directive. Each case will be similar; here's the Identity Server one:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Logger(lc => lc
        .Filter.ByIncludingOnly(Matching.FromSource("IdentityServer3"))
        .WriteTo.Trace()
        .WriteTo.EventLog("IdentityServer3"))
    // <snip>

You can apply enrichers either at the outermost level, so that they enrich all events, or on one of the nested logger configurations.

Upvotes: 3

Related Questions