ASP NET Core 2.0 appsettings.Development.json not working with logging configuration

I installed VS2017 15.3.0 Preview 4 and .NET Core 2.0 Preview 2 and created a default Web MVC application. I'm curious about how the new logging feature works, but I can't get VS to use the log values ​​defined in appsettings.Development.json when looking at the Debug output window.

I understand that the appsettings.Development.json file takes precedence over appsettings.json, but only the values ​​in the last file have any effect on the debug window. It is right? If so, is additional configuration required?

Here are the values ​​and results ...

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"
    }
  }
}

      

Empty output when debugging (only pay attention to the Insights app data. Telemetry records are displayed which I have not worked out yet, how to get rid of)

Empty output

However, if I change the log levels in appsettings.json then I see the result as expected ...

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Information"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

      

New output when debugging (note Microsoft.AspNetCore.Hosting.Internal.WebHost is now included: info)

enter image description here

My Startup.cs file is a default ASP.NET Core 2.0 template created with a new project as shown below. Also both appsettings.json and appsettings.Development.json files were automatically generated using the new project template.

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?}");
        });
    }
}

      

Here is my Program.cs which is also the default ASP.NET Core 2.0 MVC pattern.

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

      

Decision

By default, the dev config file "appsettings.Development.json" is loaded after the main config file "appsettings.json", so the dev config takes precedence. However, the appsettings.Development.json file does not include the Debug node setting for the log level by default, which seems strange. Here is a working dev config.

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "None",
      "System": "None",
      "Microsoft": "None"
    },
    "Debug": {
      "LogLevel": {
        "Default": "Information",
        "System": "None",
        "Microsoft": "Information"
      }
    }
  }
}

      

+3


source to share


1 answer


The value for each registrar from yours appsettings.json

takes precedence over the global defaults from yours appsettings.Development.json

.

You don't see any logs from the Visual Studio Output window because yours appsettings.json

has a log level of None for the logger Debug

.

You need to watch out for the .NET Core 2.0 logger format (preview 2) which should be below in yours appsettings.Development.json

to overwrite them from yours appsettings.json

.



{
  "Logging": {
    "<Logger>": {
      "LogLevel": {
        "Default": "<LogLevel>"
      }
    }
  }
}

      

To be clear, if you want the Tracing log level for the Debug log, this is what your json config should look like:

{
  "Logging": {
    "Debug": {
      "LogLevel": {
        "Default": "Trace"
  }
}

      

+2


source







All Articles