【问题标题】:Find IdentityServer4 errors in Application Insights在 Application Insights 中查找 IdentityServer4 错误
【发布时间】:2020-01-24 14:07:57
【问题描述】:

我有以下设置:

  • ASP.NET Core 3.1 应用程序,部署到 Azure 应用服务
  • 通过IdentityServer4.AspNetIdentity 包使用IDS4,版本3.1.0
  • Program.cs 中,它从Microsoft.ApplicationInsights.AspNetCore 版本2.5.1 调用.UseApplicationInsights()
  • Default LogLevel 设置为 Warning
  • 我的Error.cshtml 显示Activity.Current?.Id ?? HttpContext.TraceIdentifier

这会正确记录来自应用服务的几件事,但我找不到由 IDS4 报告的在 OpenID/OAuth2 协议级别的任何错误(例如,请求的无效范围等)。例如,我可以找到这样的东西:

requests
| where cloud_RoleName == 'my-identity-server-4-role'
| order by timestamp desc
| where url contains 'errorId'
| limit 100

这是有道理的,因为我在登录时遇到了一些(其他)问题,其中隐式流静默刷新失败并重定向到问题 url,例如 https://my-identity-domain.example.org/home/error?errorId=some-long-string-here。该页面向我显示了一个错误页面,说明我可以在我的机器上打开 DeveloperExceptionPage 功能,或者我可以使用:

请求 ID:|123aaac2c1cccf4eb3333411aaa183da7e.bba43cca1_

现在我尝试在 AppInsights 中查找 requests 条目

  • | where id contains "123aaac2c"
  • | where operation_Id contains "123aaac2c"
  • | where operation_ParentId contains "123aaac2c"
  • | where session_Id contains "123aaac2c"
  • | where itemId contains "123aaac2c"
  • | where problemId contains "123aaac2c"

exceptions 也类似,其中任何 id 字段都包含我的部分 id。但我似乎找不到结果。

我做错了什么?我还在找错地方吗?或者我应该以某种方式增加日志级别?还是我需要在某处添加代码来配置 IdentityServer4 来记录这些东西?


注意:如果我从控制台本地运行我的应用程序,我确实会看到输出流错误。例如,我在启动中添加了_logger.LogError("test error"),并将我的 SPA 配置为使用我的本地 IDS,但范围不正确,我看到了以下输出:

fail: MyApp.Identity.Startup[0]
      test error
Hosting environment: Development
Content root path: C:\git\my-app\MyApp.Identity
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
fail: IdentityServer4.Validation.ScopeValidator[0]
      Invalid scope: triggererroridwithinvalidscope
fail: IdentityServer4.Endpoints.AuthorizeEndpoint[0]
      Request validation failed

第一个错误只是检查正常错误是如何记录的,第二个错误是模拟我的实际问题(触发errorId 页面,如我的问题前面提到的)。

简而言之,我确实看到通过 ASP.NET Core 日志记录在控制台上记录的内容,但我在 AppInsights 中找不到它们。


注意:我进一步研究了 IdentityServer4 如何进行日志记录,as documented 它使用 ASP.NET Core 默认日志系统进行日志记录,例如注入来自 Microsoft 的 Abstractions 的 ILogger<T>,然后使用一些辅助方法调用 (for example):

var details = new TokenRequestValidationLog(_validatedRequest);
// abbreviated snippet
_logger.Log(LogLevel.Error, "Some message" + ", details: {@details}", details);

也许这没有出现在 AppInsights 中,因为它没有合适的地方?它不是 Trace,不是 Request,也没有真正的 Exception?

【问题讨论】:

  • 几个月后,今天重新审视了我们的生产设置。仍然无法在 AppInsights 中找到这些错误,也找不到任何使其正常工作的方法。 ????

标签: c# asp.net-core identityserver4


【解决方案1】:

如果您需要在 Application Insights 中查找与错误相关的日志条目,您可以搜索 IdentityServer 错误页面上显示的请求 ID。请求 ID 来自 System.Diagnostics.Activity.Current.Id 属性,应自动附加到日志事件。你可以这样查询:

traces
| where customDimensions["RequestId"] == "80006a82-0000-e800-b63f-84710c7967bb"
| order by timestamp desc
| limit 50

关于在 Insights 中根本没有显示的身份服务器事件,您能否尝试将其添加到您的 Startup 类中?

services.AddIdentityServer(options => {
    options.Events.RaiseErrorEvents = true;
    options.Events.RaiseInformationEvents = true;
    options.Events.RaiseFailureEvents = true;
    options.Events.RaiseSuccessEvents = true;
})

另请注意,日志事件可能需要一些时间才能真正显示在 Application Insights 日志查看器中。要将其排除为问题来源,我会在运行您的查询之前等待几分钟。

【讨论】:

  • 感谢您花时间回复!我尝试了你的建议,但无济于事,唉。我搜索了requestsexceptions,因为你没有提到我应该检查哪一个。 - PS。我注意到你建议期待一个 GUID,但 Request ID IDS 给我的格式是 \|[0-9a-z]+\.[0-9a-z]+_ 所以例如|ae33f334......a32af_。 - 我也确实等了几分钟,并尝试解决可能的采样问题...
  • 身份服务器事件可以在traces中找到。你已经看过了吗?
  • 我刚才有,但还没有找到任何 IDS 跟踪。感谢您的建议,我会继续 crafting a minimal repro 看看您的帮助是否会导致我成为生产应用程序的最终罪魁祸首。
  • 我们将在这里留下问答范围,但我在您链接的存储库中测试了代码,它对我来说很好。我只是将appsettings.json 中的检测键替换为我自己的 Insights 实例的检测键。在引发错误后,我能够看到 IdentityServer 生成的跟踪错误以及附加的请求 ID。我建议仔细检查您的检测密钥是否已正确加载,并且您的 Insights 实例没有其他配置问题。
猜你喜欢
  • 2019-11-28
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
相关资源
最近更新 更多