protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var apiLogEntry = CreateApiLogEntryWithRequestData(request); if (request.Content != null) { await request.Content.ReadAsStringAsync() .ContinueWith(task => { apiLogEntry.RequestContentBody = task.Result; }, cancellationToken); } return(await base.SendAsync(request, cancellationToken) .ContinueWith(task => { HttpResponseMessage response = new HttpResponseMessage(); try { response = task.Result; apiLogEntry.ResponseStatusCode = (int)response.StatusCode; apiLogEntry.ResponseTimestamp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss", new CultureInfo("en-US")); if (task.Exception == null) { // Update the API log entry with response info if (response.Content != null) { apiLogEntry.ResponseContentBody = response.Content.ReadAsStringAsync().Result; apiLogEntry.ResponseContentType = response.Content.Headers.ContentType.MediaType; apiLogEntry.ResponseHeaders = SerializeHeaders(response.Content.Headers); } } } finally { // Save the API log to the database : Log Request DSELog.TracingLog(apiLogEntry); } return response; }, cancellationToken)); }
public override void Log(ExceptionLoggerContext context) { // Trace.TraceError(context.ExceptionContext.Exception.ToString()); var contextBase = ((HttpContextBase)context.Request.Properties["MS_HttpContext"]); var controller = context.Request.GetRouteData().Values["controller"]; var acction = context.Request.GetRouteData().Values["action"]; ERROR_LOG_WS data = new ERROR_LOG_WS() { CLIENT_IP = contextBase.Request.UserHostAddress, SERVER_IP = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork).ToString(), ERROR_MESSAGE = context.ExceptionContext.Exception.ToString(), LOG_LEVEL = "Error", SERVICE_NAME = string.Format("{0}/{1}", controller, acction), SERVICE_TYPE = "I" }; DSELog.ExceptionLog(data); }