【日志记录类】

这个博客介绍了一个C#实现的日志记录类,包括初始化、写入日志、错误日志、清除日志功能。类使用静态方法,提供方便的调用方式,确保日志文件按日期和时间进行组织和清理。
摘要由CSDN通过智能技术生成

静态类,初始化调用InitLogApp()

其他任意位置调用LogWrite();
    public class LogApp
    {

        private static string strLocalFilePath = "";
        private static string strLocalFile = "logfile";
        private static string strErrorFile = "logEror";

        private static bool IsInited = false;

        public static void (string strFilePath)
        {
            try
            {
                strLocalFilePath = strFilePath + "logs";
                string strdir = strLocalFilePath;    //strLocalFile.Substring(0, strLocalFile.LastIndexOf("\\"));
                if (!System.IO.Directory.Exists(strdir))
                    System.IO.Directory.CreateDirectory(strdir);
                IsInited = true;
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":  " + strLocalFilePath);
            }
            catch { }
        }

        public static void LogWrite(string strval)
        {
            if (!IsInited) { InitLogApp(System.Windows.Forms.Application.StartupPath + "\\"); };
            try
            {
                if (!Directory.Exists(strLocalFilePath)) Directory.CreateDirectory(strLocalFilePath);
                System.IO.StreamWriter sw = System.IO.File.AppendText(strLocalFilePath + "\\" + strLocalFile + DateTime.Now.ToString("yyyyMMddHH") + ".log");
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":  " + strval);
                sw.Close();
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":  " + strval);
            }
            catch { }
        }

        public static void LogError(string strval)
        {
            if (!IsInited) { InitLogApp(System.Windows.Forms.Application.StartupPath + "\\"); };
            try
            {
                if (!Directory.Exists(strLocalFilePath)) Directory.CreateDirectory(strLocalFilePath);
                System.IO.StreamWriter sw = System.IO.File.AppendText(strLocalFilePath + "\\" + strErrorFile + DateTime.Now.ToString("yyyyMMddHH") + ".log");
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":  " + strval);
                sw.Close();
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":  " + strval);
            }
            catch { }
        }

        public static void LogFileClear(int day)
        {
            if (!IsInited) { InitLogApp(System.Windows.Forms.Application.StartupPath + "\\"); };
            try
            {
                System.Collections.Hashtable htFileInfos = DIR.GetAllFiles(strLocalFilePath);
                //string[] files = Directory.GetFiles(strLocalFilePath);//得到文件   
                foreach (System.Collections.DictionaryEntry de in htFileInfos)//循环文件
                {
                    FileInfo file = (FileInfo)de.Value;
                    string exname = file.Name.Substring(0, file.Name.LastIndexOf("."));//得到后缀名
                    //string exname = exname1.Substring(exname1.LastIndexOf(".") + 1);//得到后缀名

                    //string strfnDateTime = exname.Substring(7, 8).Insert(6, "-").Insert(4, "-");
                    DateTime timeFile = Convert.ToDateTime(file.LastWriteTime);
                    DateTime timeNow = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                    if (timeFile.AddDays(day) < timeNow)
                    {
                        try
                        {
                            File.Delete(file.FullName);
                        }
                        catch { }
                    }
                }
            }
            catch
            {

            }
        }


        public static void LogWrite(string strval, string strFlag)
        {
            if (!IsInited) { InitLogApp(System.Windows.Forms.Application.StartupPath + "\\"); };
            try
            {
                string strFile = strLocalFilePath + "\\" + strLocalFile + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                if (strFlag != "")
                {
                    string strPath = strLocalFilePath + "\\" + strFlag + "\\";
                    if (!Directory.Exists(strPath)) Directory.CreateDirectory(strPath);
                    strFile = strLocalFilePath + "\\" + strFlag + "\\" + strLocalFile + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                }


                System.IO.StreamWriter sw = System.IO.File.AppendText(strFile);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":  " + strval);
                sw.Close();
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":  " + strval);
            }
            catch { }
        }

        public static void LogError(string strval, string strFlag)
        {
            if (!IsInited) { InitLogApp(System.Windows.Forms.Application.StartupPath + "\\"); };
            try
            {
                string strFile = strLocalFilePath + "\\" + strErrorFile + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                if (strFlag != "")
                {
                    string strPath = strLocalFilePath + "\\" + strFlag + "\\";
                    if (!Directory.Exists(strPath)) Directory.CreateDirectory(strPath);
                    strFile = strLocalFilePath + "\\" + strFlag + "\\" + strErrorFile + DateTime.Now.ToString("yyyyMMddHH") + ".log";
                }

                System.IO.StreamWriter sw = System.IO.File.AppendText(strFile);
                // System.IO.StreamWriter sw = System.IO.File.AppendText(strLocalFilePath + "\\" + strErrorFile + DateTime.Now.ToString("yyyyMMddHH") + ".log");
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":  " + strval);
                sw.Close();
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ":  " + strval);
            }
            catch { }
        }

        public static void WriteErrorLog(string class_name, string method_name, string message_info)
        {
            StringBuilder strInfo = new StringBuilder();
            string strLogPath = strLocalFilePath + @"\LogDBInterface\";
            strLogPath = strLogPath.TrimEnd('\\') + "\\";
            if (!Directory.Exists(strLogPath)) Directory.CreateDirectory(strLogPath);
            string strOutPutFile = strLogPath + @"ErrorLog" + DateTime.Now.ToString("yyyyMMdd") + @".csv";
            //strOutPutFile = mstrLogPath + @"\ErrorLog" + DateTime.Now.ToString("yyyyMMdd") + @".csv";
            StreamWriter swCsv = new StreamWriter(strOutPutFile, true, System.Text.Encoding.GetEncoding("GB2312"));

            strInfo.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + ",");
            strInfo.Append(class_name + ",");
            strInfo.Append(method_name + ",");
            strInfo.Append("(" + message_info + ")");

            swCsv.WriteLine(strInfo.ToString());
            swCsv.Close();
        }

        public static void WriteSQLErrorLog(string class_name, string method_name, string strSql, string message_info, string strPath = "")
        {
            StringBuilder strInfo = new StringBuilder();
            string strLogPath = "";
            if (strPath == "")
                strLogPath = strLocalFilePath + @"\LogDBInterface\";
            else
                strLogPath = strLocalFilePath + "\\" + strPath + @"\LogDBInterface\";

            strLogPath = strLogPath.TrimEnd('\\') + "\\";
            if (!Directory.Exists(strLogPath)) Directory.CreateDirectory(strLogPath);

            string strOutPutFile = strLogPath + @"ErrorLog" + DateTime.Now.ToString("yyyyMMdd") + @".csv";
            //string strOutPutFile = Application.StartupPath + @"\LogDBInterface\" + mstrLogPath + @"ErrorLog" + DateTime.Now.ToString("yyyyMMdd") + @".csv";
            StreamWriter swCsv = new StreamWriter(strOutPutFile, true, System.Text.Encoding.GetEncoding("GB2312"));

            strInfo.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + ",");
            strInfo.Append(class_name + ",");
            strInfo.Append(method_name + ",");
            strInfo.Append(strSql + ",");
            strInfo.Append("(" + message_info + ")");

            swCsv.WriteLine(strInfo.ToString());
            swCsv.Close();
        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lindong0217

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值