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

2

11

I have installed VS2017 15.3.0 Preview 4 and .NET Core 2.0 Preview 2 and have created a default Web MVC application. I am interested in seeing how the new logging functionality works but I cannot get VS to use the logging values defined in appsettings.Development.json when viewing the Debug output window.

My understanding is that the appsettings.Development.json file takes precedence over the appsettings.json yet only values in the latter file has any effect on the debug window. Is this right? If so is there additional setup 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 (note only Application Insights Telemetry records are shown which I have not worked out how to get rid of yet)

Empty Output

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

appsettings.json

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

New Output when debugging (note that Microsoft.AspNetCore.Hosting.Internal.WebHost:Information is now included)

enter image description here

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

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 for an ASP.NET Core 2.0 MVC template.

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

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

SOLUTION

By default the dev "appsettings.Development.json" config file is loaded after the main "appsettings.json" config file therefore dev configuration taking priority. However the default appsettings.Development.json file does not include the Debug node for log level settings which seems strange. Here is the working dev config.

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "None",
      "System": "None",
      "Microsoft": "None"
    },
    "Debug": {
      "LogLevel": {
        "Default": "Information",
        "System": "None",
        "Microsoft": "Information"
      }
    }
  }
}
Incandesce answered 20/7, 2017 at 12:17 Comment(4)
What does your Startup.cs look like?Involucel
@Involucel i've added the startup file. Note that everything is stock from the new ASP.NET Core 2.0 MVC template. I have only played around with the log levels within the json config files.Incandesce
Can you also post your Program.cs?Involucel
@Involucel I've added the Program.cs file which is also stock for 2.0Incandesce
I
4

The per logger setting from your appsettings.json takes precedence over the global category defaults from your appsettings.Development.json.

You don't see any logs from Visual Studio's output window since your appsettings.json has a log level of None for the Debug logger.

You need to follow the per logger format for .NET Core 2.0 (preview 2), which should be the format below, in your appsettings.Development.json to overwrite the ones from your appsettings.json.

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

To be clear, if you wanted a Trace log level for the Debug logger, this is what it should look like in your json config:

{
  "Logging": {
    "Debug": {
      "LogLevel": {
        "Default": "Trace"
  }
}
Involucel answered 21/7, 2017 at 4:30 Comment(0)
B
0

for it was secrets file which I had configured long back and forgot.

// Access the configuration and environment
var configuration = builder.Configuration;
var environment = builder.Environment;

// Log the environment
Console.WriteLine($"Environment: {environment.EnvironmentName}");

// Log all the configuration sources
foreach (var provider in ((IConfigurationRoot)configuration).Providers)
{
    if (provider is FileConfigurationProvider fileProvider)
    {
        Console.WriteLine($"Configuration source: {fileProvider.Source.Path}");
    }
}

use this code to know which provider you are using for configuration.The last added provider will have highest precedence.

Burress answered 30/5 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.