Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in Azure Portal are the logger.Log statements?

I have a .NET Core WebAPI app. The app is deployed on Azure as an App Service.

In the code, I have enabled Application Insights like so

public static IWebHost BuildWebHost(string[] args) =>
    WebHost
    .CreateDefaultBuilder(args)
    .UseApplicationInsights()
    .UseStartup<Startup>()
    .ConfigureLogging((hostingContext, logging) =>
    {                      
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")).SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Error);  
        logging.AddApplicationInsights(" xxxxx-xxxx-xxxx-xxxx--xxxxxxxxxxx").SetMinimumLevel(LogLevel.Trace);
    })
    .Build();

In the constructor of a controller and inside an method of a controller I have these logging statements.

_logger.LogInformation("ApiController, information");
_logger.LogWarning("ApiController, warning");
_logger.LogCritical("ApiController, critical");
_logger.LogWarning("ApiController, error");
_logger.LogDebug("ApiController, debug");

In the Azure Portal I have Application Insights for my App Service enabled. Here a picture from the portal.

App Insights in Azure Portal

But where do I see the logging statements in the Azure portal?

When I go to Application Insights -> Logs and I query by

search *

I can see the requests made to the API but not the logging statements.

Application Insights Log

Where are the log statements?

like image 357
tolini2481 Avatar asked Sep 13 '25 02:09

tolini2481


1 Answers

First, it is not good practice to configure the log level in code. You can easily configure the log level in the appsettings.json file. So in Program.cs -> public static IWebHost BuildWebHost method, change the code to below:

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

Then in appsettings.json(also right click the file -> properties -> set "copy to output directory" to "Copy if newer"):

{
  "Logging": {
    "IncludeScopes": false,
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Trace"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "the key"
  }
}

In Controller, like ValuesController:

    public class ValuesController : Controller
    {
        private readonly ILogger _logger;
        public ValuesController(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<ValuesController>();
        }

        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            _logger.LogInformation("ApiController, information");
            _logger.LogWarning("ApiController, warning");
            _logger.LogCritical("ApiController, critical");
            _logger.LogWarning("ApiController, error");
            _logger.LogDebug("ApiController, debug");

            return new string[] { "value1", "value2" };
        }
     }

Run the project, and wait for a few minutes(application insights would always take 3 to 5 minutes or more to display the data). Then nave to azure portal -> application insights logs, remember that all the logs written by ILogger are stored in "traces" table. Just write the query like "traces" and specify a proper time range, you should see all the logs like below:

enter image description here

like image 188
Ivan Yang Avatar answered Sep 15 '25 14:09

Ivan Yang