Judy007
Judy007

Reputation: 5860

In ASP.NET Core 3.0 MVC Project, How exactly do I log to app insights using ilogger?

I'm trying to determine how exactly to log to app insights using

_logger.LogInformation

Im not finding the documentation https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core very helpful.

For example, lets imagine you create an asp.net core 3.0 MVC project using visual studio (template) By default, the controller has injected _logger.

What exact step-by-steps are required to get the logger to send the data to app insights? Any help is appreciated. I just am hoping to know which files exactly I need to update in the project.

Upvotes: 0

Views: 852

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29940

Update:

You can control it by setting proper loglevel in appsettings.json file -> adding ApplicationInsights section, without any code change(for .net core 3.0, please use Microsoft.ApplicationInsights.AspNetCore package 2.8.0 version or higher, the notes is here).

Here is the my appsettings.json:

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "InstrumentationKey": "xxxxxx"
  }
}

Then LogInformation() method can work well.

Upvotes: 3

Related Questions