1
votes

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

1
Initially in your appsettings.json all loglevels including default are commented. That is why you are not seeing the use of having appsettings.jsonBob Ash
Is it mandatory to have Default category in every provider? What happens if it's not given?Vijay
It's not mandatory. For a given Log Category if LogLevel is not mentioned in code/config, then it will look for LogLevel of Default Log Category in code/config. If that is also not available, then it takes Minimum Log Level setting as "Information".Bob Ash

1 Answers

1
votes

Environment specific logging happens.

In your example, both appsettings.Development.json and appsettings.Production.json defines LogLevel only for "Microsoft" category. But, the logging done from your code falls in a different Log Category whose LogLevel is not defined in code/config files. Hence, it takes default minimum log level as "Information" in both environments.

To see a difference, add "Default" key with different LogLevel settings in different environments as below:

appsettings.Development.json

{
  "Logging": {
    "LogLevel": {
     "Microsoft": "Information",
     "Default": "Error"
    }
  }
}

appSettings.Production.json

 {
  "Logging": {
    "Console": {
      "LogLevel": {
        "Microsoft": "Critical",
         "Default": "Critical"
      }
    }
  }
}