Steven
Steven

Reputation: 18859

Not all log levels are being logged in Application Insights

I have an application set up to use Application Insights, and I'm trying to manually log some custom information. Here's my controller:

public class MyController : Controller
{
    private ILogger<MyController> Logger { get; set; }

    public MyController(ILogger<MyController> logger)
    {            
        Logger = logger;
    }

    public IActionResult Index()
    {
        Logger.LogCritical("Test critical");
        Logger.LogError("Test error");
        Logger.LogWarning("Test warning");
        Logger.LogInformation("Test information");
        Logger.LogDebug("Test debug");
        Logger.LogTrace("Test trace");

        ...
    }

I have this in my Startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
    ...

}

And this in my Program.cs:

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

And in my appsettings.json:

"Logging": {
  "IncludeScopes": false,
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "LogLevel": {
    "Default": "Warning"
  }
}

When I look in Application Insights in my Azure portal, the only things logged are:

enter image description here

So it's skipping a few for some reason, and only logging Critical, Warning, and Error. I'd mainly like to use LogInformation.

Is there something I need to change in my Logging settings or maybe in the Startup file?

Upvotes: 4

Views: 8511

Answers (2)

espumita
espumita

Reputation: 177

We had the same problem, and our solution was to set a filter to LogLevel.Trace, and match the filter category with the ILoger category. More info in this doc.

We notice that by default only Warning / Error / Critical, is sent to Application Insights. To remove this restriction is needed to specify a filter with a valid category.

We can define a rule for all categories like:

logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);

This can be dangerious in production cause will cause masive amounts of logs. We can just disable this default behaviour only for a ILogger class specifying a category, the convention is to use the class name as category like this:

logging.AddFilter<ApplicationInsightsLoggerProvider>("Mynamespace.MyController", LogLevel.Trace);

Upvotes: 1

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29950

If you want to collect all telemetry data, you should not specify Warning logLevel in appsettings.json. The Warning log level will only collect the Warning / Error / Critical data, but abandon the Trace / Debug / Information data.

For more details, please refer to this doc and this doc.

Please specify the log level of application insights as Trace in appsettings.json, like below:

"Logging": {
  "IncludeScopes": false,
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "Trace"
    }
  },
  "LogLevel": {
    "Default": "Warning"
  }
}

Upvotes: 8

Related Questions