3

I want to log Information - Fatal with Serilog in my ASP.NET Core Web Application, using .NET Core 3.0

I want to log this data in different files (which was already asked and answered but my case is a little different I think):

  • information.log => information about what the application did (only using the automatic logging of Serilog / Microsoft)
  • error.log => All errors / unhandled exceptions that happen
  • other_information.log => (don't really have a name for this one) information about the program BUT only log messages created by me

I just can't figure out how to distinguish between information logs from Microsoft and my own.

I have found a question / answer on how to disable logging from Serilog / Microsoft but doing so it deactivates it for all LogWriters, How to turn off the logging done by the ASP.NET core framework .

Then I found an answer to another question which maybe is exactly what I'm looking for, but I don't understand what happens so I prefer to not use it, because I don't like magic because if there is a problem, fixing it is a lot more time-consuming, Can I log to separate files using Serilog?.

I also found another question that looks similar: Serilog : Log to different files but I don't understand this one either.

So if one of those really is the solution to my Problem I would be grateful if someone could explain it to me.

This is the Logger Configuration that I have until now, all it does is log all Information in one file and all errors in the other one.

            using Serilog;
            using Serilog.Events;            

            ...

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .WriteTo.Console()
                .WriteTo.Logger(l => l
                    .Filter.ByIncludingOnly(e => e
                    .Level == LogEventLevel.Information)
                    .WriteTo.File(@"Logs\Info.log"))
                .WriteTo.Logger(l => l
                    .Filter.ByIncludingOnly(e => e
                    .Level == LogEventLevel.Error)
                    .WriteTo.File(@"Logs\Error.log"))
                .CreateLogger();

And as I said, this works just fine to log all the information in one file and all the Errors in the other, but I don't know how to continue.

EDIT: I found the configurations I have at the moment from an answer to this question: Serilog - multiple log files‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌

John
  • 52
  • 8
NotNemo
  • 70
  • 1
  • 9
  • 1
    See also https://github.com/serilog/serilog-sinks-map, which is explicitly designed around this use case. HTH! – Nicholas Blumhardt Dec 06 '19 at 00:29
  • 1
    @NicholasBlumhardt I'll look into it, thanks a lot for your help! (I see you on pretty much every Serilog forum I get to, so I have to thank you for that as well.) – NotNemo Dec 06 '19 at 08:11

1 Answers1

1

I believe you just need to filter your log based on the property SourceContext, which is explained here: Filter Serilog logs to different sinks depending on context source?

Daboul
  • 2,635
  • 1
  • 16
  • 29
  • Thank you for your answer, this looks exactly like what I was searching for, but I somehow can't get it to work. I tried with adding the filter as a configuration of the `Logs/info.log` Configurations => `.Filter.ByExcluding(Matching.FromSource())` or with adding a configuration to the `other_information.log` => `.Filter.ByIncludingOnly(Matching.FromSource())`. What happens is that the `other_information.log` is created but left empty and my log message =>(Log.Information(`"This is a Information");`) is saved in the `info.log` file. do you see the problem? – NotNemo Dec 05 '19 at 13:24
  • can you try adding the SourceContext to your log template to actually see what is the source of that log and understand why it is log in that file? Something like https://stackoverflow.com/questions/48470090/serilog-format-sourcecontext-for-showing-only-assembly-name – Daboul Dec 05 '19 at 13:33
  • I did add the SourceContext to the template, just as you said and I know now where the problem comes from but I don't know why... so for the standard Microsoft Logs it sais either `Microsoft.AspNetCore...` or `Microsoft.Hosting...` but for my log the SourceContext is just empty `[INF] [] This is a Information`, the second field should be the SourceContext, jsut like here: `[INF] [Microsoft.Hosting.Lifetime] Application started.` – NotNemo Dec 05 '19 at 14:11
  • Yeah, I've seen something like that I think. If you add a log in a Controller you will see the SourceContext properly filled, but if you add a log in the Program.cs/Main method it won't. Have you try with a log in a Controller? A possible solution would be to dump everything that is Microsoft.* in one file, and all the other in another. Don't know if that would work for you. – Daboul Dec 05 '19 at 14:15
  • That would explain my Problem, I haven't tried with a Controller yet. Do you have a good source on how to create / implement one with good explanations? the ones I found weren't explained that well – NotNemo Dec 05 '19 at 14:52
  • Not really, but default microsoft template are usually a good start: https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-new?tabs=netcore22 For instance, "dotnet new mvc". – Daboul Dec 05 '19 at 14:57