public override async Task Invoke(IOwinContext context) { var requestData = new RequestData(); var responseData = new ResponseData(); var requestStream = await CreateSeekableStream(context.Request.Body); var responseStream = context.Response.Body; context.Request.Body = requestStream; using (var copyStream = new MemoryStream()) { try { context.Response.Body = copyStream; requestData = GetRequestData(context.Request); await Next.Invoke(context); responseData = GetResponseData(context.Response); _requestLogger.Log(requestData, responseData); } catch (Exception ex) { _requestLogger.LogError(requestData, responseData, ex); } requestStream.Seek(0, SeekOrigin.Begin); copyStream.Seek(0, SeekOrigin.Begin); await copyStream.CopyToAsync(responseStream); } }
public override void OnError(HttpContextBase context) { var requestData = ExtractRequestData(context.Request); var responseData = ExtractResponseData(context.Response); var ex = context.Server.GetLastError(); _requestLogger.LogError(requestData, responseData, ex); }
public void LogError_Should_Write_Exception_To_SystemConsole() { var requestData = new RequestData(); var responseData = new ResponseData(); var exception = new Exception(); _requestLogger.LogError(requestData, responseData, exception); _systemConsole.Verify(x => x.WriteError(exception), Times.Once); }
public void LogError_Should_Write_Exception_To_TraceListener() { var requestData = new RequestData(); var responseData = new ResponseData(); var exception = new Exception(); _requestLogger.LogError(requestData, responseData, exception); _traceListener.Verify(x => x.WriteError(exception), Times.Once); }
private static Func <NancyContext, Exception, dynamic> GetLogErrorHook(IRequestLogger requestLogger) { return((ctx, ex) => { var requestData = ExtractRequestData(ctx.Request); var responseData = ExtractResponseData(ctx.Response); requestLogger.LogError(requestData, responseData, ex); return ctx.Response; }); }