【发布时间】:2017-07-20 12:17:23
【问题描述】:
我已经安装了 VS2017 15.3.0 Preview 4 和 .NET Core 2.0 Preview 2 并创建了一个默认的 Web MVC 应用程序。我有兴趣了解新的日志记录功能如何工作,但在查看调试输出窗口时,我无法让 VS 使用 appsettings.Development.json 中定义的日志记录值。
我的理解是 appsettings.Development.json 文件优先于 appsettings.json 但只有后一个文件中的值对调试窗口有任何影响。这是正确的吗?如果是,是否需要额外设置?
这里是值和结果...
appsettings.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 中的日志级别,那么我会看到预期的输出...
appsettings.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();
}
解决方案
默认情况下,dev“appsettings.Development.json”配置文件在主“appsettings.json”配置文件之后加载,因此dev配置优先。但是,默认的 appsettings.Development.json 文件不包含日志级别设置的调试节点,这看起来很奇怪。这是有效的开发配置。
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "None",
"System": "None",
"Microsoft": "None"
},
"Debug": {
"LogLevel": {
"Default": "Information",
"System": "None",
"Microsoft": "Information"
}
}
}
}
【问题讨论】:
-
你的 Startup.cs 是什么样的?
-
@Dealdiane 我已经添加了启动文件。请注意,所有内容均来自新的 ASP.NET Core 2.0 MVC 模板。我只玩过 json 配置文件中的日志级别。
-
您也可以发布您的 Program.cs 吗?
-
@Dealdiane 我添加了 Program.cs 文件,该文件也是 2.0 的库存
标签: asp.net-core-mvc visual-studio-2017 asp.net-core-2.0