4

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?

tolini2481
  • 41
  • 3
  • Hello, if the answer is helpful, please help [accept it as answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work?answertab=active#tab-top), Thanks. – Ivan Glasenberg Feb 26 '20 at 02:01

1 Answers1

1

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

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60