Asp.net core 基于.NetCore 2.2中使用iLogger提供程序的环境的日志记录
我正在研究.NETCore2.2日志机制。 当我在试验基于环境的日志记录时。 因此,我创建了两个新的appSettings.json文件,其中包含相应的环境以及解决方案中的appSettings.json。一个用于开发,另一个用于生产环境Asp.net core 基于.NetCore 2.2中使用iLogger提供程序的环境的日志记录,asp.net-core,asp.net-core-mvc,asp.net-core-2.1,ilogger,Asp.net Core,Asp.net Core Mvc,Asp.net Core 2.1,Ilogger,我正在研究.NETCore2.2日志机制。 当我在试验基于环境的日志记录时。 因此,我创建了两个新的appSettings.json文件,其中包含相应的环境以及解决方案中的appSettings.json。一个用于开发,另一个用于生产环境 appsettings.json { "Logging": { "LogLevel": { //"Default": "Debug", //"System": "Information", //"Microsof
appsettings.json
{
"Logging": {
"LogLevel": {
//"Default": "Debug",
//"System": "Information",
//"Microsoft": "Error"
}
}
}
appsettings.Development.json
{
"Logging": {
"LogLevel": {
"Microsoft": "Information"
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Information",
"Default": "Information" // newly
}
}
}
}
{
"Logging": {
"LogLevel": {
"Microsoft": "Information",
"Default": "Error"
}
}
}
appSettings.Production.json
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical"
}
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical",
"Default" : "Critical" // newly added line
}
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical",
"Default": "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"
}
}
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
}
}
我知道特定于环境的应用程序设置将获得最高优先级。
我有两个问题
1) 是否发生特定于环境的日志记录?如果是,那么上述逻辑的变化是什么
2) 当我在DemoLoggingApplication
profile上以开发模式运行应用程序时。我可以看到所有信息、错误和关键日志
但是当我将ASPNETCORE\u环境的值从Development
更改为Production
,用于DemoLoggingApplication
配置文件并运行应用程序时,我可以再次看到Error
和Critical
类型的日志。
如前所述,我设置了控制台
提供程序应仅显示关键
类型的Microsoft
日志。我还显示了错误
日志
虽然我读过微软的文档,但我不理解优先顺序。有人能详细解释一下为什么我会看到这两个日志吗。
我有什么不明白的地方吗。请帮帮我
提前谢谢
更新了Bob回答后的问题
它在更改appsettings.json、appsettings.Development.json和appsettings.Production.json后工作
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical"
}
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical",
"Default" : "Critical" // newly added line
}
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical",
"Default": "Critical"
}
}
}
}
appSettings.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"
}
}
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
}
}
appsettings.Development.json
{
"Logging": {
"LogLevel": {
"Microsoft": "Information"
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Information",
"Default": "Information" // newly
}
}
}
}
{
"Logging": {
"LogLevel": {
"Microsoft": "Information",
"Default": "Error"
}
}
}
appSettings.Production.json
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical"
}
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical",
"Default" : "Critical" // newly added line
}
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical",
"Default": "Critical"
}
}
}
}
现在,当我将环境更改为Development
时,我可以从Information
登录,但只有在将Default
类别添加到Development和Production之后
我只是想知道为什么会有这种行为?
当我们进行开发和生产设置时,维护appsettings.json->Logging
有什么影响
谢谢特定于环境的日志记录发生了
在您的示例中,appsettings.Development.json和appsettings.Production.json都只为“Microsoft”类别定义了日志级别。但是,从代码中完成的日志记录属于不同的日志类别,其日志级别未在代码/配置文件中定义。因此,在这两种环境中,它都将默认的最小日志级别作为“信息”
要查看差异,请在不同环境中添加具有不同日志级别设置的“默认”键,如下所示:
appsettings.Development.json
{
"Logging": {
"LogLevel": {
"Microsoft": "Information"
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Information",
"Default": "Information" // newly
}
}
}
}
{
"Logging": {
"LogLevel": {
"Microsoft": "Information",
"Default": "Error"
}
}
}
appSettings.Production.json
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical"
}
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical",
"Default" : "Critical" // newly added line
}
}
}
}
{
"Logging": {
"Console": {
"LogLevel": {
"Microsoft": "Critical",
"Default": "Critical"
}
}
}
}
谢谢,它的工作如预期,但为什么要有一个默认类别为这些供应商?你能详细解释一下工作机制吗。最初在appsettings.json中也更新了问题,所有日志级别(包括默认值)都会被注释。这就是为什么您没有看到使用appsettings.jsoni的原因。在每个提供者中都必须有Default
类别吗?如果不给会怎么样?这不是强制性的。对于给定的日志类别,如果在code/config中没有提到LogLevel,那么它将在code/config中查找默认日志类别的LogLevel。如果这也不可用,则它将最低日志级别设置作为“信息”。