【问题标题】:ASP.NET Core trace logging on Azure with Application Insights使用 Application Insights 在 Azure 上进行 ASP.NET Core 跟踪日志记录
【发布时间】:2021-08-03 04:18:38
【问题描述】:

我有一个 ASP.NET Core Web 项目部署到 Azure 并配置了 Application Insights。 Insights 可以正常接收请求等数据,但我无法让它显示我的日志。

我正在使用 vanilla Microsoft.Extensions.Logging 框架,并且在控制器操作上记录了一个测试错误

logger.LogError("Test application insights message");

在我设置的 Startup.cs 配置方法中

loggerFactory.AddAzureWebAppDiagnostics();

...并且可以成功看到错误消息出现在 Azure 日志流中:

2017-04-14 11:06:00.313 +00:00 [Error] Test application insights message

但是,我也希望此消息出现在 Application Insights 跟踪中,但找不到它。我想我需要配置 ILoggerFactory,但不确定如何配置,也找不到有关该主题的任何文档。

提前感谢您的帮助

【问题讨论】:

  • 你配置存储了吗?我假设这就是你所指的?您希望将其保存在 Azure 存储帐户中吗?
  • @pqsk - 不,不是存储帐户,在 Application Insights 跟踪中,您可以在 Azure 门户上的“搜索”下的 Application Insights 刀片上找到
  • 您能否添加一个屏幕截图,说明您如何在门户网站上进行所有设置以及一些有关如何设置的代码(我假设是 DI)。我认为这将有助于分析这个问题

标签: asp.net-core azure-application-insights asp.net-core-logging


【解决方案1】:

如果其他人试图解决这个问题,我得到了我想要的结果:

loggerFactory.AddApplicationInsights(app.ApplicationServices);

【讨论】:

  • 我正在尝试做与您所做的相同的事情,但我仍然没有在我的应用程序洞察力中看到日志。您能否分享您的 StartUp.cs(可能还有 Program.cs)代码的其他部分?
  • 嗨 @WesternAussie,我的 Program.cs 没什么特别的...只是 hostBuilder 对象上的 .UseApplicationInsights();。在 Startup.cs 中,唯一相关的部分是 loggerFactory.AddAzureWebAppDiagnostics(); // Ensures that it goes to azure logs for streamingloggerFactory.AddApplicationInsights(app.ApplicationServices); // and in to Trace on insights
  • 谢谢。只是花了几个小时试图弄清楚为什么我的信息日志在应用程序洞察中显示在 Dev 中运行时,而不是在暂存中(不是我一直想要它,但很高兴知道如果需要的话怎么做之后)。我得到了相同的答案,但通过:github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/…
  • ASP.NET Core 2.0 您不必安装提供程序包或调用AddAzureWebAppDiagnostics 扩展方法。当您将应用程序部署到 Azure 应用服务github.com/aspnet/Docs/issues/5016 时,该提供程序自动可供您的应用程序使用
  • AddApplicationInsights 已过时
【解决方案2】:

对我来说,只有当我在 Startup.cs 中将 InstrumentationKey 指定为 services.AddLogging 时,它才有效,如下所示:

services.AddApplicationInsightsTelemetry();

services.AddLogging(builder =>
{
    // Only Application Insights is registered as a logger provider, get the key from appSettings.json file and add application insight
    var instrumentationKey = Configuration.GetValue<string>("ApplicationInsights:InstrumentationKey");
    builder.AddApplicationInsights(instrumentationKey);
});

【讨论】:

    猜你喜欢
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    相关资源
    最近更新 更多