.Net常用模块类

1.日志模块

 /// <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 + @"file\Error\";
            log(AppPath, "\r\n##" + flagTypeRemark + "##\r\nMessage:" + ex.Message + "\r\nStacktrace:" + ex.StackTrace + "\r\n");
        }

        /// <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);
        }

2.获取运行程序属性

 /// <summary>
        /// 当前程序运行路径
        /// </summary>
        public static string AppPath { get; set; }

        public static void writeLog(object str)
        {
            string filePath = AppPath + "elog.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);
                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;
        }

3.CSV写入

写入数据到CSV文件
/// <summary>
        /// 写入数据到CSV文件,覆盖形式
        /// </summary>
        /// <param name="csvPath">要写入的字符串表示的CSV文件</param>
        /// <param name="LineDataList">要写入CSV文件的数据,以string[]类型List表示的行集数据</param>
        public void OpCsv(string csvPath, List<string[]> LineDataList)
        {
            using (FileStream fs = new FileStream(csvPath.Trim(), FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                {
                    StringBuilder sb_csvStr = new StringBuilder();
                    for (int i = 0; i < LineDataList.Count; i++)//<--row
                    {
                        sb_csvStr.Clear();
                        for (int j = 0; j < LineDataList[i].Length; j++)//<--col
                        {
                            sb_csvStr.Append(string.Format("{0},", LineDataList[i][j].ToString()));
                        }
                        sw.WriteLine(sb_csvStr.ToString().Substring(0, sb_csvStr.ToString().Length - 1));
                    }
                    fs.Flush();
                }
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值