1

我正在研究.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值从更改DevelopmentProductionfor DemoLoggingApplicationprofile 并运行应用程序时,我可以再次看到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当我们进行开发和生产设置时,进行维护会产生什么影响。

谢谢

4

1 回答 1

1

发生特定于环境的日志记录。

在您的示例中, appsettings.Development.json 和 appsettings.Production.json 都仅为“Microsoft”类别定义了 LogLevel。但是,从您的代码完成的日志记录属于不同的日志类别,其 LogLevel 未在代码/配置文件中定义。因此,在两种环境中都将默认的最小日志级别作为“信息”。

要查看差异,请在不同环境中添加具有不同 LogLevel 设置的“Default”键,如下所示:

appsettings.Development.json

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

appSettings.Production.json

 {
  "Logging": {
    "Console": {
      "LogLevel": {
        "Microsoft": "Critical",
         "Default": "Critical"
      }
    }
  }
}
于 2020-02-04T11:19:57.623 回答