public Task Execute(IJobExecutionContext context) { JobDataMap dataMap = context.MergedJobDataMap; if (dataMap.ContainsKey(GlobalConst.__ServiceDataMapKey)) { // 获取当前job传递的数据 var serviceDescriptor = dataMap[GlobalConst.__ServiceDataMapKey] as ServiceJobDescriptor; if (serviceDescriptor.Command != null) { try { // 执行绑定的委托 serviceDescriptor.Command.Excute(serviceDescriptor); } catch (Exception ex) { var type = serviceDescriptor.Command.GetType(); // 写日志 var info = new NGPExceptionLog { BusinessMethod = string.Format("{0}.{1}", type.FullName, "Excute"), ExceptionContent = string.IsNullOrEmpty(ex.Message) ? (ex.InnerException != null ? ex.InnerException.Message : "Unknow Error") : ex.Message, Exception = ex }; Singleton <IEngine> .Instance.Resolve <ILogPublisher>().RegisterError(info); } } } return(Task.FromResult(0)); }
/// <summary> /// 拦截器 /// </summary> /// <param name="invocation"></param> public void Intercept(IInvocation invocation) { MethodInfo methodInfo = null; try { methodInfo = invocation.MethodInvocationTarget; if (methodInfo == null) { methodInfo = invocation.Method; } // 执行方法 invocation.Proceed(); } catch (Exception ex) { // 书写异常日志 NGPExceptionLog error = new NGPExceptionLog { ApiUrl = _workContext.CurrentRequest.Url, Parameters = _workContext.CurrentRequest.Parameter, BusinessMethod = string.Format("{0}.{1}.{2}", methodInfo.ReflectedType.Namespace, methodInfo.ReflectedType.Name, methodInfo.Name), ExceptionContent = string.IsNullOrEmpty(ex.Message) ? (ex.InnerException != null ? ex.InnerException.Message : "Unknow Error") : ex.Message, OperatedBy = _workContext.Current != null ? _workContext.Current.EmplId : string.Empty, OperatedDept = _workContext.Current != null ? _workContext.Current.DeptId : string.Empty, Exception = ex }; _logPublisher.RegisterError(error); // 设定返回值 INGPResponse response = invocation.ReturnValue as INGPResponse; if (response == null) { response = Activator.CreateInstance(methodInfo.ReturnType) as INGPResponse; } response.Message = CommonResource.OperatorException; response.Status = OperateStatus.Error; response.ErrorCode = ErrorCode.SystemError; invocation.ReturnValue = response; } }
/// <summary> /// 发送数据 /// </summary> /// <typeparam name="T">发送数据对象</typeparam> /// <param name="mqInfo">目标通道信息</param> /// <param name="data">数据</param> public void Send <T>(MessageRouteInfo mqInfo, T data) { try { // 创建链接 var connectionFactory = new ConnectionFactory(); connectionFactory.HostName = mqInfo.HostName; connectionFactory.UserName = mqInfo.UserName; connectionFactory.Password = mqInfo.Password; connectionFactory.AutomaticRecoveryEnabled = true; using (var connection = connectionFactory.CreateConnection()) { using (var model = connection.CreateModel()) { // 注册交换机 model.ExchangeDeclare(mqInfo.ExchangeName, ExchangeType.Fanout, true); // 注册队列 model.QueueDeclare(mqInfo.QueueName, mqInfo.QueueDurable, false, false, null); // 对象转换成字节流 var json = JsonConvert.SerializeObject(data, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); var messageBodyBytes = Encoding.UTF8.GetBytes(json); // 发布数据 model.BasicPublish(mqInfo.ExchangeName, mqInfo.RouteKey, null, messageBodyBytes); } } } catch (Exception ex) { var type = this.GetType(); // 写日志 var info = new NGPExceptionLog { BusinessMethod = string.Format("{0}.{1}", type.FullName, "Excute"), ExceptionContent = string.IsNullOrEmpty(ex.Message) ? (ex.InnerException != null ? ex.InnerException.Message : "Unknow Error") : ex.Message, Exception = ex }; Singleton <IEngine> .Instance.Resolve <ILogPublisher>().RegisterError(info); } }
/// <summary> /// 插入错误日志 /// </summary> /// <param name="info">错误信息</param> public void InsertSysErrorLog(NGPExceptionLog info) { try { //// 注册仓储接口依赖项 //var unitRepository = UnityContainerFactory.Resolve<IUnitRepository>(); //var errorLog = info.CopyItem<ErrorLogInfo, ZM_SYS_ErrorLog>((from, to) => //{ // to.Id = GuidExtend.NewGuid(); // to.IsDelete = false; // to.UpdatedBy = from.OperateBy; // to.UpdatedDept = from.OperateDept; // to.UpdatedTime = DateTime.Now; //}); //unitRepository.Insert(errorLog); //UnityContainerFactory.Resolve<IUnitOfWork>().Commit(); } catch { } }
/// <summary> /// 注册监听 /// </summary> /// <typeparam name="T">监听数据类型</typeparam> /// <param name="mqInfo">监听通道对象</param> /// <param name="action">监听回调</param> public void Monitor <T>(MessageRouteInfo mqInfo, Action <T> action) { // 传输对象 var paramInfo = new MonitorMessageRouteInfo <T> { Action = action, MessageRoute = mqInfo }; // 后台线程 var thr = new Thread((obj) => { var param = obj as MonitorMessageRouteInfo <T>; var mqRouteInfo = param.MessageRoute; var factory = new ConnectionFactory(); factory.HostName = mqRouteInfo.HostName; factory.UserName = mqRouteInfo.UserName; factory.Password = mqRouteInfo.Password; factory.AutomaticRecoveryEnabled = true; using (var connection = factory.CreateConnection()) { using (var model = connection.CreateModel()) { // 注册交换机 model.ExchangeDeclare(mqRouteInfo.ExchangeName, ExchangeType.Fanout, true); // 注册队列 model.QueueDeclare(mqRouteInfo.QueueName, mqRouteInfo.QueueDurable, false, false, null); // 绑定队列 model.QueueBind(mqRouteInfo.QueueName, mqRouteInfo.ExchangeName, mqRouteInfo.RouteKey); var subscription = new Subscription(model, mqRouteInfo.QueueName, false); while (true) { try { BasicDeliverEventArgs basicDeliveryEventArgs = subscription.Next(); var json = Encoding.UTF8.GetString(basicDeliveryEventArgs.Body); var data = JsonConvert.DeserializeObject <T>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, }); param.Action(data); subscription.Ack(basicDeliveryEventArgs); } catch (Exception ex) { var type = GetType(); // 写日志 var info = new NGPExceptionLog { BusinessMethod = string.Format("{0}.{1}", type.FullName, "Excute"), ExceptionContent = string.IsNullOrEmpty(ex.Message) ? (ex.InnerException != null ? ex.InnerException.Message : "Unknow Error") : ex.Message, Exception = ex }; Singleton <IEngine> .Instance.Resolve <ILogPublisher>().RegisterError(info); } } } } }) { IsBackground = true }; thr.Start(paramInfo); }
/// <summary> /// 错误日志数据处理 /// </summary> /// <param name="context">上下文</param> private static void SendErrorHandler(NGPExceptionLog context) { Singleton <IEngine> .Instance.Resolve <ILogProvider>().InsertSysErrorLog(context); }
/// <summary> /// 注册错误日志 /// </summary> /// <param name="obj">日志对象</param> public void RegisterError(NGPExceptionLog obj) { _sendErrorHandler.Post(obj); }