We are writing a new logging functionality and injecting into our existing services.
Following is the way we do it:
1) Setup Serilog using UseSerilog() call in Program.cs
2) Setup Serilog config in appsettings.json to setup sinks for writing to Log file.
3) Wrote a Facade Custom Logger Class which does logging internally using Serilog global Log instance.
public class MyLogger<T> : IMyLogger<T>
public MyLogger()
{
_logger = Log.ForContext(typeof(T));
}
public void LogInformation(Exception ex)
{
_logger.Information(ex.Message);
}
4) Inject IMyLogger into all my existing services via DI
5) Register all services (Logger, Project related services) in ConfigureServices()
Everything works. But I want to set different log levels for different namespaces in my Application so that in Production I can simple set in config only 4-5 important Core Namespaces whose Logging Method calls (MyLogger.LogInformation()) will be sent to Log file.
Is something like below possible? Because MyLogger is the class thats logging. But If the call is from MyProject.CartManager Namespace, then it should automatically Log based on the config below in Example.
Please share your thoughts.
eg:
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Information",
"MyProject.Inventory": "Information"
"MyProject.CartManager": "Error"
}
}
Your
IMyLoggerseems to be excessive. Microsoft offersMicrosoft.Extensions.Loggingfor the same purpose. So you can inject justILogger<MyProject.Inventory.Foo>into your class and Serilog handles that with theSerilog.Extensions.Loggingpackage. Beyond that your example must work as it is, meeting your expectations. Probably for your particular injections you used a wrongT(the implementation has no check over that).Just started googling for almost the same: using
MinimumLeveloverrides for a few dedicated namespaces or contexts and first found this question with no answer, but the next was this great article.The essentials: