简体   繁体   中英

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM