我有以下设置:
- 部署到 Azure 应用服务的 ASP.NET Core 3.1 应用程序
- 通过
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 协议(protocol)级别的任何错误(例如,请求的范围无效等)。例如,我可以找到这样的东西:
requests
| where cloud_RoleName == 'my-identity-server-4-role'
| order by timestamp desc
| where url contains 'errorId'
| limit 100
这是有道理的,因为我在登录方面遇到了一些(其他)问题,其中隐式流程静默刷新失败并重定向到问题网址 https://my-identity-domain.example.org/home/error?errorId=some-long-string-here
。该页面向我显示了一个错误页面,解释了我可以在我的计算机上打开 DeveloperExceptionPage 功能,或者我可以使用:
Request ID: |123aaac2c1cccf4eb3333411aaa183da7e.bba43cca1_
现在我尝试找到 requests
AppInsights 条目由
-
| 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 默认日志系统进行日志记录,例如注入(inject)一个ILogger<T>
来自 Microsoft 的 Abstractions,然后使用一些辅助方法来调用( for example ):
var details = new TokenRequestValidationLog(_validatedRequest);
// abbreviated snippet
_logger.Log(LogLevel.Error, "Some message" + ", details: {@details}", details);
也许这不会出现在 AppInsights 中,因为没有合适的地方?它不是 Trace,不是 Request,也没有真正的 Exception?
最佳答案
如果需要在 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 日志查看器中可能需要一些时间。为了排除这个问题来源,我会等待几分钟再运行您的查询。
关于c# - 在 Application Insights 中查找 IdentityServer4 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59898089/