Exemplo n.º 1
0
        public void 发送全局日志_连接初始化一次()
        {
            var sw = new Stopwatch();

            sw.Start();

            for (int i = 0; i < count; i++)
            {
                var msg = new LogQueueModel
                {
                    RequestId   = "RequestId info",
                    RequestMsg  = "RequestMsg info",
                    ResponseMsg = "ResponseMsg info",
                    Date        = DateTime.Now
                };
                LogSend.GetInstance().SendToQueue(msg);
            }
            sw.Stop();

            TimeSpan ts          = sw.Elapsed;
            string   elapsedTime = String.Format("{0:00}:时 {1:00}:分 {2:00}:秒:{3:00}:毫秒",
                                                 ts.Hours, ts.Minutes, ts.Seconds,
                                                 ts.Milliseconds / 10);

            //00:时 00:分 00:秒:22:毫秒(1000)

            Assert.AreEqual(true, true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 发送到队列
        /// </summary>
        /// <param name="msg"></param>
        public void SendToQueue(LogQueueModel msg)
        {
            this.InitChannelWithLog(QueueName.LogQueue, msg);

            var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg));

            channel.BasicPublish("", QueueName.LogQueue, properties, body);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 发生错误 处理方式
        /// </summary>
        /// <param name="actionExecutedContext"></param>
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            LogQueueModel logQueueModel = new LogQueueModel()
            {
                FileName = ApiFileDirectoryPara.ApiErrorDir,
                Msg      = actionExecutedContext.Exception.Message
            };

            LogHelp.AddLogQueue(logQueueModel);

            var returnModel = ApiReturnModel.ReturnError(actionExecutedContext.Exception.Message);

            actionExecutedContext.Response         = new HttpResponseMessage();
            actionExecutedContext.Response.Content = new StringContent(JsonConvert.SerializeObject(returnModel));

            return;
        }
Exemplo n.º 4
0
        /// <summary>
        /// 微信回调 处理方法 包含签名验证
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="requestContent">回调内容</param>
        /// <param name="deserializeType">内容反序列化 类型id 默认为0  0-xml反序列化 1-json反序列化</param>
        /// <returns></returns>
        public override T Callback <T>(string requestContent, int deserializeType = 0)
        {
            T   request = new T();
            var pros    = request.GetType().GetProperties();

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(requestContent);

            XmlNode     xmlNode = xmlDoc.FirstChild;//获取到根节点<xml>
            XmlNodeList nodes   = xmlNode.ChildNodes;

            string sign = string.Empty;

            foreach (XmlNode xn in nodes)
            {
                foreach (var pro in pros)
                {
                    if (xn.Name == pro.Name)
                    {
                        if (pro.PropertyType == typeof(int))
                        {
                            pro.SetValue(request, Convert.ToInt32(xn.InnerText));
                        }
                        else
                        {
                            pro.SetValue(request, xn.InnerText);
                        }

                        if (pro.Name == SignProName)
                        {
                            sign = xn.InnerText;
                        }
                        break;
                    }
                }
            }
            if (CheckSign(sign, request))
            {
                LogQueueModel logQueueModel = new LogQueueModel()
                {
                    FileName = ApiFileDirectoryPara.WeiXinBusinessLog,
                    Msg      = $"收到微信合法回调 请求数据为 : \r\n {requestContent}"
                };

                LogHelp.AddLogQueue(logQueueModel);

                return(request);
            }
            else
            {
                //说明有人伪造 请求 记录日志
                LogQueueModel logQueueModel = new LogQueueModel()
                {
                    FileName = ApiFileDirectoryPara.WeiXinBusinessLog,
                    Msg      = $"收到微信非法回调(签名错误) 请求数据为 : \r\n {requestContent}"
                };

                LogHelp.AddLogQueue(logQueueModel);

                return(default(T));
            }
        }