Пример #1
0
        public BaseModule()
        {
            DateTime elapsedTime = DateTime.Now;
            bool     ignoreLog   = false;

            Before += ctx =>
            {
                var route = GetRequestData(ctx.Request);
                OptimalRoute           = route;
                HeadData.Authorization = RouteHelper.CreateToken(route);
                ignoreLog = SettingsHelper.IgnoreLogChannel(HeadData.Channel);
                return(null);
            };

            After += ctx =>
            {
                if (!ignoreLog)
                {
                    string response;
                    using (MemoryStream respData = new MemoryStream())
                    {
                        ctx.Response.Contents(respData);
                        response = Encoding.UTF8.GetString(respData.ToArray());
                    }

                    LogHelper.Info(HeadData.Command,
                                   string.Format(
                                       "Route request successfully,Address:{0},Time:{1}(s),Head:{2},Body:{3},RouteData:{4},Response:{5},UseCache:{6}",
                                       Request.Url, (DateTime.Now - elapsedTime).TotalSeconds, JsonConvert.SerializeObject(HeadData),
                                       JsonConvert.SerializeObject(BodyData), JsonConvert.SerializeObject(OptimalRoute), response,
                                       FinalUseCache));
                }
            };

            OnError += (ctx, ex) =>
            {
                if (!ignoreLog)
                {
                    LogHelper.Error(
                        string.Format("Route request Error,Command{0}", HeadData == null ? "" : HeadData.Command),
                        string.Format(
                            "Route request error,Address:{0},End time:{1},Head:{2},Body:{3},RouteData:{4},Error Message:{5}",
                            Request.Url, DateTime.Now, JsonConvert.SerializeObject(HeadData), JsonConvert.SerializeObject(BodyData),
                            JsonConvert.SerializeObject(OptimalRoute), ex.Message), ex);
                }
                dynamic response = new ExpandoObject();
                response.Code         = "500";
                response.ErrorMessage = string.Format("Route请求异常,Message:{0}", ex.Message);
                return(JsonConvert.SerializeObject(response));
            };
        }