在 ASP.NET Core 3.0 MVC 项目中,我究竟如何使用 ilogger 记录应用洞察?

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

我正在尝试确定如何使用

准确记录应用洞察
_logger.LogInformation

我觉得文档 https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core 不是很有帮助。

例如,假设您使用 visual studio(模板)创建了一个 asp.net 核心 3.0 MVC 项目 默认情况下,控制器已注入 _logger。

让记录器将数据发送到应用洞察需要哪些具体的步骤?任何帮助表示赞赏。 我只是希望知道我需要在项目中更新哪些文件。

更新:

您可以通过在 appsettings.json 文件中设置适当的日志级别 -> 添加 ApplicationInsights section 来控制它,无需任何代码更改(对于 .net core 3.0,请使用 Microsoft.ApplicationInsights.AspNetCore package 2.8.0 version or higher, the notes is here)。

这是我的 appsettings.json:

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

那么LogInformation()方法就可以了