我们尝试通过配置文件(默认的 appsettings.json)为 ASP.NET Core 应用程序配置日志记录。该文档说这应该是可能的(https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1),但我们无法让它按预期工作。
到目前为止我们已经尝试过:
public static IHostBuilder CreateHostBuilder (string[] args) =>
Host.CreateDefaultBuilder (args)
.ConfigureAppConfiguration ((hostingContext, config) =>
{
config.Sources.Clear ();
var env = hostingContext.HostingEnvironment;
config.AddJsonFile ("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile ($"appsettings.{env.EnvironmentName}.json",
optional: true, reloadOnChange: true);
config.AddEnvironmentVariables ();
})
.ConfigureLogging ((hostingContext, logging) =>
{
logging.ClearProviders ();
logging.AddConfiguration (hostingContext.Configuration.GetSection ("Logging"));
})
.ConfigureWebHostDefaults (webBuilder =>
{
webBuilder.UseStartup<Startup> ();
});
Run Code Online (Sandbox Code Playgroud)