private async Task LogResponse(HttpResponseMessage response) { if (response == null) throw new ArgumentNullException("response"); HttpRequestMessage request = response.RequestMessage; try { string method = request.Method.Method; string relativeUrl = request.RequestUri.AbsolutePath; Guid requestId = GetCorrelationId(request); string shortRequestId = GetShortRequestId(requestId); string logSummary = string.Format("#{3} HTTP {0} - {1} {2}", (int) response.StatusCode, method, relativeUrl, shortRequestId); string userName = GetUserName(request) ?? "<no user>"; string requestContent = await GetContentStringAsync(request.Content); string responseContent = await GetContentStringAsync(response.Content); // ReSharper disable RedundantArgumentName var logEvent = new LogEvent(request: request, response: response, requestId: requestId, shortRequestId: shortRequestId, userName: userName, userHostAddress: request.GetUserHostAddress(), requestContent: requestContent, responseContent: responseContent, summary: logSummary); // ReSharper restore RedundantArgumentName RaiseResponseCompleted(logEvent); } catch (Exception e) { Trace.TraceError(e.ToString()); RaiseError(response, e); } }
private void RaiseResponseCompleted(LogEvent logEvent) { try { Action<LogEvent> handler = ResponseCompleted; if (handler != null) handler(logEvent); } // ReSharper disable once EmptyGeneralCatchClause catch { // Ignore } }