Override Map not applying correctly
See original GitHub issueHi have 2 appsettings in my ASP.NET Core project. Default is Production and override is Development. I have the following code in my configs. In appsettings.json,
"Serilog": {
"Using": [ "Serilog.Sinks.Async", "Serilog.Sinks.RollingFile" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Error"
}
},
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "C:/Logfiles/project/log-{Date}.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"fileSizeLimitBytes": 2147483648,
"retainedFileCountLimit": 5
}
}
],
"Properties": {
"Application": "ServiceMyCar.Web.Api"
}
}
And the overriding appsettings.development.json,
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information",
"System": "Information"
}
}
}
So from my understanding, in Development environment, the MinimumLevel should be Debug. In Production environment, the MinimumLevel should be Information. However, for some reason the overriding configuration is not applying. I have attached the output.
As you can see in the image, HasOverrideMap is true (marked by blue). The LevelSwitch (marked by green) is showing MinimumLevel of the appsettings.json value. And the configuration from appsettings.development.json is loaded under the OverrideMap values. So the configuration has been loaded into the Logger. But in the IsEnabled check, the level for Debug is false. Which means the OverrideMap is not applying. And that’s why I am not seeing the Debug messages from my application.
Can someone help me figure out why this is happening and how I can resolve this problem? I am using latest Serilog versions and using ASP.NET Core 2.2.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Just to be sure there is no misunderstanding, let me explain what you are doing, and then you can confirm that it is indeed what you are trying to achieve 😃
When you do
Log.ForContext<HttpContext>
, this is pretty much equivalent toLog.ForContext("SourceContext", "Microsoft.AspNetCore.Http.HttpContext")
, i.e. you are adding a property namedSourceContext
whose value is the full namespaced-name of the type you pass to the type parameterT
in.ForContext<T>
. At first glance, this seems a bit strange, as you would normally use.ForContext<T>
whereT
is a type you own. This is typically used to be able to track down which class wrote which log events.It is the property
SourceContext
that is taken into account also for “Level Overrides”. As far as I understand, you are therefore writing some of your logs withSourceContext = "Microsoft.AspNetCore.Http.HttpContext"
, but you have defined some Level Overrides that state that forSourceContext
starting withMicrosoft.*
, only write logs when the event level is> Information
…Would this explain what you are seeing ?
If what you are trying to do is attach some properties of the
HttpContext
to the events you are logging, the expected approach would be to use the form you described later in you description :Serilog.Log.ForContext("RequestHost", context?.Request?.Host)
.If you really want to attach all the properties of
HttpContext
, then you should be using :but this is really not recommended 😛
@skomis-mm yeah it didn’t seem to be supported, that’s a shame. I will do this then thank you 👍 @tsimbalar yeah that’s my fault for not posting an accurate example. This is not my exact setup but I was trying to give an example of adding an entry. Whoops 🤦♂️