7

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"
    }
}
janw
  • 8,758
  • 11
  • 40
  • 62
rfa3
  • 73
  • 1
  • 3

2 Answers2

9

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.

Adrián Alvarez
  • 329
  • 3
  • 10
  • 2
    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.. – Alex Apr 15 '22 at 08:12
  • 3
    Thanks - I wasted far too much time on this. – kra May 13 '22 at 05:28
  • 1
    I don't understand why would sink rewrite the log level... it doesn't make sense. anyway, thnx, it works. – Alamakanambra Aug 05 '22 at 18:53
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);

}
cijothomas
  • 2,818
  • 1
  • 13
  • 25
  • 8
    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. – tdykstra Sep 23 '18 at 20:35