异常日志文件errorlong

Posted 西枫叶落

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了异常日志文件errorlong相关的知识,希望对你有一定的参考价值。

////////////////////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的主要内容,如果未能解决你的问题,请参考以下文章

Oracle数据库从RMAN备份集片段还原指定单个归档日志进行日志挖掘分析

常用python日期日志获取内容循环的代码片段

我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情

应用程序立即崩溃,没有异常或日志(看起来像 xamarin/mono 错误)

argparse 代码片段只打印部分日志

python常用代码片段总结