ASP NET Core 2.0 appsettings.Development.json 不適用於日誌記錄配置
我已經安裝了 VS2017 15.3.0 Preview 4 和 .NET Core 2.0 Preview 2 並創建了一個預設的 Web MVC 應用程序。我有興趣了解新的日誌記錄功能如何工作,但在查看調試輸出視窗時,我無法讓 VS 使用 appsettings.Development.json 中定義的日誌記錄值。
我的理解是 appsettings.Development.json 文件優先於 appsettings.json 但只有後一個文件中的值對調試視窗有任何影響。這是正確的嗎?如果是這樣,是否需要額外的設置?
以下是數值和結果…
應用設置.json
{ "Logging": { "IncludeScopes": false, "Debug": { "LogLevel": { "Default": "None" } }, "Console": { "LogLevel": { "Default": "Information" } } } }appsettings.Development.json
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Information", "System": "Information", "Microsoft": "Information" } } }調試時輸出為空(請注意僅顯示 Application Insights 遙測記錄,我還沒有弄清楚如何擺脫這些記錄)
但是,如果我更改 appsettings.json 中的日誌級別,那麼我會看到預期的輸出……
應用設置.json
{ "Logging": { "IncludeScopes": false, "Debug": { "LogLevel": { "Default": "Information" } }, "Console": { "LogLevel": { "Default": "Information" } } } }調試時的新輸出(注意現在包含 Microsoft.AspNetCore.Hosting.Internal.WebHost:Information)
我的 Startup.cs 文件是使用新項目創建的預設 ASP.NET Core 2.0 模板,如下所示。appsettings.json 和 appsettings.Development.json 文件也是由新項目模板自動創建的。
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddTransient<ISmsSender, AuthMessageSender>(); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } }這是我的 Program.cs,它也是 ASP.NET Core 2.0 MVC 模板的預設值。
public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); }解決方案
預設情況下,開發“appsettings.Development.json”配置文件在主“appsettings.json”配置文件之後載入,因此開發配置優先。但是,預設的 appsettings.Development.json 文件不包含日誌級別設置的調試節點,這看起來很奇怪。這是工作的開發配置。
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "None", "System": "None", "Microsoft": "None" }, "Debug": { "LogLevel": { "Default": "Information", "System": "None", "Microsoft": "Information" } } } }
您的每個記錄器設置
appsettings.json優先於您的全域類別預設設置appsettings.Development.json。您在 Visual Studio 的輸出視窗中看不到任何日誌,因為您的記錄器
appsettings.json的日誌級別為“無”Debug。您需要遵循 .NET Core 2.0(預覽版 2)的每個記錄器格式,它應該是下面的格式,
appsettings.Development.json以覆蓋來自.NET Core 的記錄器格式appsettings.json。{ "Logging": { "<Logger>": { "LogLevel": { "Default": "<LogLevel>" } } } }需要明確的是,如果您想要 Debug 記錄器的Trace日誌級別,那麼它在您的 json 配置中應該是這樣的:
{ "Logging": { "Debug": { "LogLevel": { "Default": "Trace" } }

