Azure Application Insights 不显示数据

Azure Application Insights not showing data

我有一个 ASP.NET 核心应用程序 运行 作为 Azure 应用服务。 Azure Application Insights 已启用(我关注 these instructions). The problem is my instance of Azure Insights on Azure Portal isn't showing any useful data except for Live Metrics (see the screenshot)。如您所见,屏幕截图中有多个请求和自定义事件。

但是,当我打开交易搜索时,它什么也没显示 (see the screenshot)。 活动页面也是空的 (see the screenshot)。

到目前为止,我仔细检查了一个 InstrumentKey。我也尝试使用 ConnectionString 代替 InstrumentKey,但没有帮助。

我的应用程序在 .NET Core 3.1 上 运行。我安装了最新版本的 Microsoft.ApplicationInsights.AspNetCore 软件包,即 2.19.0.

以下是 Program.cs 中日志记录的配置方式:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureLogging(builder =>
            {
                builder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
            });

下面是来自 Startup.cs 的代码:

services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions 
        {
            ConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")
        });

LogLevel也配置在appsettings.json:

"Logging": {
"LogLevel": {
  "Default": "Warning"
},
"ApplicationInsights": {
  "LogLevel": {
    "Default": "Information"
  }
}

更新: 拥有更多权限的我的管理员可以看到所有数据,包括事件、性能操作等。所以我想这与权限有关。虽然很奇怪我没有看到任何警告消息。管理员为我分配了更多角色 (see the screenshot),但没有任何区别。

如果能就此问题提供任何帮助,我将不胜感激!

我也试过了,可以看到门户内的日志。

按照 Peter Bons 的建议确保您在控制器中使用 ILogger

这是我遵循的步骤。

  • 我从 GitHub 下载了一个示例项目,然后在 Visual studio 中提取打开的项目并使用 Application insight 遥测进行配置。将最新的 Microsoft.ApplicationInsights.AspNetCore 更新为 2.19.0

并在从 Azure 门户>Application insight(my applnsight)>overview 复制的 appsettings.json 中添加了检测密钥.

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "InstrumentationKey": "mykey",
    "ConnectionString": "InstrumentationKey=6xxxxxx-xxxxx-xxxx-xxxx-0000000xxxxxxxx.in.applicationinsights.azure.com/"
  }
}
  • Ilogger 配置在我的 controller.cs
namespace ApplicationInsightsTutorial.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

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

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {

            var iteracion = 4;

            _logger.LogDebug($"Debug {iteracion}");
            _logger.LogInformation($"Information {iteracion}");
            _logger.LogWarning($"Warning {iteracion}");
            _logger.LogError($"Error {iteracion}");
            _logger.LogCritical($"Critical {iteracion}");

            try
            {
                throw new NotImplementedException();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
            }

            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

startup.cs中添加

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddApplicationInsightsTelemetry();//telemetry added
        }

完成以上所有配置后 运行 应用程序并导航到 Azure 门户以检查日志。

请确保您已在我的 controller.cs 中提供了您想要检查的日志信息。

从日志中我们也可以看到带有代码行的 exceptions/errors。 以下是一些截图供参考:

有关更多信息,请参阅此

在扯掉几乎所有的头发后,我终于解决了这个问题! 原来 Azure Application Insights 的实例链接到属于我无权访问的资源组的日志分析工作区。所以日志被妥善存储,但我没有权限阅读它们。 我的管理员通过创建一个新的 Azure Application Insights 实例解决了这个问题,该实例链接到我的资源组中的日志分析工作区。 对于不熟悉 Log Analytic Workspace 的任何人 - 可以在创建 Azure Application Insights (see the screen) 的新实例时指定它。

感谢大家的帮助!