如何将中间件与拦截器一起使用?

时间:2019-08-23 16:26:39

标签: c# .net oop logging middleware

中间件足以检测异常请求/响应,但不足以处理从流程起点到端点的所有异常。例如:


    public class HttpStatusCodeExceptionMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly ILogger<HttpStatusCodeExceptionMiddleware> _logger;

        public HttpStatusCodeExceptionMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
        {
            _next = next ?? throw new ArgumentNullException(nameof(next));
            _logger = loggerFactory?.CreateLogger<HttpStatusCodeExceptionMiddleware>() ?? throw new ArgumentNullException(nameof(loggerFactory));
        }

        public async Task Invoke(HttpContext context)
        {
            string message = "";
            try
            {
                await _next.Invoke(context);
            }
           catch (LoginException ex)
            {
                message = ex.Message;

                StackTrace st = new StackTrace(true);
                int num = 0;
                var list = new List<string>();
                for (int i = 0; i < st.FrameCount; i++) // 4 frame var

                {

                  ///....
                    list.Add(strBuilder.ToString());

                }

                if (!context.Response.HasStarted)
                {
                    context.Response.ContentType = "application/json";
                    var response = new ExceptionModel(message);
                    // var json = JsonConvert.SerializeObject(response.getMessage());

                    var json = JsonConvert.SerializeObject(list);
                    await context.Response.WriteAsync(json);
                }
            }
            catch (DivideByZeroException ex)
            {
                message = ex.Message;

                StackTrace st = new StackTrace(true);


                int num = 0;
                var list = new List<string>();
                for (int i = 0; i < st.FrameCount; i++) // 4 frame var

                {
                    //.....

                    list.Add(strBuilder.ToString());

                }

                if (!context.Response.HasStarted)
                {
                    context.Response.ContentType = "application/json";
                    var response = new ExceptionModel(message);
                    // var json = JsonConvert.SerializeObject(response.getMessage());

                    var json = JsonConvert.SerializeObject(list);
                    await context.Response.WriteAsync(json);
                }
            }
        }
    }


everything looks good but if I check error list as json . I can easly said that I can not see throwing error result. At the end and of the day I have to see
all steps of throwing method. 

[
  " Metod: Void MoveNext() File: C:\\Projects\\PlayGround\\Enrichment\\Enrichment\\WebAppEnrichment\\Extensions\\HttpStatusCodeExceptionMiddleware.cs Row: 39 Message 1 : Attempted to divide by zero.",
  " Metod: Void Start[TStateMachine](TStateMachine ByRef) File:  Row: 0 Message 2 : Attempted to divide by zero.",
  " Metod: System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext) File:  Row: 0 Message 3 : Attempted to divide by zero.",
  " Metod: System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext) File:  Row: 0 Message 4 : Attempted to divide by zero.",
  " Metod: Void MoveNext() File:  Row: 0 Message 5 : Attempted to divide by zero.",
  " Metod: Void Start[TStateMachine](TStateMachine ByRef) File:  Row: 0 Message 6 : Attempted to divide by zero.",
  " Metod: System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext) File:  Row: 0 Message 7 : Attempted to divide by zero.",
  " Metod: System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext) File:  Row: 0 Message 8 : Attempted to divide by zero.",
  " Metod: System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext) File:  Row: 0 Message 9 : Attempted to divide by zero.",
  " Metod: System.Threading.Tasks.Task ProcessRequestAsync(Context) File:  Row: 0 Message 10 : Attempted to divide by zero.",
  " Metod: Void MoveNext() File:  Row: 0 Message 11 : Attempted to divide by zero.",
  " Metod: Void Start[TStateMachine](TStateMachine ByRef) File:  Row: 0 Message 12 : Attempted to divide by zero.",
  " Metod: System.Threading.Tasks.Task`1[System.Boolean] ProcessRequestAsync() File:  Row: 0 Message 13 : Attempted to divide by zero.",
  " Metod: Void MoveNext() File:  Row: 0 Message 14 : Attempted to divide by zero.",
  " Metod: Void Start[TStateMachine](TStateMachine ByRef) File:  Row: 0 Message 15 : Attempted to divide by zero.",
  " Metod: System.Threading.Tasks.Task HandleRequest(Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext) File:  Row: 0 Message 16 : Attempted to divide by zero.",
  " Metod: Void <HandleRequest>b__26_0(System.Object) File:  Row: 0 Message 17 : Attempted to divide by zero.",
  " Metod: Void RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) File:  Row: 0 Message 18 : Attempted to divide by zero.",
  " Metod: Boolean Dispatch() File:  Row: 0 Message 19 : Attempted to divide by zero."
]

HttpStatusCodeExceptionMiddleware.cs行:39消息1:试图除以零。”不是正确的地址。因为它是从上一类抛出的。我必须看到:  enter image description here

不用担心我正在尝试的方法名称。我该如何返回看到实际发生的错误而不是最后一次发生错误的位置。我通过使用Microsoft统一拦截器创建了此结果。  我如何将拦截器和中间件结合在一起以查看最深的细节,而无需编写try-catch shit。谢谢。

0 个答案:

没有答案