public void OnAfterRequest(NancyContext context, IDictionary <string, object> logData) { var request = context.Request; var response = context.Response; if (context.Items.ContainsKey(NancyServiceBootstrapper.StartTimeString)) { var startTime = (DateTime)context.Items[NancyServiceBootstrapper.StartTimeString]; var endTime = DateTime.UtcNow; logData[NancyServiceBootstrapper.StartTimeString] = startTime; logData[NancyServiceBootstrapper.EndTimeString] = endTime; logData["CallDuration"] = (int)endTime.Subtract(startTime).TotalMilliseconds; } var correlationId = string.Empty; if (context.Items.ContainsKey(CorrelationIdString)) { correlationId = (string)context.Items[CorrelationIdString]; } string responseBody; using (var stream = new MemoryStream()) { response.Contents(stream); stream.Position = 0; using (var streamReader = new StreamReader(stream)) { responseBody = streamReader.ReadToEnd(); } } var responseIsJson = response.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(responseBody); var bodyObject = responseIsJson ? JsonConvert.DeserializeObject(responseBody) : responseBody; //If the body is too large, just log the first x characters (and don't return the JSON object if applicable) if (_loggedBodySizeLimit > -1 && responseBody.Length > _loggedBodySizeLimit) { bodyObject = responseBody.Substring(0, _loggedBodySizeLimit); } logData["Host"] = Environment.MachineName; logData["Body"] = bodyObject; logData["StatusCode"] = response.StatusCode; logData["ResponseReason"] = response.ReasonPhrase; _logger.Info(new BaseMessage { Message = request.Path, CorrelationId = correlationId, Info = logData }); context.Response.Headers.Add("CorrelationId", correlationId); }
public void OnAfterRequest(NancyContext context, IDictionary <string, object> logData) { _logger.Info(logData); }