Ejemplo n.º 1
0
        private static void ReadyForWork(string message, string logType, bool isWriteTxt)
        {
            if (!AppConfig.Log.IsWriteLog)
            {
                Error.Throw("Error : " + logType + " : " + message);
            }
            SysLogs log = new SysLogs();

            log.IsWriteLogOnError = false;
            log.IsWriteToTxt      = isWriteTxt;
            log.Message           = message;
            log.LogType           = logType;
            log.CreateTime        = DateTime.Now;
            if (AppConfig.IsWeb && System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Handler != null)
            {
                System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
                log.PageUrl = request.Url.Scheme + "://" + request.Url.Authority + System.Web.HttpUtility.UrlDecode(request.RawUrl);
                if (request.UrlReferrer != null && request.Url != request.UrlReferrer)
                {
                    log.RefererUrl = System.Web.HttpUtility.UrlDecode(request.UrlReferrer.ToString());
                }
                log.HttpMethod = request.HttpMethod;
                log.ClientIP   = request.Headers["X-Real-IP"] ?? request.UserHostAddress;
            }
            LogWorker.Add(log);
        }
Ejemplo n.º 2
0
        private static void DoWork(object p)
        {
            //检测文件夹
            string folder  = AppConfig.WebRootPath;
            string logPath = AppConfig.Log.LogPath;

            if (logPath.StartsWith("~/"))
            {
                logPath = logPath.Substring(2);
            }
            folder = folder + logPath;
            if (!System.IO.Directory.Exists(folder))
            {
                System.IO.Directory.CreateDirectory(folder);
            }
            int empty = 0;

            while (true)
            {
                empty++;
                while (_LogQueue.Count > 0)
                {
                    empty = 0;
                    SysLogs sys = _LogQueue.Dequeue();
                    if (sys == null)
                    {
                        continue;
                    }
                    if (!sys.IsWriteToTxt)
                    {
                        if (sys.Insert(InsertOp.None)) //直接写数据库。
                        {
                            sys.Dispose();
                            continue;
                        }
                    }

                    string todayKey = DateTime.Today.ToString("yyyyMMdd") + ".txt";

                    if (!string.IsNullOrEmpty(sys.LogType))
                    {
                        todayKey = sys.LogType.TrimEnd('_') + '_' + todayKey;
                    }
                    string filePath = folder + todayKey;
                    //检测数据库
                    //检测文件路径:
                    string body = sys.GetFormatterText();
                    IOHelper.Save(filePath, body, true, false);
                    sys.Dispose();
                }
                Thread.Sleep(5000);
                if (empty > 100)
                {
                    //超过10分钟没日志产生
                    hashObj.Clear();
                    threadIsWorking = false;
                    break;//结束线程。
                }
            }
        }
Ejemplo n.º 3
0
        public static void Add(SysLogs log)
        {
            long hash = log.Message.GetHashCode() + log.LogType.GetHashCode();

            if (hashObj.ContainsKey(hash))
            {
                if (hashObj[hash].AddMinutes(3) < DateTime.Now)
                {
                    hashObj.Remove(hash); //超过1分钟的,不再存档。
                }
                log = null;               //直接丢掉。
                return;
            }
            hashObj.Add(hash, DateTime.Now);
            _LogQueue.Enqueue(log);
            if (!threadIsWorking)
            {
                threadIsWorking = true;
                ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), null);
            }
        }
Ejemplo n.º 4
0
        public static void WriteLogToDB(string message, string logType, string userName)
        {
            string conn = AppConfig.Log.LogConn;

            if (string.IsNullOrEmpty(conn))
            {
                WriteLogToTxt("[LogConnIsEmpty]:" + message);
                return;
            }
            try
            {
                string pageUrl = string.Empty;
                if (AppConfig.IsWeb)
                {
                    System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
                    pageUrl = request.Url.Scheme + "://" + request.Url.Authority + request.RawUrl;
                }
                else
                {
                    pageUrl = AppConst.RunFolderPath;
                }

                using (SysLogs el = new SysLogs())
                {
                    el.AllowWriteLog = false;
                    el.LogType       = logType.ToString();
                    el.PageUrl       = pageUrl;
                    el.Message       = message;
                    el.UserName      = userName;
                    el.CreateTime    = DateTime.Now;
                    el.Insert(InsertOp.None);
                }
            }
            catch
            {
                WriteLogToTxt("[WriteDbLogError]:" + message);
            }
        }
Ejemplo n.º 5
0
        private static void ReadyForWork(string message, string logType, bool isWriteTxt)
        {
            if (!AppConfig.Log.IsWriteLog)
            {
                Error.Throw("Error : " + logType + " : " + message);
            }
            SysLogs log = new SysLogs();

            log.IsWriteLogOnError = false;
            log.IsWriteToTxt      = isWriteTxt;
            log.Message           = message;
            log.LogType           = logType;
            log.CreateTime        = DateTime.Now;
            if (AppConfig.IsWeb && System.Web.HttpContext.Current != null)
            {
                System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
                log.PageUrl = request.Url.Scheme + "://" + request.Url.Authority + request.RawUrl;
                if (request.UrlReferrer != null && request.Url != request.UrlReferrer)
                {
                    log.RefererUrl = request.UrlReferrer.ToString();
                }
            }
            LogWorker.Add(log);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///  将日志写到数据库中[需要配置LogConn项后方生效 ]
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="logType">日志类型</param>
        /// <param name="userName">用户账号</param>
        public static void WriteLogToDB(string message, string logType, string userName)
        {
            string conn = AppConfig.Log.LogConn;
            if (string.IsNullOrEmpty(conn))
            {
                WriteLogToTxt("[LogConnIsEmpty]:" + message);
                return;
            }
            try
            {
                string pageUrl = string.Empty;
                if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request != null)
                {
                    System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
                    pageUrl = request.Url.Scheme + "://" + request.Url.Authority + request.RawUrl;
                }
                else
                {
                    pageUrl = AppConst.RunFolderPath;
                }

                using (SysLogs el = new SysLogs())
                {
                    el.AllowWriteLog = false;
                    el.LogType = logType.ToString();
                    el.PageUrl = pageUrl;
                    el.Message = message;
                    el.UserName = userName;
                    el.CreateTime = DateTime.Now;
                    el.Insert(InsertOp.None);
                }
            }
            catch
            {
                WriteLogToTxt("[WriteDbLogError]:" + message);
            }
        }