0

我使用以下代码创建了一个托管服务:

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纯粹的环境变量。我错过了什么吗?

4

2 回答 2

1

你可能需要这样的东西:

.ConfigureLogging((hostingContext, logging) => 
{
    logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
    logging.AddConsole();
})
于 2019-04-10T11:15:32.603 回答
-1

我没有托管服务的经验,所以我不知道这会有所帮助,但是,我确实在 ASP.NET 应用程序上工作,并将其添加到appsettings .json中。

  "Logging": {
   "LogLevel": {
    "Default": "Warning",
    "System": "Error",
    "Microsoft": "Error"
   },
   "Console": {
    "IncludeScopes": true
   }
  }
于 2019-04-10T11:10:34.163 回答