I am working on .Net Core 2.2 logging mechanism. While I was experimenting the logging based on environment. So, I created two new appSettings.json files with respective environments along with appSettings.json present in the solution. One for Development, other for Production environment.
appsettings.json
{
"Logging": {
"LogLevel": {
//"Default": "Debug",
//"System": "Information",
//"Microsoft": "Error"
}
}
}
appsettings.Development.json
{
"Logging": {
"LogLevel": {
"Microsoft": "Information"
}
}
}
appSettings.Production.json
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical"
}
}
}
}
Changed the StartUp.cs file
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
var currentEnvironment = env.EnvironmentName;
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{currentEnvironment}.json", optional: true);
}
Logging Method
public void LogExceptionToConsole()
{
_logger.LogError("This is raised by error");
_logger.LogCritical("This is raised by critical ");
}
And launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:2131",
"sslPort": 44388
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"DemoLoggingApplication": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
I understand that environment specific appSettings would be taken the highest precedence. And I have two questions
1) Does environment specific logging happens? If yes, then what could be the changes in the above logic.
2) When I ran the application in Development mode on DemoLoggingApplication
profile. I can see all the Information,Error & Critical
logs.
But when I changed the ASPNETCORE_ENVIRONMENT
value from Development
to Production
for DemoLoggingApplication
profile and ran the application I could again see both the logs of type Error
& Critical
.
As, I've set that Console
provider should only display Critical
logs of type Microsoft
category .I've been displayed Errors
logs also.
Though I've read the Microsoft documents, I couldn't understand the prioritization. Could anyone explain me detail why I'm seeing both the logs. Am I missing any understanding. Please help me.
Thanks in advance
Updated the question after Bob's answer It worked after changing the appsettings.json, appsettings.Development.json & appSettings.Production.json
appSettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
}
}
appsettings.Development.json
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Information",
"Default": "Information" // newly
}
}
}
}
appSettings.Production.json
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical",
"Default" : "Critical" // newly added line
}
}
}
}
Now when I changed the environment to Development
, I could log from Information
but only after adding Default
category to both Development and Production.
I just wanted to know why is this behavior?
And what's the impact of having maintaining appsettings.json -> Logging
when we are having Development & Production settings.
Thanks
Default
category in every provider? What happens if it's not given? – Vijay