ASP NET Core 2.0 appsettings.Development.json无法使用日志记录配置

OjM*_*OjM 7 asp.net-core-mvc visual-studio-2017 asp.net-core-2.0

我已经安装了VS2017 15.3.0 Preview 4和.NET Core 2.0 Preview 2,并创建了一个默认的Web MVC应用程序.我有兴趣了解新的日志记录功能如何工作但我无法让VS在查看Debug输出窗口时使用appsettings.Development.json中定义的日志记录值.

我的理解是appsettings.Development.json文件优先于appsettings.json,但只有后一个文件中的值对调试窗口有任何影响.这是正确的吗?如果有,是否需要额外的设置?

以下是价值和结果......

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "None"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

appsettings.Development.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Information",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

调试时清空输出(请注意仅显示Application Insights遥测记录,我还没有找到如何摆脱它)

空输出

但是,如果我更改appsettings.json中的日志级别,那么我会看到输出符合预期...

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Information"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

调试时的新输出(请注意,Microsoft.AspNetCore.Hosting.Internal.WebHost:现在包含信息)

在此输入图像描述

我的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?}");
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的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();
}
Run Code Online (Sandbox Code Playgroud)

默认情况下,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"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Dea*_*ane 4

您的每个记录器设置appsettings.json优先于您的appsettings.Development.json.

您在 Visual Studio 的输出窗口中看不到任何日志,因为记录器appsettings.json的日志级别为“无”Debug

您需要遵循 .NET Core 2.0(预览版 2)的每个记录器格式(应为以下格式),appsettings.Development.json以覆盖 .NET Core 2.0 中的记录器格式appsettings.json

{
  "Logging": {
    "<Logger>": {
      "LogLevel": {
        "Default": "<LogLevel>"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

需要明确的是,如果您想要调试记录器的跟踪日志级别,那么它在 json 配置中应该如下所示:

{
  "Logging": {
    "Debug": {
      "LogLevel": {
        "Default": "Trace"
  }
}
Run Code Online (Sandbox Code Playgroud)