Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"
      }
    }
  }
}
like image 320
OjM Avatar asked Jul 20 '17 12:07

OjM


1 Answers

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"
  }
}
like image 87
Dealdiane Avatar answered Oct 15 '22 18:10

Dealdiane