public async Task <object> Convert(HttpContext context) { byte[] bufferBytes = new byte[1024]; string strRequestBody = null; //尝试获取请求内容和响应内容 if (context.Request != null && context.Request.Body != null && context.Request.Body.CanRead) { using (MemoryStream requestStream = new MemoryStream()) { List <byte> requestBytes = new List <byte>(); //context.Request.Body.Position = 0; await context.Request.Body.CopyToAsync(requestStream); requestStream.Position = 0; long totalLength = 0; while (true) { int bufSize = 1024; if (totalLength + 1024 > _maxRequestLength) { bufSize = (int)(_maxRequestLength - totalLength); } var length = await requestStream.ReadAsync(bufferBytes, 0, bufSize); totalLength = totalLength + length; requestBytes.AddRange(bufferBytes.Take(length)); if (length != 1024) { break; } } strRequestBody = UTF8Encoding.UTF8.GetString(requestBytes.ToArray()); } } //取出存储在上下文Item中的异常 var ex = (Exception)context.Items["ExecuteException"]; CommonLogLocalContent content = new CommonLogLocalContent() { RequestUri = context.Request.Path.Value, ActionName = "", RequestBody = strRequestBody, Message = $"Unhandle Error,\nmessage:{ex.Message},\nstacktrace:{ex.StackTrace}" }; return(await Task.FromResult(content)); }
public async Task <object> Do(ExceptionContext context) { byte[] bufferBytes = new byte[1024]; string strRequestBody = null; //尝试获取请求内容和响应内容 if (context.HttpContext.Request != null && context.HttpContext.Request.Body != null && context.HttpContext.Request.Body.CanRead) { using (MemoryStream requestStream = new MemoryStream()) { List <byte> requestBytes = new List <byte>(); //context.HttpContext.Request.Body.Position = 0; await context.HttpContext.Request.Body.CopyToAsync(requestStream); requestStream.Position = 0; long totalLength = 0; while (true) { int bufSize = 1024; if (totalLength + 1024 > _maxRequestLength) { bufSize = (int)(_maxRequestLength - totalLength); } var length = await requestStream.ReadAsync(bufferBytes, 0, bufSize); totalLength = totalLength + length; requestBytes.AddRange(bufferBytes.Take(length)); if (length != 1024) { break; } } strRequestBody = UTF8Encoding.UTF8.GetString(requestBytes.ToArray()); } } CommonLogLocalContent content = new CommonLogLocalContent() { RequestUri = context.HttpContext.Request.Path.Value, ActionName = context.ActionDescriptor.DisplayName.Split("(")[0].Trim(), Message = $"Unhandle Error,\nmessage:{context.Exception.Message},\nstacktrace:{context.Exception.StackTrace}", RequestBody = strRequestBody, ResponseBody = "" }; return(await Task.FromResult(content)); }
public async Task <object> Convert(HttpContextData data) { byte[] bufferBytes = new byte[1024]; string strRequestBody = null; string strResponseBody = null; //尝试获取请求内容和响应内容 if (data.Request != null) { using (data.Request) { List <byte> requestBytes = new List <byte>(); long totalLength = 0; while (true) { int bufSize = 1024; if (totalLength + 1024 > _maxRequestLength) { bufSize = (int)(_maxRequestLength - totalLength); } var length = await data.Request.ReadAsync(bufferBytes, 0, bufSize); totalLength = totalLength + length; requestBytes.AddRange(bufferBytes.Take(length)); if (length != 1024) { break; } } strRequestBody = UTF8Encoding.UTF8.GetString(requestBytes.ToArray()); } } if (data.Response != null) { using (data.Response) { List <byte> responseBytes = new List <byte>(); long totalLength = 0; while (true) { int bufSize = 1024; if (totalLength + 1024 > _maxResponseLength) { bufSize = (int)(_maxResponseLength - totalLength); } var length = await data.Response.ReadAsync(bufferBytes, 0, bufSize); totalLength = totalLength + length; responseBytes.AddRange(bufferBytes.Take(length)); if (length != 1024) { break; } } strResponseBody = UTF8Encoding.UTF8.GetString(responseBytes.ToArray()); } } string strActionName = string.Empty; CommonLogLocalContent content = new CommonLogLocalContent() { RequestUri = "", ActionName = strActionName, RequestBody = strRequestBody, ResponseBody = strResponseBody, Message = "" }; return(await Task.FromResult(content)); }