• 异常日志文件errorlong


            #region log
            ////////////////////use///////////////
            /// <summary>
            /// 异常日志
            /// </summary>
            /// <param name="ex">Exception ex</param>
            /// <param name="flagTypeRemark">异常类型备注</param>
            public void LogException(Exception ex, string flagTypeRemark = "**")
            {
                AppPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"fileError";
                log(AppPath, "
    ##" + flagTypeRemark + "##
    Message:" + ex.Message + "
    Stacktrace:" + ex.StackTrace + "
    ");
            }
    
            /// <summary>
            /// 本地日志记录
            /// </summary>
            /// <param name="logPath">日志路径</param>
            /// <param name="logContent">要记录的日志内容</param>
            public static void log(string logPath, string logContent)
            {
                string filePath = AppPath + "elog.log";
                string content = DateTime.Now.ToString("yyyyMMddHHmmss:") + logContent;
                if (!System.IO.Directory.Exists(AppPath)) System.IO.Directory.CreateDirectory(AppPath);
                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 = "Pro_ErrorLog.log";
                thread.Start(logContent);
            }
    
            /// <summary>
            /// 当前程序运行路径
            /// </summary>
            public static string AppPath { get; set; }
    
            public static void writeLog(object str)
            {
                string filePath = AppPath + "elog.log";
                string content = "
    " + 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);
                    string backPath = AppPath + @"BackError";
                    if (!System.IO.Directory.Exists(backPath)) System.IO.Directory.CreateDirectory(backPath);
                    System.IO.File.Move(filePath, backPath + "elog" + 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;
            }
            #endregion
    //use
    LogException(ex, "异常类型备注文本");//
  • 相关阅读:
    [CentOS5]安装VirtualBox
    [Java]一则自定义的XStream转换器,主要用于POJO XML反序列化为Map/List
    [CentOS5]开启vsftpd中本地用户的上传权限
    [CSS]强制TD不换行
    [CentOS5]快速关闭SeLinux
    [vba]获取PPT幻灯片中的所有标题
    [VBA]批量转换xls为csv
    [OTHER]玩具的报复 绿化版 注册表
    [CentOS]在vsftpd中为本地用户指定默认目录
    递归读取输出无限分类目录
  • 原文地址:https://www.cnblogs.com/xifengyeluo/p/8473388.html
Copyright © 2020-2023  润新知