问题描述
我从 this repo 运行代码并从 Book.API 生成错误,我得到如下错误页面:
请求 ID 为 |574e8e13-4e9d31fed64d4793。
当我查看文件中的日志条目时,我看到:
{
"Timestamp": "2021-01-25T18:07:24.6565993-06:00","Level": "Fatal","MessageTemplate": "BADnesS!!! Cannot open database \"Demo_EffectiveLogging\" requested by the login. The login Failed.\r\nLogin Failed for user 'TCA-PRECISION\\terry.aney'. -- {ErrorId}.","Exception": "System.Data.sqlClient.sqlException (0x80131904): Cannot open database \"Demo_EffectiveLogging\" requested by the login. The login Failed.\r\nLogin Failed for user 'TCA-PRECISION\\terry.aney'.\r\n at System.Data.sqlClient.sqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,sqlConnectionString connectionoptions,sqlCredential credential,Object providerInfo,String newPassword,securestring newSecurePassword,Boolean redirectedUserInstance,sqlConnectionString userConnectionoptions,SessionData reconnectSessionData,Boolean applyTransientFaultHandling,String accesstoken)\r\n at System.Data.sqlClient.sqlConnectionFactory.CreateConnection(DbConnectionoptions options,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,DbConnectionPool pool,DbConnection owningConnection,DbConnectionoptions userOptions)\r\n at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool,DbConnection owningObject,DbConnectionoptions options,DbConnectionoptions userOptions)\r\n at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,DbConnectionoptions userOptions,DbConnectionInternal oldConnection)\r\n at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject,DbConnectionInternal oldConnection)\r\n at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean allowCreate,Boolean onlyOneCheckConnection,DbConnectionInternal& connection)\r\n at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,taskcompletionsource`1 retry,DbConnectionInternal& connection)\r\n at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,DbConnectionInternal oldConnection,DbConnectionInternal& connection)\r\n at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,DbConnectionoptions userOptions)\r\n at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionoptions userOptions)\r\n at System.Data.sqlClient.sqlConnection.TryOpen(taskcompletionsource`1 retry)\r\n at System.Data.sqlClient.sqlConnection.open()\r\n at Dapper.sqlMapper.QueryImpl[T](IDbConnection cnn,CommandDeFinition command,Type effectiveType)+MoveNext() in /_/Dapper/sqlMapper.cs:line 1079\r\n at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n at Dapper.sqlMapper.Query[T](IDbConnection cnn,String sql,Object param,IDbTransaction transaction,Boolean buffered,Nullable`1 commandTimeout,Nullable`1 commandType) in /_/Dapper/sqlMapper.cs:line 721\r\n at BookClub.Data.BookRepository.GetAllBooks() in C:\\Users\\terry.aney\\OneDrive\\.NET Core Tutorials\\EffectiveLogging\\BookClub.Data\\BookRepository.cs:line 33\r\n at BookClub.Logic.BookLogic.GetAllBooks(Boolean callBadMethod) in C:\\Users\\terry.aney\\OneDrive\\.NET Core Tutorials\\EffectiveLogging\\BookClub.Logic\\BookLogic.cs:line 26\r\n at BookClub.API.Controllers.BookController.GetBooks(Boolean throwException) in C:\\Users\\terry.aney\\OneDrive\\.NET Core Tutorials\\EffectiveLogging\\BookClub.API\\Controllers\\BookController.cs:line 45\r\n at lambda_method(Closure,Object )\r\n at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper,ObjectMethodExecutor executor,Object controller,Object[] arguments)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker,ValueTask`1 actionResultValueTask)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterasync>g__Awaited|10_0(ControllerActionInvoker invoker,Task lastTask,State next,Scope scope,Object state,Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next,Scope& scope,Object& state,Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterasync()\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker,Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next,Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)\r\n at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint,Task requestTask,ILogger logger)\r\n at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)\r\n at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext,ISwaggerProvider swaggerProvider)\r\n at BookClub.Infrastructure.Middleware.ApiExceptionMiddleware.Invoke(HttpContext context) in C:\\Users\\terry.aney\\OneDrive\\.NET Core Tutorials\\EffectiveLogging\\BookClub.Infrastructure\\Middleware\\ApiExceptionMiddleware.cs:line 28\r\nClientConnectionId:2daf3389-b366-4b50-b1c2-651419fd247e\r\nError Number:4060,State:1,Class:11","Properties": {
"ErrorId": "0cc05d84-e36f-4e2b-b8e0-57bf5e465046","SourceContext": "BookClub.Infrastructure.Middleware.ApiExceptionMiddleware","RequestId": "8000017c-000c-fc00-b63f-84710c7967bb","RequestPath": "/api/Book","SpanId": "|303de91c-4b6f6e187de2f4f3.","TraceId": "303de91c-4b6f6e187de2f4f3","ParentId": ""
}
}
{
"Timestamp": "2021-01-25T18:07:24.8378974-06:00","Level": "Error","MessageTemplate": "An unhandled exception has occurred while executing the request.","Exception": "System.Exception: API Failure\r\n at BookClub.UI.StandardHttpMessageHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) in C:\\Users\\terry.aney\\OneDrive\\.NET Core Tutorials\\EffectiveLogging\\BookClub.UI\\StandardHttpMessageHandler.cs:line 57\r\n at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask,HttpRequestMessage request,CancellationTokenSource cts,Boolean disposeCts)\r\n at BookClub.UI.Pages.BookListModel.OnGetAsync() in C:\\Users\\terry.aney\\OneDrive\\.NET Core Tutorials\\EffectiveLogging\\BookClub.UI\\Pages\\BookList.cshtml.cs:line 27\r\n at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.NonGenericTaskHandlerMethod.Execute(Object receiver,Object[] arguments)\r\n at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()\r\n at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterasync()\r\n at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)\r\n at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next,Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterasync()\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker,Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker,Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)\r\n at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint,ILogger logger)\r\n at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware,HttpContext context,Task task)","Properties": {
"EventId": {
"Id": 1,"Name": "UnhandledException"
},"SourceContext": "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware","RequestId": "80000069-0004-fb00-b63f-84710c7967bb","RequestPath": "/BookList","SpanId": "|574e8e13-4e9d31fed64d4793.","TraceId": "574e8e13-4e9d31fed64d4793","ParentId": ""
}
}
所以它似乎匹配 SpanId
而不是 RequestId
。
所以我的问题:
- 我正在观看 Pluralsight 课程,屏幕截图似乎正确显示了
RequestId
。他的节目怎么可能是正确的RequestId
而我的节目是SpanId
? - 我创建了一个新项目,据我所知,以与存储库相同的方式实现了所有内容,但我的日志属性中没有
SpanId
、TraceId
或ParentId
。猜猜我可能错过了什么? -
SpanId
是否应该在 WebApi 和网站调用之间匹配(即上面的两个日志,第一个是 Web API,第二个是网站,并且都具有唯一的跨度/请求 ID)?或者我应该使用 CorrelationId 吗? - 一般问题,SpanId、TraceId 和 HttpContext.TraceIdentifier 之间有什么区别?我的 error.cshtml 是否应该只使用
TraceIdentifier
而不是查看 Activity.Current.Id?
更新:我在 .NET 5 中实现了我的项目。这可能是日志中缺少属性的罪魁祸首,但其余问题仍然有效。
更新:我已经找到了解决 .NET 5 中缺少的属性的方法。而且我还有 TraceId
跨越从站点到 api 的调用,但我想知道 {{1 }} 代表,我在网上不容易找到。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)