public void OnException(ExceptionContext context) { var httpStatusCode = HttpStatusCode.InternalServerError; if (context.Exception is ArgumentNullException) { httpStatusCode = HttpStatusCode.BadRequest; } var response = new WebApiResponse { Success = false, Error = new ErrorDescriber { Code = httpStatusCode.ToString(), Description = context.Exception.GetErrorMessage() } }; context.Result = new ObjectResult(response) { StatusCode = (int)HttpStatusCode.OK, DeclaredType = typeof(WebApiResponse) }; if (httpStatusCode != HttpStatusCode.BadRequest) { var logServer = IoCExtensions.GetService <LogService>(); logServer.LogException(context.Exception, "WebApiException"); } }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var sw = Stopwatch.StartNew(); var response = await base.SendAsync(request, cancellationToken); sw.Stop(); var logService = IoCExtensions.GetService <LogService>(); logService.LogAPIRequest(m_logCode, new APIRequestInfo { Method = request.Method.ToString(), Url = request.RequestUri.AbsoluteUri, Paras = request.Content == null ? null : await request.Content.ReadAsStringAsync(), Spend = (int)sw.ElapsedMilliseconds, IsSuccess = response.IsSuccessStatusCode }); return(response); }