ILogger Not Respecting Log Level for Application Insights
S

2

8

I've been trying to set up Application Insights with an ASP.NET Core 2.0 application. While running my application locally, logs are showing up in Application Insights as expected. However, when deployed to an Azure App Service, while logs are being sent to the "requests" table in Application Insights, no logs are showing up in "exceptions" or "traces".

The only thing that seems to resolve this is adding the below line of code to Startup.Configure():

loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information);

The solution above is undesirable as we want to configure the log level differently by the environment, so a configuration-based solution would be preferred. Also, my guess is that the problem is configuration-related as it works fine locally. There is no special configuration that was done in Azure.

Here is the entire Startup.Configure():

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc();

    loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information);
}

My Program.cs is as follows:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }
    
    public static IWebHost BuildWebHost(string[] args) => WebHost
        .CreateDefaultBuilder(args)
        .UseApplicationInsights()
        .UseStartup<Startup>()
        .Build();
}

appsettings.json (InstrumentationKey replaced for privacy):

{
    "Logging": {
        "IncludeScopes": false,
        "Debug": {
            "LogLevel": {
                "Default": "Debug"
            }
        },
        "Console": {
            "LogLevel": {
                "Default": "Debug"
            }
        },
        "LogLevel": {
            "Default": "Debug",
            "System": "Information",
            "Microsoft": "Information"
        }
    },
    "ApplicationInsights": {
        "InstrumentationKey": "00000000-0000-0000-0000-000000000000"
    }
}
Saratov answered 2/8, 2018 at 15:32 Comment(0)
D
7

Update: New instructions are here for correctly enabling log capture. https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger

The correct way of enabling logging support is by using loggerFactory.AddApplicationInsights() in Configure method. When you run from Visual Studio, you don't need this line because VS does it under the covers for you. But it won't work when ran from outside VS, so please add loggerFactory.AddApplicationInsights method. https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Logging

To get different log level based on environment, use this line inside condition statements. Something like the following example.

if(env.IsDeveleopment())
{
   loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Debug);
}
else if(env.IsPreProduction())
{
   loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Verbose);

}
else
{
   loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);

}
Danie answered 2/8, 2018 at 19:48 Comment(1)
The answer addresses the question, which was how to vary log level by environment, but what if you want to use configuration so you can vary by category? For example, OP's appsettings.json logs Microsoft and System categories at Information and everything else at Debug.Motif
L
12

Been a long time since this was asked, but I have faced the same issue and I didn't want to need a re-deploy just to change the LogLevel.

After much research without a satisfactory answer, merging information from various sources for different use cases, I came across this solution which worked for me.

You might need to add an "ApplicationInsights" section WITHIN the "Logging" section in appsettings.json:

"Logging": {
    "LogLevel": {
      "Default": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }

This made it work without any code changes.

Loner answered 10/3, 2022 at 17:9 Comment(4)
thank you. This is what i have been looking for. I was under the impression that the usual log levels are used, but seems like not..Jalisajalisco
Thanks - I wasted far too much time on this.Botulinus
I don't understand why would sink rewrite the log level... it doesn't make sense. anyway, thnx, it works.Nahshon
But is it required to place ApplicationInsights inside Logging? Shouldn't the LogLevel and Default setting not apply to all providers including ApplicationInsights? I've noticed if I don't specify ApplicationInsights under Logging then I don't see any Information messages even though this is set ""Logging": { "LogLevel": { "Default": "Information" },Narrate
D
7

Update: New instructions are here for correctly enabling log capture. https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger

The correct way of enabling logging support is by using loggerFactory.AddApplicationInsights() in Configure method. When you run from Visual Studio, you don't need this line because VS does it under the covers for you. But it won't work when ran from outside VS, so please add loggerFactory.AddApplicationInsights method. https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Logging

To get different log level based on environment, use this line inside condition statements. Something like the following example.

if(env.IsDeveleopment())
{
   loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Debug);
}
else if(env.IsPreProduction())
{
   loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Verbose);

}
else
{
   loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);

}
Danie answered 2/8, 2018 at 19:48 Comment(1)
The answer addresses the question, which was how to vary log level by environment, but what if you want to use configuration so you can vary by category? For example, OP's appsettings.json logs Microsoft and System categories at Information and everything else at Debug.Motif

© 2022 - 2024 — McMap. All rights reserved.