![](/img/trans.png)
[英]An unhandled exception occurred while processing the request- ASP .NET Core
[英]“Unhandled” exception in ASP .NET Core 3.1
我的日志文件中出现异常。 问题是我的 Startup.Configure() 方法中有一个全局异常处理程序:
app.UseExceptionHandler("/Home/Error");
它实际上是被调用的。 所以异常被处理。 如何禁止将此消息放入日志文件?
谢谢!
ERROR|Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware|An unhandled exception has occurred while executing the request. Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Request body too large.
at Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw(RequestRejectionReason reason)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.OnReadStarting()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.TryStart()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.ReadAsyncInternal(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)
at Microsoft.AspNetCore.WebUtilities.BufferedReadStream.EnsureBufferedAsync(Int32 minCount, CancellationToken cancellationToken)
at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at Microsoft.AspNetCore.WebUtilities.StreamHelperExtensions.DrainAsync(Stream stream, ArrayPool`1 bytePool, Nullable`1 limit, CancellationToken cancellationToken)
at Microsoft.AspNetCore.WebUtilities.MultipartReader.ReadNextSectionAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Features.FormFeature.InnerReadFormAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.ModelBinding.FormValueProviderFactory.AddValueProviderAsync(ValueProviderFactoryContext context)
at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ActionContext actionContext, IList`1 factories)
at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.TryCreateAsync(ActionContext actionContext, IList`1 factories)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)```
可靠地记录异常是全局异常处理程序实际上可以做的一件事。 之后它应该总是让异常 go 开启。 对于日志记录,它必须捕获广泛,这意味着它会捕获致命异常,并且应该始终让 go 捕获。 全局处理程序是迟到的方式来修复任何东西了。 甚至可能再正确记录任何内容都为时已晚,因为那时大多数内容都已超出 scope 的范围。
要知道如何正确处理它,我们首先需要对其进行分类。 我使用这个分类系统: https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/
BadHttpRequestException
看起来像一个愚蠢的异常。 那些你永远不应该忽视,最好永远不要抓住的人。 那些你应该修复的。
然而,当我们处理网络时,它有可能是一个外生或令人烦恼的异常。 如果它是其中之一,您应该尽可能准确地抓住它。 还给调用代码一些提示出了点问题,所以预期的结果不存在。 我曾经为复制的 TryParse 编写了这个示例:
//Parse throws ArgumentNull, Format and Overflow Exceptions.
//And they only have Exception as base class in common, but identical handling code (output = 0 and return false).
bool TryParse(string input, out int output){
try{
output = int.Parse(input);
}
catch (Exception ex){
if(ex is ArgumentNullException ||
ex is FormatException ||
ex is OverflowException){
//these are the exceptions I am looking for. I will do my thing.
output = 0;
return false;
}
else{
//Not the exceptions I expect. Best to just let them go on their way.
throw;
}
}
//I am pretty sure the Exception replaces the return value in exception case.
//So this one will only be returned without any Exceptions, expected or unexpected
return true;
}
Parse 中令人烦恼的部分? 那些被捕获并通过 bool 返回进行通信。 我不得不捕获宽泛或一式三份的处理代码,所以我尽我所能is
检查以稍后过滤。
对于一般体面的做法,我确实有一篇文章。 连同上面的分类,它是我所有异常处理决策的核心。
我建议您不要禁用日志记录错误,因为它是查找用户问题的主要方法之一。 无论如何,如果您确实想禁用错误日志记录,您可以从配置中执行此操作。 默认配置文件是“appsettings.json”文件:
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": 6,
"Microsoft.Hosting.Lifetime": 6
}
}
这将禁止来自 Microsoft 或 Hosting 的任何错误消息。 请注意,在开发环境中还使用了“appsettings.Development.json”文件,您可能也希望在那里禁用日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.