示例#1
0
        //开启定时任务,写监控日志
        static void WriteMonitorLogThread()
        {
            int sleepMillSec = 600000;//10分钟

            try
            {
                int sleepMillSecTemp = AppConfig.GetFinalConfig("logMonitorIntervalMins", 10, LogApi.GetLogMonitorIntervalMins()) * 60000;
                if (sleepMillSecTemp > 0)
                {
                    sleepMillSec = sleepMillSecTemp;
                }
            }
            catch
            {
            }
            if (sleepMillSec <= 0)
            {
                return;
            }

            int    num    = 0;
            Thread thread = new Thread(() =>
                                       //     thread = new Thread(new ThreadStart(() =>
            {
                Thread.Sleep(1000 * 30);
                while (true)
                {
                    try
                    {
                        num++;
                        LogMonitorEdm monitor = new LogMonitorEdm()
                        {
                            Remark = "定时监控"
                        };
                        WriteLog(monitor);
                        WriteModelToFileForDebug(new { 内容 = DateTime.Now.ToString("HH:mm:ss.fff") + " , 第" + num + "次记录监控日志" });
                    }
                    catch
                    {
                    }
                    finally
                    {
                        Thread.Sleep(sleepMillSec);
                    }
                }
            });

            thread.IsBackground = true;
            thread.Priority     = ThreadPriority.AboveNormal;
            thread.Start();
            LogTraceEdm logModel = new LogTraceEdm()
            {
                Detail = "定时监控服务启动,服务执行周期为" + (sleepMillSec / 60000.0) + "分钟"
            };

            WriteLog(logModel);
        }
示例#2
0
        /// <summary>
        /// 注册日志组件到本系统,为日志组件准备基础信息:服务器IP、服务器主机名,系统名称等
        /// </summary>
        /// <param name="sys">业务系统类型</param>
        /// <param name="application">应用程序的Application对象</param>
        /// <param name="bWriteStartLog">是否是启动日志</param>
        /// <param name="bLogMonitor">是否写定时监控日志</param>
        public static void RegisterLogInitMsg(SysCategory sys, object applicationObj = null, UserCfg userConfig = null, Dictionary <SysCategory, string> webAppName = null, bool bWriteStartLog = true, bool bLogMonitor = true)
        {
            webAppNames = webAppName ?? new Dictionary <SysCategory, string>();
            new HttpCacheHelper().SetCache(AppConfig.GetCacheKey(CacheConst.userCfgInCode), userConfig, expireType: Expire.Month);
            userCfg   = GetUserConfigItem();
            userCfg   = userCfg ?? new UserCfg();
            dataCache = CacheFac.CacheFactory();
            appender  = AppenderFac.AppenderFactory();

            logLevelCfg = AppConfig.GetFinalConfig("log2NetLevel", LogLevel.Debug, GetLog2NetLevel());
            if (logLevelCfg == LogLevel.Off)
            {
                return;
            }

            // AutoMapperConfig.Configure();//注册AutoMapper
#if NET
            bRegister = true;
            ApplicationVisitCount.ApplicationObj = applicationObj;
#endif

            //var machineName = System.Web.HttpContext.Current.Server.MachineName;//服务器计算机名
            var    machineName = Environment.MachineName;//服务器计算机名
            var    server      = ClientServerInfo.ClientInfo.GetIPAccordingHost(machineName);
            string serverIP    = server.IP;
            dataCache.SetCache(AppConfig.GetCacheKey(CacheConst.serverIP), serverIP, expireType: Expire.Month);
            dataCache.SetCache(AppConfig.GetCacheKey(CacheConst.serverHost), machineName, expireType: Expire.Month);
            dataCache.SetCache(AppConfig.GetCacheKey(CacheConst.systemName), sys, expireType: Expire.Month);

            #region 使用EF自动创建数据库
            try
            {
                var initTraceDBWhenOracle = AppConfig.GetFinalConfig("initTraceDBWhenOracle", false, LogApi.IsInitTraceDBWhenOracle());
                //     initTraceDBWhenOracle = "1";
                var traceDBType = ComDBFun.GetDBGeneralInfo(DBType.LogTrace).DataBaseType;
                if (traceDBType != DataBaseType.Oracle || (traceDBType == DataBaseType.Oracle && initTraceDBWhenOracle))
                {
                    using (var context = new Log_OperateTraceContext())  //oracle 不建议使用EF,会导致字段名和数据库名必须加引号
                    {
#if NET
                        context.Database.Initialize(true); //EF6
#else
                        context.Database.EnsureCreated();  //EFCore
#endif
                    }
                }

                var initMonitorDBWhenOracle = AppConfig.GetFinalConfig("initMonitorDBWhenOracle", false, LogApi.IsInitMonitorDBWhenOracle());
                //    initMonitorDBWhenOracle = "1";
                var monitorDBType = ComDBFun.GetDBGeneralInfo(DBType.LogMonitor).DataBaseType;
                if (monitorDBType != DataBaseType.Oracle || (monitorDBType == DataBaseType.Oracle && initMonitorDBWhenOracle))
                {
                    using (var context = new Log_SystemMonitorContext())
                    {
#if NET
                        context.Database.Initialize(true); //EF6
#else
                        context.Database.EnsureCreated();  //EFCore
#endif
                    }
                }
            }
            catch (Exception ex)
            {
                WriteExceptToFile(ex);
            }
            #endregion 使用EF自动创建数据库

            if (bWriteStartLog)
            {
                WriteServerStartupLog();//系统启动的日志
            }

            StartThreadToWriteFileToAppender();  //开启线程,将备份日志写到Appender中

            //在线人数和访客人数的初始化
            VisitOnline.VisitCountFactory.GetInstance().SetVisitNumWhenInit();
            if (bLogMonitor)
            {
                WriteMonitorLogThread();
            }
        }