public override void OnException(HttpActionExecutedContext context) { Global.OutPrint("Uncaught error: " + context.Exception.Message, "com.LoanTek.API.Pricing.Clients", context.Request.RequestUri.AbsoluteUri, String.Empty, SimpleLogger.LogLevelType.ERROR); AApiResponse apiResponse = new AApiResponse(); apiResponse.ApiEndPoint = context.Request.RequestUri.AbsoluteUri; apiResponse.Message = "An error occured: " + context.Exception.Message + ". "; apiResponse.Message += "Please contact [email protected] if you continue to see this exception."; string ip = ClientInfo.GetIPAddress(context.Request); if (ip.StartsWith("::1") || ip.StartsWith("10.0")) { string s = JsonConvert.SerializeObject(apiResponse, Global.JsonSettings); s += "<hr /><h5>Exception:</h5><ul><li>Message:" + context.Exception.Message + "</li>"; s += "<li>Source:" + context.Exception.Source + "</li>"; s += "<li>StackTrace:" + context.Exception.StackTrace + "</li></ul>"; context.Response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, s); } else { context.Response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, apiResponse, new JsonMediaTypeFormatter() { SerializerSettings = Global.JsonSettings }); } }
public override void OnActionExecuted(HttpActionExecutedContext actionContext) { try { new Task(() => { AApiResponse apiResponse = new AApiResponse(); AApiRequest request = actionContext.Request.Properties[Global.ARequestPropertyName] as AApiRequest; if (request != null) { //if the request Id is zero, then the save to the data context may not have completed. Let's give it a little extra time... int counter = 0; while (request.Id == 0 && counter++ < 20) { Thread.Sleep(50); } apiResponse.Id = request.Id; apiResponse.ClientDefinedIdentifier = request.ClientDefinedIdentifier; } var objContent = (actionContext.ActionContext.Response.Content as ObjectContent); Requests.IResponse content = objContent?.Value as Requests.IResponse; if (content != null) { apiResponse.ApiEndPoint = content.ApiEndPoint; apiResponse.Message = content.LoanTekDefinedIdentifier.ToString(); apiResponse.ExecutionTimeInMillisec = content.ExecutionTimeInMillisec; apiResponse.HttpStatusCode = actionContext.ActionContext.Response.StatusCode; apiResponse.Save(); } }).Start(); } catch (Exception ex) { Global.OutPrint("ERROR: " + ex.Message, new SimpleLogger.LocationObject(this, "OnActionExecuted"), SimpleLogger.LogLevelType.ERROR); } }