由慢速客户端引起的BadHttpRequestException出现在我的应用程序见解中

我的API的一个调用者有时会遇到一些性能问题,这些问题最终会使他们的CPU占用100%的内存,并且我的API由于请求超时而拒绝了他们的某些调用(HTTP 408)。这对我来说很好,因为我认为拒绝来电是一种保护。但是,由于超时被作为异常进行跟踪,因此它还会在Application Insights中触发警报。这些请求是POST。

我该怎么做才能防止这种情况?我研究了Application Insights的过滤配置,但是找不到任何可以过滤此特定异常的内容。我不想仅仅因为这个而过滤太多的日志条目。

此外,我可以在Stacktrace中看到Swashbuckle。在我的ConfigureServices方法中重新安排内容是否有帮助?当前在AddApplicationInsightsTelemetry之后配置了Swashbuckle。

我使用在.NET 4.7.2上运行的ASP.NET Core 2.2。通过 microsoft.ApplicationInsights.AspNetCore

添加了Application Insights。

异常详细信息:

Message          Reading the request body timed out due to data arriving too slowly. See MinRequestBodyDataRate.    
Exception type   microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException   
Failed method    Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware+<Invoke>d__5.MoveNext

microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:
   at microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw (microsoft.AspNetCore.Server.Kestrel.Core,Version=2.2.0.0,Culture=neutral,PublicKeyToken=adb9793829ddae60)
   at microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody+<PumpAsync>d__4.MoveNext (microsoft.AspNetCore.Server.Kestrel.Core,PublicKeyToken=adb9793829ddae60)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (mscorlib,Version=4.0.0.0,PublicKeyToken=b77a5c561934e089)
   at System.IO.Pipelines.PipeCompletion.ThrowLatchedException (System.IO.Pipelines,Version=4.0.0.1,PublicKeyToken=cc7b13ffcd2ddd51)
   at System.IO.Pipelines.Pipe.GetReadResult (System.IO.Pipelines,PublicKeyToken=cc7b13ffcd2ddd51)
   at System.IO.Pipelines.Pipe.GetReadAsyncResult (System.IO.Pipelines,PublicKeyToken=cc7b13ffcd2ddd51)
   at microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody+<ReadAsync>d__27.MoveNext (microsoft.AspNetCore.Server.Kestrel.Core,PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.Taskawaiter.HandleNonSuccessAndDebuggerNotification (mscorlib,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream+<ReadAsyncInternal>d__21.MoveNext (microsoft.AspNetCore.Server.Kestrel.Core,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.WebUtilities.FileBufferingReadStream+<ReadAsync>d__35.MoveNext (microsoft.AspNetCore.WebUtilities,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.WebUtilities.StreamHelperExtensions+<DrainAsync>d__3.MoveNext (microsoft.AspNetCore.WebUtilities,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Mvc.Formatters.JsonInputFormatter+<ReadRequestBodyAsync>d__17.MoveNext (microsoft.AspNetCore.Mvc.Formatters.Json,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder+<BindmodelAsync>d__7.MoveNext (microsoft.AspNetCore.Mvc.Core,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder+<BindmodelAsync>d__11.MoveNext (microsoft.AspNetCore.Mvc.Core,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider+<>c__DisplayClass0_0+<<CreateBinderDelegate>g__Bind|0>d.MoveNext (microsoft.AspNetCore.Mvc.Core,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Mvc.Internal.ControlleractionInvoker+<InvokeInnerFilterAsync>d__13.MoveNext (microsoft.AspNetCore.Mvc.Core,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResourceFilter>d__23.MoveNext (microsoft.AspNetCore.Mvc.Core,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow (microsoft.AspNetCore.Mvc.Core,PublicKeyToken=adb9793829ddae60)
   at microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next (microsoft.AspNetCore.Mvc.Core,PublicKeyToken=adb9793829ddae60)
   at microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeFilterPipelineAsync>d__18.MoveNext (microsoft.AspNetCore.Mvc.Core,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeAsync>d__16.MoveNext (microsoft.AspNetCore.Mvc.Core,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Routing.EndpointMiddleware+<Invoke>d__3.MoveNext (microsoft.AspNetCore.Routing,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Routing.EndpointRoutingMiddleware+<Invoke>d__6.MoveNext (microsoft.AspNetCore.Routing,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Authentication.Authenticationmiddleware+<Invoke>d__6.MoveNext (microsoft.AspNetCore.Authentication,PublicKeyToken=b77a5c561934e089)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware+<Invoke>d__5.MoveNext (Swashbuckle.AspNetCore.SwaggerUI,Version=4.0.1.0,PublicKeyToken=4232c99127b3c254)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (mscorlib,PublicKeyToken=b77a5c561934e089)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware+<Invoke>d__6.MoveNext (Swashbuckle.AspNetCore.Swagger,PublicKeyToken=62657d7474907593)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (mscorlib,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Server.IISIntegration.IISMiddleware+<Invoke>d__13.MoveNext (microsoft.AspNetCore.Server.IISIntegration,PublicKeyToken=b77a5c561934e089)
   at microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+<ProcessRequests>d__185`1.MoveNext (microsoft.AspNetCore.Server.Kestrel.Core,PublicKeyToken=adb9793829ddae60)

lulou215 回答:由慢速客户端引起的BadHttpRequestException出现在我的应用程序见解中

要过滤这种类型的异常"Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException",可以使用ITelemetryProcessor

在您的.net核心项目中,添加一个名为MyTelemetryProcessor的类,以下是该类的详细信息:

public class MyTelemetryProcessor: ITelemetryProcessor
{
    private ITelemetryProcessor Next { get; set; }

    public MyTelemetryProcessor(ITelemetryProcessor next)
    {
        this.Next = next;
    }

    public void Process(ITelemetry telemetry)
    {

        ExceptionTelemetry err1 = telemetry as ExceptionTelemetry;

        //you can use exception type or the error message to filter
        if(err1 !=null && err1.Exception.GetType().ToString().Contains("the exception type you want to filter"))
        {
            return;
        }

        if (err1 == null)
        {
            this.Next.Process(telemetry);

        }

        if (err1 != null)
        {
            this.Next.Process(err1);
        }
    }
}

然后在Startup.cs-> ConfigureServices()方法中,添加以下代码:

services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetryProcessor<MyTelemetryProcessor>();
本文链接:https://www.f2er.com/3163670.html

大家都在问