private async Task InvokeHttpCommonAsync(HttpContext context) { var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); ConfigTrace(context); try { await Next(context); } catch (Exception ex) { if (!context.Items.ContainsKey(BasicConfig.HttpReportsGlobalException)) { context.Items.Add(BasicConfig.HttpReportsGlobalException, ex); } throw ex; } finally { stopwatch.Stop(); context.Items.Add(BasicConfig.HttpReportsTraceCost, stopwatch.ElapsedMilliseconds); context.Items.Add(BasicConfig.HttpReportsRequestBody, ""); context.Items.Add(BasicConfig.HttpReportsResponseBody, ""); if (!string.IsNullOrEmpty(context.Request.Path)) { InvokeProcesser.Process(context); } } }
private async Task InvokeHttpAsync(HttpContext context) { if (FilterRequest(context)) { await Next(context); return; } var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); ConfigTrace(context); string requestBody = await GetRequestBodyAsync(context); var originalBodyStream = context.Response.Body; var responseMemoryStream = new MemoryStream(); try { context.Response.Body = responseMemoryStream; await Next(context); } catch (Exception ex) { if (!context.Items.ContainsKey(BasicConfig.HttpReportsGlobalException)) { context.Items.Add(BasicConfig.HttpReportsGlobalException, ex); } throw ex; } finally { stopwatch.Stop(); string responseBody = await GetResponseBodyAsync(context); context.Items.Add(BasicConfig.HttpReportsTraceCost, stopwatch.ElapsedMilliseconds); context.Items.Add(BasicConfig.HttpReportsRequestBody, requestBody); context.Items.Add(BasicConfig.HttpReportsResponseBody, responseBody); if (responseMemoryStream.CanRead && responseMemoryStream.CanSeek) { await responseMemoryStream.CopyToAsync(originalBodyStream); responseMemoryStream.Dispose(); } if (!string.IsNullOrEmpty(context.Request.Path)) { InvokeProcesser.Process(context); } } }
private async Task InvokeHttpAsync(HttpContext context) { if (FilterRequest(context)) { await _next(context); return; } var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); ConfigTrace(context); string requestBody = await GetRequestBodyAsync(context); var originalBodyStream = context.Response.Body; var responseMemoryStream = new MemoryStream(); try { context.Response.Body = responseMemoryStream; await _next(context); } finally { stopwatch.Stop(); string responseBody = await GetResponseBodyAsync(context); context.Items.Add(BasicConfig.HttpReportsRequestBody, requestBody); context.Items.Add(BasicConfig.HttpReportsResponseBody, responseBody); await responseMemoryStream.CopyToAsync(originalBodyStream); responseMemoryStream.Dispose(); if (!string.IsNullOrEmpty(context.Request.Path)) { InvokeProcesser.Process(context, stopwatch); } } }
private async Task InvokeGrpcAsync(HttpContext context) { var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); ConfigTrace(context); try { await Next(context); } finally { stopwatch.Stop(); string requestBody = string.Empty; string responseBody = string.Empty; if (context.Items.ContainsKey(BasicConfig.HttpReportsGrpcRequest)) { requestBody = System.Text.Json.JsonSerializer.Serialize(context.Items[BasicConfig.HttpReportsGrpcRequest], JsonSetting); } if (context.Items.ContainsKey(BasicConfig.HttpReportsGrpcResponse)) { responseBody = System.Text.Json.JsonSerializer.Serialize(context.Items[BasicConfig.HttpReportsGrpcResponse], JsonSetting); } context.Items.Add(BasicConfig.HttpReportsTraceCost, stopwatch.ElapsedMilliseconds); context.Items.Add(BasicConfig.HttpReportsRequestBody, requestBody); context.Items.Add(BasicConfig.HttpReportsResponseBody, responseBody); if (!string.IsNullOrEmpty(context.Request.Path)) { InvokeProcesser.Process(context); } } }
private async Task InvokeGrpcAsync(HttpContext context) { var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); ConfigTrace(context); try { await _next(context); } finally { stopwatch.Stop(); string requestBody = string.Empty; string responseBody = string.Empty; if (context.Items.ContainsKey(BasicConfig.HttpReportsGrpcRequest)) { requestBody = JsonConvert.SerializeObject(context.Items[BasicConfig.HttpReportsGrpcRequest]); } if (context.Items.ContainsKey(BasicConfig.HttpReportsGrpcResponse)) { responseBody = JsonConvert.SerializeObject(context.Items[BasicConfig.HttpReportsGrpcResponse]); } context.Items.Add(BasicConfig.HttpReportsRequestBody, requestBody); context.Items.Add(BasicConfig.HttpReportsResponseBody, responseBody); if (!string.IsNullOrEmpty(context.Request.Path)) { InvokeProcesser.Process(context, stopwatch); } } }