Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Trace logging in ASP.NET Core?

I cannot get basice LogTrace(...) output in my application. Here's a repro:

  1. Create a new ASP.NET Core application using Visual Studio 2017.
  2. (Optional) comment out .UseApplicationInsights() so the repro is clearer
  3. Replace the code in ValuesController.cs with this:

    using System.Collections.Generic;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;
    
    namespace WebApplication1.Controllers
    {
        [Route("api/[controller]")]
        public class ValuesController : Controller
        {
            private readonly ILogger<ValuesController> logger;
    
            public ValuesController(ILogger<ValuesController> logger)
            {
                this.logger = logger;
            }
    
            [HttpGet]
            public IEnumerable<string> Get()
            {
                logger.LogError("ERROR!");
                logger.LogWarning("WARN!");
                logger.LogInformation("INFO!");
                logger.LogTrace("TRACE!");
                return new string[] { "value1", "value2" };
            }
        }
    }
    
  4. Change appsettings.Development.json to this:

    {
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Trace",
          "System": "Information",
          "Microsoft": "Information"
        }
      }
    }
    
  5. Run and view the Debug output

This leads to:

  • Actual output:

    only ERROR, WARN, and INFO

  • Expected output would be the "TRACE!" message as well

I've tried tweaking the values in the appsettings.json file as well, but that had no effect either.

Weirdly, changing the value in either file to "Error" doesn't do anything either.

Bottom Line / Question

What do I need to do to make my injected ILogger<ValuesController> respect the logging settings, including Trace level?


Footnote

Here's some of the relevant code that would be auto-generated with the above repro:

Startup.cs

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        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, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseMvc();
    }
}

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();

        host.Run();
    }
}

appsettings.json default:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}
like image 635
Jeroen Avatar asked May 28 '17 11:05

Jeroen


People also ask

How do I enable trace level logging?

To enable tracing and/or logging, you must edit the omsconfig. properties file located on the Management Server machine in the $ORACLE_HOME/sysman/config/ directory. Any properties specified in the omsconfig. properties file are case sensitive.

What is tracing in .NET Core?

ASP.NET tracing enables you to follow a page's execution path, display diagnostic information at run time, and debug your application. ASP.NET tracing can be integrated with system-level tracing to provide multiple levels of tracing output in distributed and multi-tier applications.


1 Answers

I tried:

services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));

Where the problem lies...

None of those helped me. In my case I had appsettings.Development.json in folder with exe. This file had set Default to Information. That was reason why I could not see Trace logs. This file is hidden in Solution Explorer in visual studio. I had to expand appsettings.json to see this file.

After I changed Information => Trace I could see logs with severity Trace.

like image 64
Michal Zhradnk Nono3551 Avatar answered Oct 12 '22 03:10

Michal Zhradnk Nono3551