【发布时间】:2020-01-13 08:00:43
【问题描述】:
我们正在尝试通过配置文件(默认 appsettings.json)为我们的 ASP.NET Core 应用程序配置日志记录。文档说它应该是可能的 (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1),但我们无法让它像预期的那样工作。
到目前为止我们所做的尝试:
- 将代码保留在 ASP.NET Core 的项目模板中,只需更改 appsettings.json => 无效
- 将 appsettings.json 显式添加为配置文件并在 Program.cs 中配置日志记录 => 完全不记录
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> ();
});
- ... 在 Startup.ConfigureServices 中配置日志记录 => 无效
public void ConfigureServices (IServiceCollection services)
{
services.AddLogging (config => config.AddConfiguration (Configuration.GetSection("Logging")));
services.AddControllers ();
}
【问题讨论】:
标签: asp.net-core logging .net-core