Tried this and it’s only capturing logs with same project. If we have a class library, how can we capture the logs?
@user-rp9iis1en6h Жыл бұрын
How to remove some property from the log, lets say when there is a error, serilog writes everything to the file. I want to configure serilog so that it does not write sourcecontext property to the file. And I want to add my own property with custom values. How to achieve it?
@hwd19786 ай бұрын
Thank you!
@TheoTiger792 жыл бұрын
Thank's a lot for this video. It is really helpful. I have also a question: I have a ASP .Net Core application and want to use Serilog with File Sink. My application has another project next to the main application project which has a Program.cs and a Startup.cs. I want to use the configured Serilog logger from the main project also in the other (regular) C# library project (i.e. log events from several projects should go into one log file). Is there a best practice on how to pass this logger to classes in different projects and use it there?
@Codewrinkles2 жыл бұрын
The best way to achieve this is trough dependency injection. This is a very theoretical answer, but you can register the classes where you want to log in the DI container and then inject ILogger via the constructor. But depending on the exact scenario, there are several other options: use the decorator pattern, pass the ILogger when you instantiate new objects of your classes in the other project and so on.
@TheoTiger792 жыл бұрын
@@Codewrinkles Thanks a lot for your answer! Today I tried the "dependency injection"-way of doing it and it works really well. I inject using "ILoggerFactory" and I pass this to some class from other projects. In the other projects I don't have any Serilog dependencies, just using Microsoft.Extensions.Logging. Like this (I hope) I can use a different logging provider more easy in the future. Like this: "ILogger logger = logFactory.CreateLogger();" - which is called from a non-main project using the logFactory passed from the class defined in the main application project via DI - I can also see the T-specific Serilog-SourceContext in the log file.