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"
}
}
As you suggested in your configuration as well. It is possible to do so. Below is the sample configuration.
MinimumLevel config section dictates what should be default and overrides for specific namespaces.
As side note I registered my Serilog as
And formatter part is Trivial in context of the question but if you want to use it I refered Serilog.Sinks.ElasticSearch as extra package to use it.