我正在研究.Net Core 2.2 日志机制。虽然我正在尝试基于环境的日志记录。因此,我创建了两个具有各自环境的新 appSettings.json 文件以及解决方案中存在的 appSettings.json。一个用于开发,另一个用于生产环境。
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"
}
}
}
}
更改了 StartUp.cs 文件
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);
}
记录方法
public void LogExceptionToConsole()
{
_logger.LogError("This is raised by error");
_logger.LogCritical("This is raised by critical ");
}
和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"
}
}
}
}
我了解特定于环境的 appSettings 将具有最高优先级。我有两个问题
1) 是否发生特定于环境的日志记录?如果是,那么上述逻辑的变化可能是什么。
2)当我在DemoLoggingApplication
配置文件上以开发模式运行应用程序时。我可以看到所有的Information,Error & Critical
日志。
但是,当我将ASPNETCORE_ENVIRONMENT
值从更改Development
为Production
for DemoLoggingApplication
profile 并运行应用程序时,我可以再次看到Error
&类型的日志Critical
。因为,我已经设置该Console
提供者应该只显示Critical
类型为Microsoft
类别的日志。我Errors
也被显示了日志。
虽然我已经阅读了 Microsoft 文档,但我无法理解优先级。谁能详细解释一下为什么我会同时看到这两个日志。我是否缺少任何理解。请帮我。
提前致谢
在 Bob 的回答之后更新了问题 它在更改 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
}
}
}
}
现在,当我将环境更改为 时Development
,我可以登录,Information
但只有在将Default
类别添加到开发和生产之后。
我只是想知道为什么会出现这种行为?appsettings.json -> Logging
当我们进行开发和生产设置时,进行维护会产生什么影响。
谢谢