예제 #1
0
 private void InternalLog(LogLevel logLevel, string moduleName, string categoryName, string subcategoryName, string message, ExtraInfo extraInfo)
 {
     try
     {
         if (AppInfoCenterConfiguration.GetConfig().LoggingServiceConfig.Enabled)
         {
             var strategy = GetLogStrategy(moduleName, logLevel);
             if (strategy != null)
             {
                 if (strategy.LocalLog)
                 {
                     LocalLog(logLevel, moduleName, categoryName, subcategoryName, message, extraInfo);
                 }
                 if (strategy.RemoteLog)
                 {
                     var info = new LogInfo();
                     info.LogLevel        = logLevel;
                     info.ModuleName      = moduleName;
                     info.CategoryName    = categoryName;
                     info.SubCategoryName = subcategoryName;
                     info.Message         = message;
                     info.ExtraInfo       = extraInfo;
                     ProcessInfo(info);
                     MongodbService.MongodbInsertService.Insert(info);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         LocalLoggingService.Error("InternalLog出现错误,异常信息为:" + ex.ToString());
     }
 }
예제 #2
0
        public void StartPerformanceMeasure(string name, string categoryName, string subcategoryName, ExtraInfo extraInfo = null)
        {
            try
            {
                if (!AppInfoCenterConfiguration.GetConfig().PerformanceServiceConfig.PerformanceMeasureConfig.Enabled)
                {
                    return;
                }
                if (HttpContext.Current == null)
                {
                    return;
                }
                Dictionary <string, PerformanceInfo> info = HttpContext.Current.Items[AppInfoCenterConfiguration.Const.ContextPerformanceMeasureKey] as Dictionary <string, PerformanceInfo>;
                if (info == null)
                {
                    info = new Dictionary <string, PerformanceInfo>();
                }

                if (!info.ContainsKey(name))
                {
                    info.Add(name, new PerformanceInfo
                    {
                        Name              = name,
                        sw                = Stopwatch.StartNew(),
                        threadTime        = GetCurrentThreadTimes(),
                        PerformancePoints = new Dictionary <string, PerformancePoint>(),
                    });
                }
                HttpContext.Current.Items[AppInfoCenterConfiguration.Const.ContextPerformanceMeasureKey] = info;
            }
            catch (Exception ex)
            {
                ex.Handle(AppInfoCenterService.ModuleName, ServiceName, "StartPerformanceMeasure");
            }
        }
예제 #3
0
        private void InternalHandle(ExceptionInfo info, Exception exception, string moduleName, string categoryName, string subcategoryName, string description, ExtraInfo extraInfo)
        {
            if (exception == null)
            {
                return;
            }
            try
            {
                if (AppInfoCenterConfiguration.GetConfig().ExceptionServiceConfig.Enabled)
                {
                    var strategy = GetExceptionStrategy(moduleName, info.GetType().Name, exception.GetType().Name);
                    if (strategy != null)
                    {
                        if (strategy.LocalLog)
                        {
                            LocalLog(info, exception, moduleName, categoryName, subcategoryName, description, extraInfo);
                        }
                        if (strategy.RemoteLog)
                        {
                            info.Exception         = MapException(exception);
                            info.Description       = description;
                            info.ExceptionTypeName = exception.GetType().FullName;
                            info.ExtraInfo         = extraInfo;
                            info.ModuleName        = moduleName;
                            info.CategoryName      = categoryName;
                            info.SubCategoryName   = subcategoryName;
                            info.ExceptionMessage  = exception.Message;
                            ProcessInfo(info);
                            MongodbService.MongodbInsertService.Insert(info);
                        }

                        if (info is WebSiteUnhandledExceptionInfo && HttpContext.Current != null)
                        {
                            HttpContext.Current.Response.Clear();

                            //HttpContext.Current.Response.StatusCode = strategy.ResponseStatusCode;

                            if (!HttpContext.Current.Request.IsLocal && strategy.ClearException)
                            {
                                HttpContext.Current.Server.ClearError();

                                if (!string.IsNullOrEmpty(strategy.RedirectUrl))
                                {
                                    HttpContext.Current.Response.Redirect(string.Format("{0}/?ID={1}", strategy.RedirectUrl.TrimEnd('/'), info.ID), false);
                                }
                                else
                                {
                                    HttpContext.Current.Response.Write(string.Format(AppInfoCenterConfiguration.GetConfig().ExceptionServiceConfig.UnhandledExceptionMessage, info.ID));
                                    HttpContext.Current.Response.End();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LocalLoggingService.Error("InternalHandle出现错误,异常信息为:" + ex.ToString());
            }
        }
예제 #4
0
        private static IncludeInfoStrategy GetIncludeInfoStrategyByName(string name)
        {
            var strategy = AppInfoCenterConfiguration.GetConfig().IncludeInfoStrategys.Values.FirstOrDefault(s => s.Name == name);

            if (strategy == null)
            {
                //AppInfoCenterService.LoggingService.Warning(AppInfoCenterService.ModuleName, "BaseService", "GetIncludeInfoStrategyByName", string.Format("没取到名为 {0} 的包含信息策略!", name));
                return(new IncludeInfoStrategy());
            }
            return(strategy);
        }
예제 #5
0
        private StateServiceConfigurationItem GetStateServiceConfigurationItem(string typeFullName)
        {
            var allconfigs = AppInfoCenterConfiguration.GetConfig().StateServiceConfig.StateServiceConfigurationItems;
            var config     = allconfigs.Values.FirstOrDefault(c => c.TypeFullName == typeFullName);

            if (config == null)
            {
                AppInfoCenterService.LoggingService.Warning(AppInfoCenterService.ModuleName, ServiceName, "GetStateServiceConfigurationItem", string.Format("没取到状态服务配置!参数:{0}", typeFullName));
                return(new StateServiceConfigurationItem());
            }
            return(config);
        }
예제 #6
0
        internal void EndPerformanceMeasure(long executionTime)
        {
            if (!AppInfoCenterConfiguration.GetConfig().PerformanceServiceConfig.PerformanceMeasureConfig.Enabled)
            {
                return;
            }

            if (executionTime < AppInfoCenterConfiguration.GetConfig().PerformanceServiceConfig.PerformanceMeasureConfig.PageExecutionMilliSecondsThreshold ||
                DateTime.Now < AppInfoCenterConfiguration.GetConfig().PerformanceServiceConfig.PerformanceMeasureConfig.BeginTime ||
                DateTime.Now > AppInfoCenterConfiguration.GetConfig().PerformanceServiceConfig.PerformanceMeasureConfig.EndTime)
            {
                return;
            }

            if (HttpContext.Current == null || HttpContext.Current.Items == null)
            {
                return;
            }

            var info = HttpContext.Current.Items[AppInfoCenterConfiguration.Const.ContextPerformanceMeasureKey] as Dictionary <string, PerformanceInfo>;

            if (info == null)
            {
                return;
            }

            foreach (var item in info)
            {
                try
                {
                    var pi = item.Value;
                    ProcessInfo(pi);
                    if (pi.PerformancePoints != null)
                    {
                        pi.TotalCPUTime               = pi.PerformancePoints.SelectMany(p => p.Value.PerformancePointItems).Sum(p => p.CPUTime);
                        pi.TotalTimeElapsed           = pi.PerformancePoints.SelectMany(p => p.Value.PerformancePointItems).Sum(p => p.TimeElapsed);
                        pi.TotalPerformancePointCount = pi.PerformancePoints.Count;
                    }

                    foreach (var point in pi.PerformancePoints)
                    {
                        point.Value.TotalPerformancePointItemCount = point.Value.PerformancePointItems.Count;
                        point.Value.AverageCPUTime     = Convert.ToInt32(point.Value.PerformancePointItems.Average(ppi => ppi.CPUTime));
                        point.Value.AverageTimeElapsed = Convert.ToInt32(point.Value.PerformancePointItems.Average(ppi => ppi.TimeElapsed));
                    }
                    MongodbService.MongodbInsertService.Insert(pi);
                }
                catch (Exception ex)
                {
                    ex.Handle(AppInfoCenterService.ModuleName, ServiceName, "EndPerformanceMeasure");
                }
            }
        }
예제 #7
0
        private void application_EndRequest(object sender, EventArgs e)
        {
            try
            {
                if (AdhesiveFramework.Status != AdhesiveFrameworkStatus.Started)
                {
                    return;
                }

                HttpContext httpContext = HttpContext.Current;
                if (httpContext == null)
                {
                    return;
                }

                long pageExecutionTime = -1;
                if (httpContext.Items.Contains(AppInfoCenterConfiguration.Const.ContextStopwatchKey))
                {
                    Stopwatch sw = httpContext.Items[AppInfoCenterConfiguration.Const.ContextStopwatchKey] as Stopwatch;
                    if (sw != null)
                    {
                        pageExecutionTime = sw.ElapsedMilliseconds;
                        //网站慢页面请求
                        WebsitePageExecutionStateService.Report(pageExecutionTime);
                        //代码性能测量服务
                        ((CodePerformanceService)AppInfoCenterService.PerformanceService).EndPerformanceMeasure(pageExecutionTime);
                    }
                }

                //嵌入页面
                if (AppInfoCenterConfiguration.GetConfig().CommonConfig.EmbedInfoToPage)
                {
                    httpContext.Response.Write("<!--");
                    httpContext.Response.Write(httpContext.Server.MachineName);
                    httpContext.Response.Write(", ");
                    httpContext.Response.Write(DateTime.Now.ToString());
                    httpContext.Response.Write(", ");
                    httpContext.Response.Write(string.Concat(pageExecutionTime.ToString(), " ms"));
                    httpContext.Response.Write("-->");
                }
            }
            catch (Exception ex)
            {
                LocalLoggingService.Error("application_EndRequest" + ex.ToString());
            }
        }
예제 #8
0
        private ExceptionStrategy GetExceptionStrategy(string moduleName, string exceptionInfoTypeName, string exceptionTypeName)
        {
            var strategy = AppInfoCenterConfiguration.GetConfig().ExceptionServiceConfig.StrategyList.Values.FirstOrDefault
                               (s => (s.ModuleName == moduleName || s.ModuleName == AbstractInfoProvider.DefaultModuleName) &&
                               s.ExceptionInfoTypeName == exceptionInfoTypeName && (string.IsNullOrEmpty(s.ExceptionTypeName) ||
                                                                                    s.ExceptionTypeName == exceptionTypeName));

            if (strategy == null)
            {
                if (AppInfoCenterService.ModuleName != moduleName)
                {
                    AppInfoCenterService.LoggingService.Warning(AppInfoCenterService.ModuleName, ServiceName, "GetExceptionStrategy",
                                                                string.Format("没取到错误策略!参数:{0},{1},{2}", moduleName, exceptionInfoTypeName, exceptionTypeName));
                }
                return(new ExceptionStrategy());
            }
            return(strategy);
        }
예제 #9
0
        private static IncludeInfoStrategy GetIncludeInfoStrategy(object item)
        {
            var type       = item.GetType();
            var allconfigs = AppInfoCenterConfiguration.GetConfig().IncludeInfoStrategyConfigurations;
            var configs    = allconfigs.Values.Where(c => c.TypeFullName == type.FullName).ToList();

            if (configs.Count == 1)
            {
                var config = configs.First();
                return(GetIncludeInfoStrategyByName(config.IncludeInfoStrategyName));
            }
            else
            {
                foreach (var config in configs.OrderByDescending(c => c.Conditions.Count))
                {
                    bool match = true;
                    foreach (var condition in config.Conditions)
                    {
                        if (condition.Value == null)
                        {
                            continue;
                        }

                        var property = type.GetProperty(condition.Key);
                        if (property != null)
                        {
                            var value = property.FastGetValue(item);
                            if (value != null && condition.Value.ToString() != value.ToString())
                            {
                                match = false;
                                break;
                            }
                        }
                    }
                    if (match)
                    {
                        return(GetIncludeInfoStrategyByName(config.IncludeInfoStrategyName));
                    }
                }
            }
            //AppInfoCenterService.LoggingService.Warning(AppInfoCenterService.ModuleName, "BaseService", string.Format("AdhesiveFramework.BaseService.GetIncludeInfoStrategy({0}) 没找到匹配的策略!", type.FullName));
            return(new IncludeInfoStrategy());
        }
예제 #10
0
        private LogStrategy GetLogStrategy(string moduleName, LogLevel logLevel)
        {
            var strategy = AppInfoCenterConfiguration.GetConfig().LoggingServiceConfig.StrategyList.Values.FirstOrDefault(s => s.ModuleName == moduleName &&
                                                                                                                          s.LogLevel == logLevel);

            if (strategy == null)
            {
                strategy = AppInfoCenterConfiguration.GetConfig().LoggingServiceConfig.StrategyList.Values.FirstOrDefault(s => s.ModuleName == AbstractInfoProvider.DefaultModuleName &&
                                                                                                                          s.LogLevel == logLevel);
            }
            if (strategy == null)
            {
                if (AppInfoCenterService.ModuleName != moduleName)
                {
                    AppInfoCenterService.LoggingService.Warning(AppInfoCenterService.ModuleName, ServiceName, "GetLogStrategy", string.Format("没取到日志策略!参数:{0},{1}", moduleName, logLevel));
                }
                return(new LogStrategy());
            }
            return(strategy);
        }
예제 #11
0
        public bool DoFilter(HttpContext httpContext)
        {
            var config = AppInfoCenterConfiguration.GetConfig().ExceptionServiceConfig.UnhandledExceptionFilterConfig;

            if (config.SpiderExceptionFilterConfig.Enabled)
            {
                var spiderIdList = config.SpiderExceptionFilterConfig.SpiderIdList;
                if (spiderIdList != null && spiderIdList.Count > 0)
                {
                    foreach (var spiderId in spiderIdList)
                    {
                        if (!string.IsNullOrEmpty(spiderId) && httpContext.Request.UrlReferrer == null && httpContext.Request.UserAgent.ToLower().Contains(spiderId.ToLower()))
                        {
                            LocalLoggingService.Debug("SpiderExceptionFilter catched.SpiderId:" + spiderId);
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
예제 #12
0
 internal static void Report(long executionTime)
 {
     try
     {
         if (!AppInfoCenterConfiguration.GetConfig().PerformanceServiceConfig.WebsitePageExecutionStateConfig.Enabled ||
             executionTime < AppInfoCenterConfiguration.GetConfig().PerformanceServiceConfig.WebsitePageExecutionStateConfig.LogSlowPageExecutionMilliSecondsThreshold)
         {
             return;
         }
         var info = new WebsitePageExecutionInfo();
         info.PageExecutionTime = executionTime;
         if (executionTime < 1000)
         {
             info.ExecutionTime = ExecutionTime.Less_1;
         }
         else if (executionTime >= 1000 && executionTime < 2000)
         {
             info.ExecutionTime = ExecutionTime.Between_1_2;
         }
         else if (executionTime >= 2000 && executionTime < 3000)
         {
             info.ExecutionTime = ExecutionTime.Between_2_3;
         }
         else if (executionTime >= 3000 && executionTime < 5000)
         {
             info.ExecutionTime = ExecutionTime.Between_3_5;
         }
         else
         {
             info.ExecutionTime = ExecutionTime.Greater_5;
         }
         ProcessInfo(info);
         MongodbService.MongodbInsertService.Insert(info);
     }
     catch (Exception ex)
     {
         ex.Handle(AppInfoCenterService.ModuleName, "WebsitePageExecutionStateService", "Report");
     }
 }
예제 #13
0
        private void application_Error(object sender, EventArgs e)
        {
            try
            {
                if (AdhesiveFramework.Status != AdhesiveFrameworkStatus.Started)
                {
                    return;
                }

                if (!AppInfoCenterConfiguration.GetConfig().ExceptionServiceConfig.Enabled)
                {
                    return;
                }

                HttpContext httpContext = HttpContext.Current;
                if (httpContext == null || httpContext.Server == null)
                {
                    return;
                }

                if (httpContext.Handler is DefaultHttpHandler)
                {
                    return;
                }


                Exception exception = httpContext.Server.GetLastError();

                //提取内部异常
                if (exception is HttpUnhandledException && exception.InnerException != null)
                {
                    exception = exception.InnerException;
                }

                if (exception is HttpException && exception.InnerException != null)
                {
                    exception = exception.InnerException;
                }

                if (exception == null)
                {
                    return;
                }
                if (exception is HttpRequestValidationException)
                {
                    return;
                }
                bool filtered = false;
                if (AppInfoCenterConfiguration.GetConfig().ExceptionServiceConfig.UnhandledExceptionFilterConfig.Enabled)
                {
                    foreach (var filterType in _filterTypes)
                    {
                        IUnhandledExceptionFilter filter = (IUnhandledExceptionFilter)Activator.CreateInstance(filterType);
                        if (filter.DoFilter(httpContext))
                        {
                            filtered = true;
                            break;
                        }
                    }
                }
                //处理
                if (!filtered)
                {
                    ((ExceptionService)AppInfoCenterService.ExceptionService).WebSiteUnhandledException(exception);
                }
            }
            catch (Exception ex)
            {
                LocalLoggingService.Error("application_Error" + ex.ToString());
            }
        }