public void OnException(ExceptionContext context) { if (context.Exception != null) { var exception = context.Exception.GetInnerException(); AsyncLogger.LogException(exception, context.HttpContext.Request); var errorCode = exception.HResult; if (errorCode == 0) { errorCode = 500; } context.Result = JsonResultFormat.ResponseFail(errorCode, "Server exception."); context.ExceptionHandled = true; } }
public async Task Invoke(HttpContext context) { var sw = Stopwatch.StartNew(); try { AsyncLogger.StartRequestLog(context.TraceIdentifier, $"=> Starting request ${context.TraceIdentifier}"); await _next.Invoke(context); } catch (Exception e) { AsyncLogger.LogException(e.Message, e); } finally { sw.Stop(); Debug.WriteLine(AsyncLogger.EndRequestLog($"<= Ending request ${context.TraceIdentifier}. Total duration: {sw.ElapsedMilliseconds} ms")); } }
/// <summary> /// 将当前 <see cref="Exception"/> 对象用异步方式写入日志 /// </summary> /// <param name="exception">当前 Exception 对象</param> /// <param name="request"><see cref="HttpRequest"/>对象</param> public static void WriteLogAsync(this Exception exception, HttpRequest request) => AsyncLogger.LogException(exception, request);
/// <summary> /// 将当前 <see cref="Exception"/> 对象用异步方式写入日志 /// </summary> /// <param name="exception">当前 Exception 对象</param> public static void WriteLogAsync(this Exception exception) => AsyncLogger.LogException(exception);