0
votes

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

_logger.LogInformation

Im not finding the documentation https://docs.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.

1
@MohsinMehmood, thats for 2.1? Im looking for dotnet core 3.0jbooker
@joey Which version of nuget package "Microsoft.ApplicationInsights.AspNetCore" are you using? You should try version 2.8.0 or higher.Ivan Yang
@ivan, can you please post an answer with the exact steps necessary?including the package reference?jbooker
@joey, please let me know if you have more issues after the answer below :).Ivan Yang

1 Answers

3
votes

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.