/// <summary> /// 页面操作处理入口 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { CurrentPageName = GetFriendPageUrl(); LogRecorder.BeginMonitor(CurrentPageName); LogRecorder.MonitorTrace(Request.Url.AbsolutePath); LogRequestInfo(); try { ProcessRequest(); } catch (Exception exception) { LogRecorder.EndStepMonitor(); LogRecorder.BeginStepMonitor("Exception"); LogRecorder.MonitorTrace(exception.Message); LogRecorder.Exception(exception); Debug.WriteLine(exception); OnFailed(exception); LogRecorder.EndStepMonitor(); } LogRecorder.BeginStepMonitor("Result"); OnResult(); LogRecorder.EndStepMonitor(); LogRecorder.EndMonitor(); }
/// <summary> /// 开始计数 /// </summary> /// <returns></returns> private static void EndMonitor(RouteData data) { if (!LogRecorder.LogMonitor) { return; } try { if (!string.IsNullOrWhiteSpace(data.Uri.Query)) { LogRecorder.MonitorTrace($"Query:{data.Uri.Query}"); } if (!string.IsNullOrWhiteSpace(data.Form)) { LogRecorder.MonitorTrace($"Form:{data.Form}"); } if (!string.IsNullOrWhiteSpace(data.Context)) { LogRecorder.MonitorTrace("Context:" + data.Context); } LogRecorder.MonitorTrace($"Status : {data.Status}"); LogRecorder.MonitorTrace($"Result:{data.ResultMessage}"); } catch (Exception e) { LogRecorder.MonitorTrace(e.Message); LogRecorder.Exception(e); } finally { LogRecorder.EndMonitor(); } }
private Task <HttpResponseMessage> DoEnd(Task <HttpResponseMessage> t1, HttpRequestMessage request, CancellationToken cancellationToken) { try { t1.Wait(cancellationToken); HttpResponseMessage result; if (t1.IsCanceled) { LogRecorder.MonitorTrace("操作被取消"); result = request.ToResponse(ApiResult.Error(-7, "服务器正忙", "操作被取消")); LogRecorder.EndMonitor(); return(Task <HttpResponseMessage> .Factory.StartNew(() => result, cancellationToken)); } if (t1.IsFaulted) { LogRecorder.MonitorTrace(t1.Exception?.Message); LogRecorder.Exception(t1.Exception); result = request.ToResponse(ApiResult.Error(-1, "未知错误", t1.Exception?.Message)); } else { result = t1.Result; } return(Task <HttpResponseMessage> .Factory.StartNew(delegate { OnEnd(request, result, cancellationToken); return result; }, cancellationToken)); } finally { IocHelper.DisposeScope(); } }
Task <HttpResponseMessage> DoEnd(Task <HttpResponseMessage> t1, HttpRequestMessage request, CancellationToken cancellationToken) { t1.Wait(cancellationToken); HttpResponseMessage result; if (t1.IsCanceled) { LogRecorder.MonitorTrace("操作被取消"); result = request.ToResponse(ApiResult.Error(ErrorCode.Ignore, "服务器正忙", "操作被取消")); LogRecorder.EndAllStepMonitor(); LogRecorder.EndMonitor(); return(Task <HttpResponseMessage> .Factory.StartNew(() => result, cancellationToken)); } if (t1.IsFaulted) { LogRecorder.MonitorTrace(t1.Exception?.Message); LogRecorder.Exception(t1.Exception); result = request.ToResponse(ApiResult.Error(ErrorCode.UnknowError, "未知错误", t1.Exception?.Message)); } else { result = t1.Result; } /* * result.ContinueWith((task, state) => , null, * TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.ExecuteSynchronously); */ return(Task <HttpResponseMessage> .Factory.StartNew(() => { OnEnd(request, result, cancellationToken); return result; }, cancellationToken)); }
/// <summary> /// 准备 /// </summary> /// <param name="message">当前消息</param> /// <returns></returns> Task IMessageMiddleware.OnEnd(IInlineMessage message) { if (LogRecorder.LogMonitor) { LogRecorder.MonitorTrace("[State] {0} [Result]{1}", message.State, message.Result); if (message.Trace != null) { LogRecorder.MonitorTrace(() => $"[Trace] {message.Trace.ToJson()}"); } var root = LogRecorder.EndMonitor(); LogRecorder.TraceMonitor(root); } return(Task.CompletedTask); }
/// <summary> /// 结束时的处理 /// </summary> void IHttpSystemHandler.OnEnd(HttpRequestMessage request, CancellationToken cancellationToken, HttpResponseMessage response) { if (LogRecorder.LogMonitor) { try { Task <string> task = response.Content.ReadAsStringAsync(); task.Wait(cancellationToken); LogRecorder.MonitorTrace($"Result:{task.Result}"); } catch (Exception e) { LogRecorder.MonitorTrace($"Result:{e.Message}"); } LogRecorder.EndMonitor(); } }
/// <summary> /// 开始计数 /// </summary> /// <returns></returns> private static void EndMonitor(RouteData data) { if (!LogRecorder.LogMonitor) { return; } try { LogRecorder.MonitorTrace($"Result:{data.ResultMessage}"); } catch (Exception e) { LogRecorder.MonitorTrace(e.Message); LogRecorder.Exception(e); } finally { LogRecorder.EndMonitor(); } }