码迷,mamicode.com
首页 > 其他好文 > 详细

异常日志文件errorlong

时间:2018-02-26 16:19:05      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:turn   AC   str   end   object   orm   static   mat   pdo   

////////////////////use///////////////
        public void LogException(Exception ex)
        {
            appPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Error\";
            log("\r\nMessage:" + ex.Message + "\r\nStacktrace:" + ex.StackTrace + "\r\n");
        }

        /// <summary>
        /// 本地日志记录
        /// </summary>
        /// <param name="str">要记录的日志内容</param>
        public static void log(string str)
        {
            string filePath = @appPath + "error.log";
            string content = DateTime.Now.ToString("yyyyMMddHHmmss:") + str;
            if (!System.IO.File.Exists(filePath))
            {
                System.IO.File.AppendAllText(filePath, content);
                return;
            }
            ParameterizedThreadStart threadStart = new ParameterizedThreadStart(writeLog);
            Thread thread = new Thread(threadStart);
            thread.Name = "HYAppFrame.log";
            thread.Start(str);
        }

        /// <summary>
        /// 当前程序运行路径
        /// </summary>
        private static string appPath;

        public static void writeLog(object str)
        {
            string filePath = @appPath + "error.log";
            string content = "\r\n" + DateTime.Now.ToString("yyyyMMddHHmmss:") + str.ToString();
            System.IO.FileInfo info = new System.IO.FileInfo(filePath);
            if (info.Length > 1024 * 1024 * 5)
            {
                while (IsFileInUse(filePath))
                    Thread.Sleep(100);
                System.IO.File.Move(filePath, @appPath + "error" + DateTime.Now.ToString("yyyyMMdd") + ".log");
                System.IO.File.Delete(filePath);
            }
            while (IsFileInUse(filePath))
                Thread.Sleep(100);
            if (!IsFileInUse(filePath))
            {
                #region write file
                System.IO.FileStream fs = null;
                try
                {
                    fs = new System.IO.FileStream(filePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None);
                    fs.Write(Encoding.UTF8.GetBytes(content), 0, Encoding.UTF8.GetByteCount(content));
                }
                catch
                {
                    ;
                }
                finally
                {
                    if (fs != null)
                        fs.Close();
                }
                #endregion
            }
        }

        /// <summary>
        /// 文件是否被占用??
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool IsFileInUse(string fileName)
        {
            bool inUse = true;
            System.IO.FileStream fs = null;
            try
            {
                fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None);
                inUse = false;
            }
            catch
            {
                inUse = true;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            return inUse;
        }

 

异常日志文件errorlong

标签:turn   AC   str   end   object   orm   static   mat   pdo   

原文地址:https://www.cnblogs.com/xifengyeluo/p/8473388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!