2

I am trying to implement serilog in a little sample-project. I´ve did the following so far:

  • Create new ASP.NET Core Web Application (MVC)
  • Install-Package Serilog.AspNetCore
  • Install-Package Serilog.Sinks.File
  • Install-Package Serilog.Formatting.Compact
  • Follow this blogpost
  • Try to follow this blogpost

My question:

Is there a possibility to configure two different loggers, based on CategoryName (in my case "MyLogger")? I want this logger to use a different file, than my default-logger. In my happy-world-fantasy it would look like:

        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            .Enrich.FromLogContext()
            .WriteTo.File(new CompactJsonFormatter(), @"C:\temp\standardLog.txt")
            .WriteTo.File(new CompactJsonFormatter(), @"C:\temp\specialLog.txt").Filter.ByIncludingOnly(x => x.SourceContext("MySpecialLogger")
            .CreateLogger();

And if I create a new Logger var logger = _loggerFactory.CreateLogger("MySpecialLogger"); the logs will be safed in my specialLog.txt-file.

Any ideas?

1 Answer 1

1

You need Serilog.Sinks.Map for this.

dotnet add <PROJECT> package Serilog.Sinks.Map -v 1.0.0-dev-00012

Then:

    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
        .Enrich.FromLogContext()
        .WriteTo.Map("SourceContext", null, (sc, wt) =>
            wt.File(new CompactJsonFormatter(), sc == "MySpecialLogger" ?
                @"C:\temp\specialLog.txt" :
                @"C:\temp\standardLog.txt"))
        .CreateLogger();
Sign up to request clarification or add additional context in comments.

3 Comments

First of all: Thanks for having time for my question!! Regarding your answer: The conditional operator fails (compiler-error only assignment, call (...) can be used as a statement).. This param is not valid: Action<string, LoggerSinkConfiguration> configure = (x, wt) => x == "" ? wt.File("") : wt.File("") ; - but I really like the Map-approach.. Any other idea?
Great! If I use RollingFile-Sink I have everything I wanted :) Thanks a lot!!! But there is still some unexpected behaviour: MySpecialLogger works as expected. But if I use the File-Sink, my StandardLogger only outputs "Starting Web Host". If I use RollingFile, i get 2 log files (standard-{date}.txt and standard-{date}_001.txt) where the 001-log contains my messages and the other only contains the startup-message. If I remove the Log.Information("Starting web host") -Call, everything works as expected...
Ah! Sorry, the answer's not 100% correct, I'll update it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.