I have ASP.NET Core application. I am using Microsoft.Extensions.Logging.ILogger
interface with Serilog
logging provider.
In production environment, the log level is set to Error
. So anything below Error
loglevel will not get logged.
Is there Force
logging available in Microsoft.Extensions.Logging
, which would log the message regardless of what the loglevel is set?
Basically I want to log some information (temporarily) without setting LogLevel to Information because that would log all other unrelevant information messages.
Update 1
So i updated my appsettings.json as per the suggestion however that did not work. Note that serilog also has its own loglevel which i have commented out. But Still not working.
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Error",
"MyUserService": "Information"
}
},
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
//"MinimumLevel": "Error",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
//"restrictedToMinimumLevel": "Error",
"connectionString": "Data Source=.\\SQLExpress;Initial Catalog=MyDataBase;Integrated Security=True;MultipleActiveResultSets=True",
"tableName": "Logs"
}
}
],
"Properties": {
"Application": "MyApplication"
}
}
and then in MyUserService
public class MyUserService : BaseService, IMyUserService
{
private readonly ILogger<MyUserService> _logger;
public MyUserService(MyDBContext dbContext, ILogger<MyUserService> logger)
: base(dbContext)
{
_logger = logger;
}
public async Task DoSomethig()
{
_logger.LogInformation("Log something meaningful here");
}
}
and in program.cs
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseApplicationInsights()
.UseUrls("http://*:40006")
.ConfigureAppConfiguration((hostingContext, config) =>
{
//removed for brevity
})
.ConfigureLogging((hostingContext, logging) =>
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(hostingContext.Configuration)
.CreateLogger();
logging.AddSerilog();
})
.Build();
The idea is to configure the log level per log cagtegory.
Once you let yourself inject an
ILogger<MyCategory>
(whereMyCategory
can be any type - a special type just to indicate log categories or just the class you log from), you can configure the log level independent of everything else: