Application Insights不记录跟踪信息

问题描述 投票:0回答:1

我有一个应用程序,试图将消息记录到Application Insights。我对Application Insights和Microsoft日志记录框架比较陌生,因此在这里我可能弄错了一些术语,但是很高兴澄清是否有不清楚的地方。

我已经尝试按照此处的指南进行操作:https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#aspnet-core-application即简单地在Startup类中的IServiceCollection上调用AddApplicationInsightsTelemetry方法,然后将ILogger注入到我的一个控制器中以尝试记录消息。

我没有尝试配置过滤器来设置最低严重性级别,所以我只是在做_logger.LogWarning(“我的消息”),因为默认情况下应该记录警告。

点击我的控制器后,我检查了Application Insights中的跟踪,但是在那里看不到我的消息。但是我确实看到了遥测数据,例如我可以看到请求已击中我的应用程序。

然后,我附加了调试器,并逐步执行了控制器中的代码,并注意到我的ILogger不包含ApplicationInsightsLogger。我还尝试注入ILoggerFactory并在其上调用CreateLogger方法,结果相同,即记录器不包含ApplicationInsightsLogger。

这似乎表明出于某种原因,对AddApplicationInsightsTelemetry的调用未注册ApplicationInsightsLoggerProvider,或者在添加LoggerProviders之后清除了某些东西,但是我在Startup类中看不到任何可以实现的功能。

我应该提到,同一解决方案中有另一个项目,使用相同版本的Microsoft.ApplicationInsights.AspNetCore(2.14.0),我在其中做同样的事情,但是日志消息确实显示在Application Insights中。

完全知道这里可能是什么问题,或者我如何继续对其进行故障排除?

c# asp.net-core azure-application-insights
1个回答
0
投票

请对asp .net核心应用程序执行以下步骤。我正在使用.net core 3.1 Web应用程序进行此测试。

1。请按照this article配置来自Visual Studio的应用程序见解。注意,配置完成后,将Microsoft.ApplicationInsights.AspNetCore更新到最新版本2.14.0。

2。在appsettings.json中,使用以下设置(默认情况下,应用程序见解仅记录警告级别或更高级别。)>

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "InstrumentationKey": "the key"
  }
}

3。在Startup.cs-> ConfigureServices方法中,检查代码是否如下所示:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddApplicationInsightsTelemetry();
    }

4。在您的控制器中,使用下面的类似代码:

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        _logger.LogInformation("222 this is a information message...");
        _logger.LogWarning("222 this is a warning message...");
        return View();
    }

 }

5。然后在azure门户中->您的应用程序见解->日志,您可以看到在跟踪表中记录了遥测数据:

enter image description here

[如果您还有其他问题,请告诉我:)。

© www.soinside.com 2019 - 2024. All rights reserved.