Matt Douhan
Matt Douhan

Reputation: 2113

Where do I find my logs in azure app services?

I have enabled logging in my web app running on azure web services, I can see the log output if I enable log streaming but I cannot find any GUI where I can find the logs so where are they?

I have defined my logging as follows in program.cs

WebHost.CreateDefaultBuilder(args)
                .UseApplicationInsights()
                .ConfigureLogging((hostingContext, logging) =>
                {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddConsole();
                logging.AddDebug();
                logging.AddEventSourceLogger();
                logging.AddApplicationInsights();
                })
                .UseStartup<Startup>();

And in my API controller I am simply doing this

 private readonly ILogger _logger;
        public ReveController(ILogger<Controller> logger)
        {
            _logger = logger;
        }

Followed by

_logger.LogInformation("Test test test");

My logging settings in appsettings.json looks as follows

"Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },

I looked on the app service and in App Insights but nowhere in the GUI can I find the entries where are they?

Am I missing something?

Upvotes: 2

Views: 1210

Answers (2)

Peter Bons
Peter Bons

Reputation: 29711

Log written using the Ilogger interface end up as traces in app insights. You can view them using Search in Application Insights or using Log Analytics.

The deault log level for App Insights is set to Warning, and so is the log level for the other destionations per your appsettings.json. You can change that to match your code so that everything of severity Information or higher is logged:

in code using AddFilter (Source):

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
  WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
  .ConfigureLogging(
        builder =>
        {
            // Providing an instrumentation key here is required if you're using
            // standalone package Microsoft.Extensions.Logging.ApplicationInsights
            // or if you want to capture logs from early in the application startup
            // pipeline from Startup.cs or Program.cs itself.
            builder.AddApplicationInsights();

            // Optional: Apply filters to control what logs are sent to Application Insights.
            // The following configures LogLevel Information or above to be sent to
            // Application Insights for all categories.
            builder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>
                             ("", LogLevel.Information);
        }
    );

or in config file by setting the LogLevel in the ApplicationInsights section of the Logging section (Source):

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

By the way, do mind this:

The default project template calls CreateDefaultBuilder, which adds the following logging providers:

  • Console
  • Debug
  • EventSource (starting in ASP.NET Core 2.2)

so you do not have to do that in code like you do now. It will also read the configuration. (Source)

More information
See this issue for a discussion around the default loglevel being Warning and why it cannot be overwritten in config without a ApplicationInsights section in appsettings.json.

Read more about log levels here

Read more about filters (including ones for log levels) here

Upvotes: 2

Joel Oughton
Joel Oughton

Reputation: 486

You have the default log level set to Warning but are logging at the Information level. This will prevent Application Insights from receiving the log output.

Upvotes: 1

Related Questions