在 Application Insights 中查找 IdentityServer4 错误

Jer*_*oen 6 c# asp.net-core identityserver4

我有以下设置:

  • ASP.NET Core 3.1 应用程序,部署到 Azure 应用服务
  • 通过IdentityServer4.AspNetIdentity包使用 IDS4 ,版本 3.1.0
  • Program.cs.UseApplicationInsights()Microsoft.ApplicationInsights.AspNetCore版本 2.5.1调用
  • 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
Run Code Online (Sandbox Code Playgroud)

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

请求 ID:|123aaac2c1cccf4eb3333411aaa183da7e.bba43cca1_

现在我尝试通过以下方式找到requestsAppInsights 中的条目

  • | 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 但范围不正确,我看到以下输出:

requests
| where cloud_RoleName == 'my-identity-server-4-role'
| order by timestamp desc
| where url contains 'errorId'
| limit 100
Run Code Online (Sandbox Code Playgroud)

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

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


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

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

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

Red*_*one 2

如果需要在 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
Run Code Online (Sandbox Code Playgroud)

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

services.AddIdentityServer(options => {
    options.Events.RaiseErrorEvents = true;
    options.Events.RaiseInformationEvents = true;
    options.Events.RaiseFailureEvents = true;
    options.Events.RaiseSuccessEvents = true;
})
Run Code Online (Sandbox Code Playgroud)

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