/// <summary> /// 服务端处理 /// </summary> /// <typeparam name="TRequest">请求类型</typeparam> /// <typeparam name="TResponse">响应类型</typeparam> /// <param name="request">请求对象</param> /// <param name="context">服务回调上下文</param> /// <param name="continuation">服务方法</param> /// <returns>响应任务</returns> public override Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod <TRequest, TResponse> continuation) { var stop = new Stopwatch(); stop.Start(); var path = context.GetHttpContext().Request.Path.Value; var re = continuation(request, context); stop.Stop(); var msg = $"GRpc请求:{path} 耗时:{stop.ElapsedMilliseconds}ms"; string eventId = null; if (theOperation != null) { if (string.IsNullOrWhiteSpace(theOperation.EventId)) { theOperation.EventId = context.GetHttpContext().Request.GetEventId(); } eventId = theOperation.EventId; } switch (options.LogLevel) { case LogLevelEnum.TRACE: _ = log.TraceAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path); break; case LogLevelEnum.DEBUG: _ = log.DebugAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path); break; case LogLevelEnum.WRAN: _ = log.WranAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path); break; case LogLevelEnum.INFO: _ = log.InfoAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path); break; case LogLevelEnum.ERROR: _ = log.ErrorAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path); break; case LogLevelEnum.FATAL: _ = log.FatalAsync(msg, null, "GRpcRequestLogInterceptor", eventId: eventId, path); break; } return(re); }
/// <summary> /// 执行 /// </summary> /// <param name="context">http上下文</param> /// <returns>任务</returns> public async Task InvokeAsync(HttpContext context) { var path = context.Request.Path.Value.ToLower(); if (path.StartsWith(options.PfxApiPath)) { try { await next(context); } catch (BusinessException ex) // 业务异常不记录日志 { var re = new BasicReturnInfo(); re.SetCodeMsg(ex.Code, ex.Msg, ex.Desc); await WriteReturnInfo(context, options, re); } catch (Exception ex) { var routeValue = context.Request.RouteValues; var routes = routeValue.GetControllerAction(); var msg = new StringBuilder($"请求:{path} method:{context.Request.Method} "); string controller = null, action = null; if (routes != null && routes.Length == 2) { controller = routes[0]; action = routes[1]; msg.AppendFormat("controller:{0},action:{1}.", controller, action); } msg.Append("发生异常." + ex.Message); _ = log.ErrorAsync(msg.ToString(), ex, "ApiExceptionHandleMiddleware", path, controller, action); var returnInfo = new BasicReturnInfo() { Code = options.ExceptionCode, Msg = options.ExceptionMsg, }; if (options.IsDevelopment) { returnInfo.Ex = ex; returnInfo.Desc = ex.ToString(); } else { returnInfo.Desc = ex.Message; } await WriteReturnInfo(context, options, returnInfo); } } else { await next(context); } }
/// <summary> /// 执行 /// </summary> /// <param name="context">http上下文</param> /// <returns>任务</returns> public async Task InvokeAsync(HttpContext context) { var stop = new Stopwatch(); stop.Start(); var path = context.Request.Path.Value.ToLower(); await next(context); stop.Stop(); var msg = $"请求:{path},耗时:{stop.ElapsedMilliseconds}ms"; switch (options.LogLevel) { case LogLevelEnum.TRACE: _ = log.TraceAsync(msg, null, "RequestLogMiddleware", path); break; case LogLevelEnum.DEBUG: _ = log.DebugAsync(msg, null, "RequestLogMiddleware", path); break; case LogLevelEnum.WRAN: _ = log.WranAsync(msg, null, "RequestLogMiddleware", path); break; case LogLevelEnum.INFO: _ = log.InfoAsync(msg, null, "RequestLogMiddleware", path); break; case LogLevelEnum.ERROR: _ = log.ErrorAsync(msg, null, "RequestLogMiddleware", path); break; case LogLevelEnum.FATAL: _ = log.FatalAsync(msg, null, "RequestLogMiddleware", path); break; } }
/// <summary> /// 进行处理 /// </summary> /// <param name="evt">基本传递事件参数</param> public override void ProcessRequest(BasicDeliverEventArgs evt) { try { if (receiveMessageFun != null) { // 这里调用处理业务 returnClientMessage = receiveMessageFun(evt.Body); } base.ProcessRequest(evt); } catch (Exception ex) { log.ErrorAsync(ex.Message, ex, thisName, thisFullName); } }
/// <summary> /// 执行 /// </summary> /// <param name="context">http上下文</param> /// <returns>任务</returns> public async Task InvokeAsync(HttpContext context) { var path = context.Request.Path.Value.ToLower(); if (path.StartsWith(options.PfxApiPath)) { try { await next(context); } catch (Exception ex) { _ = log.ErrorAsync(ex.Message, ex, "ApiExceptionHandleMiddleware"); var returnInfo = new BasicReturnInfo() { Code = options.ExceptionCode, Msg = options.ExceptionMsg, }; if (options.IsDevelopment) { returnInfo.Ex = ex; returnInfo.Desc = ex.ToString(); } else { returnInfo.Desc = ex.Message; } context.Response.ContentType = "text/html;charset=UTF-8"; context.Response.StatusCode = options.HttpStatusCode; await context.Response.WriteAsync(options.Serialization.Serialize(returnInfo)); } } else { await next(context); } }
/// <summary> /// 生成业务异常信息 /// </summary> /// <param name="ex">异常</param> /// <param name="queueMessage">队列消息</param> /// <param name="amqpQueue">AMQP队列信息</param> /// <param name="log">日志</param> /// <param name="desc">描述</param> /// <returns>业务异常信息</returns> public static BusinessExceptionInfo BuilderBusinessException(Exception ex, object queueMessage, AmqpQueueInfo amqpQueue, ILogable log, string desc = null) { if (log == null) { throw new ArgumentNullException("日志不能为null"); } string queueMessageJson = null; if (queueMessage != null) { try { queueMessageJson = queueMessage.ToJsonString(); } catch (Exception ex1) { log.ErrorAsync("JSON序列化业务异常信息出错", ex1, typeof(AmqpUtil).Name); return(null); } } var busEx = new BusinessExceptionInfo() { ExId = snowflakeId.New(), HostId = amqpQueue.HostId, Time = DateTime.Now, ServiceName = string.IsNullOrWhiteSpace(amqpQueue.ExceptionHandle.ServiceName) ? App.AppServiceName : amqpQueue.ExceptionHandle.ServiceName, ExceptionString = ex.ToString(), ExceptionMessage = ex.Message, Exchange = amqpQueue.ExchangeName, Queue = amqpQueue.Queue.Name, QueueMessageJsonString = queueMessageJson, Desc = desc, ServerMachineName = Environment.MachineName, ServerIP = NetworkUtil.LocalIP }; return(busEx); }
/// <summary> /// 执行 /// </summary> /// <param name="context">http上下文</param> /// <returns>任务</returns> public async Task InvokeAsync(HttpContext context) { var routeValue = context.Request.RouteValues; var routes = routeValue.GetControllerAction(); // 过滤掉非控制器(排除掉GRpc) if (routes.IsNullOrLength0() && !GRpcChannelUtil.IsRequestGRpc(context.Request.ContentType)) { await next(context); return; } var stop = new Stopwatch(); stop.Start(); var path = context.Request.Path.Value.ToLower(); await next(context); stop.Stop(); var msg = new StringBuilder($"请求:{path} method:{context.Request.Method} "); string controller = null, action = null; if (routes != null && routes.Length == 2) { controller = routes[0]; action = routes[1]; msg.AppendFormat("controller:{0},action:{1}.", controller, action); } msg.Append($"耗时:{stop.ElapsedMilliseconds}ms"); var msgStr = msg.ToString(); string eventId = theOperation != null ? theOperation.EventId : null; switch (options.LogLevel) { case LogLevelEnum.TRACE: _ = log.TraceAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; case LogLevelEnum.DEBUG: _ = log.DebugAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; case LogLevelEnum.WRAN: _ = log.WranAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; case LogLevelEnum.INFO: _ = log.InfoAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; case LogLevelEnum.ERROR: _ = log.ErrorAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; case LogLevelEnum.FATAL: _ = log.FatalAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; } }