.NET Core 托管服务在环境变量中记录 LogLevel
.NET Core Hosted Service Logging LogLevel in Environment Variable
我使用以下代码创建了一个托管服务:
class Program
{
static async Task Main(string[] args)
{
await new HostBuilder()
.ConfigureAppConfiguration((hostContext, configApp) =>
{
configApp.AddEnvironmentVariables(prefix: "SAMPLEHOST_");
configApp.AddCommandLine(args);
})
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<SampleHostedService>();
services.AddHostedService<AnotherHostedService>();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConsole();
})
.RunConsoleAsync();
}
}
...具有以下 launchsettings.json
.
{
"profiles": {
"SampleHost.Cli": {
"environmentVariables": {
"LOGGING__LOGLEVEL__DEFAULT": "Debug",
}
}
}
}
我似乎无法将其显示在我的控制台中。 :(
Logger.LogDebug("Hello debug");
我想纯粹根据环境变量修改和调整 LogLevel
。我错过了什么吗?
我没有使用托管服务的经验,所以我不知道这是否有帮助,但是,我确实开发了 ASP.NET 应用程序,并将其添加到 appsettings.json.
"Logging": {
"LogLevel": {
"Default": "Warning",
"System": "Error",
"Microsoft": "Error"
},
"Console": {
"IncludeScopes": true
}
}
您可能需要这样的东西:
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
})
我使用以下代码创建了一个托管服务:
class Program
{
static async Task Main(string[] args)
{
await new HostBuilder()
.ConfigureAppConfiguration((hostContext, configApp) =>
{
configApp.AddEnvironmentVariables(prefix: "SAMPLEHOST_");
configApp.AddCommandLine(args);
})
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<SampleHostedService>();
services.AddHostedService<AnotherHostedService>();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConsole();
})
.RunConsoleAsync();
}
}
...具有以下 launchsettings.json
.
{
"profiles": {
"SampleHost.Cli": {
"environmentVariables": {
"LOGGING__LOGLEVEL__DEFAULT": "Debug",
}
}
}
}
我似乎无法将其显示在我的控制台中。 :(
Logger.LogDebug("Hello debug");
我想纯粹根据环境变量修改和调整 LogLevel
。我错过了什么吗?
我没有使用托管服务的经验,所以我不知道这是否有帮助,但是,我确实开发了 ASP.NET 应用程序,并将其添加到 appsettings.json.
"Logging": {
"LogLevel": {
"Default": "Warning",
"System": "Error",
"Microsoft": "Error"
},
"Console": {
"IncludeScopes": true
}
}
您可能需要这样的东西:
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
})