gpt4 book ai didi

.net-core - netcore2 控制台应用程序在 appsettings 配置日志级别

转载 作者:行者123 更新时间:2023-12-04 16:29:07 26 4
gpt4 key购买 nike

我尝试向我的控制台应用程序添加一个记录器。所有配置都正确应用,除了 Logging
程序.cs:

   private static void Main(string[] args)
{
var services = new ServiceCollection();
ConfigureServices(services);

var serviceProvider = services.BuildServiceProvider();

serviceProvider.GetService<App>().Run();
Console.ReadKey();
}

private static void ConfigureServices(ServiceCollection services)
{

var envName = Environment.GetEnvironmentVariable("MY_ENV") ?? DevelopmentEnvName;

var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false)
.AddJsonFile($"appsettings.{envName}.json", true, true)
.AddEnvironmentVariables()
.Build();

services.AddSingleton(configuration);
services.AddLogging(builder =>
{
builder.AddConfiguration(configuration)
.AddConsole()
.AddDebug();
});

ConfigureOptions(services, configuration);

ConfigureDependencies(services);
}

App.Run()我尝试记录调试和信息消息
 public void Run()
{
_logger.LogDebug("Debug");
_logger.LogDebug(_appOptions.Env); //print nothing

_logger.LogInformation("Info");
_logger.LogInformation(_appOptions.Env); //print Info and debug env lines
}

appsettings.json:
 {
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information"
}
},
"AppOptions": {
"Env": "none"
}
}

appsettings.Dev.json:
 {
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug"
}
},
"AppOptions": {
"Env": "debug env"
}
}

我需要更改什么才能通过 json 设置控制日志级别?

最佳答案

简短的回答是:您错过了 GetSection调用您的 AddConfiguration .

builder.AddConfiguration(configuration.GetSection("Logging"))...

这是我对你的程序的重建:

ConsoleApp1.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.Dev.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>


程序.cs
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;
using System;
using System.IO;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var services = new ServiceCollection();
var envName = Environment.GetEnvironmentVariable("MY_ENV") ?? "Dev";

var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.{envName}.json", false, true)
.AddEnvironmentVariables()
.Build();

services.AddSingleton<IConfiguration>(configuration);
services.AddLogging(builder =>
{
builder.ClearProviders();

builder.AddConfiguration(configuration.GetSection("Logging"))
.AddConsole()
.AddDebug();
});

var provider = services.BuildServiceProvider();
var config = provider.GetService<IConfiguration>();
var appOptions = new AppOptions();

config.GetSection("AppOptions").Bind(appOptions);

var logger = provider.GetService<ILogger<Program>>();

logger.LogDebug("Debug");
logger.LogDebug(appOptions.Env); //print nothing

logger.LogInformation("Info");
logger.LogInformation(appOptions.Env); //print Info and debug env lines

Console.ReadLine();
}
}

public class AppOptions
{
public string Env { get; set; }
}
}


应用设置.json
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AppOptions": {
"Env": "none"
}
}

appSettings.Dev.json
{
"Logging": {
"LogLevel": {
"Default": "Debug"
}
},
"AppOptions": {
"Env": "debug env"
}
}

关于.net-core - netcore2 控制台应用程序在 appsettings 配置日志级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com