C#简单写日志

来源:互联网 发布:如何计算矩阵的行列式 编辑:程序博客网 时间:2024/06/04 19:26

自己整理了一个简单写日志的类,方便以后再次用到。

说明:CS程序,程序启动目录下,自动创建Log目录,写日志。日志按天记录,每天的日志作为一个txt文件保存。

 /// <summary>    /// 日志管理功能    /// </summary>    public class LogManager    {        private static string logdirectory = Application.StartupPath + @"\Log\";                public static void Write(string msg)        {            if (!System.IO.Directory.Exists(logdirectory))            {                System.IO.Directory.CreateDirectory(logdirectory);            }            string name = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0');            string datapath = logdirectory + name + ".txt";            if (System.IO.File.Exists(datapath))//追加            {                using (FileStream fs = new FileStream(datapath, FileMode.Append))                {                    StreamWriter sw = new StreamWriter(fs);                    //开始写入                    sw.WriteLine("\n");                    sw.WriteLine("当前时间:" + DateTime.Now.ToString());                    sw.WriteLine("日志信息:" + msg);                    //清空缓冲区                    sw.Flush();                    //关闭流                    sw.Close();                    fs.Close();                }            }            else//创建            {                using (FileStream fs = new FileStream(datapath, FileMode.Create))                {                    StreamWriter sw = new StreamWriter(fs);                    //开始写入                    sw.WriteLine("当前时间:" + DateTime.Now.ToString());                    sw.WriteLine("日志信息:" + msg);                    //清空缓冲区                    sw.Flush();                    //关闭流                    sw.Close();                    fs.Close();                }            }        }        public static void Write(Exception ex)        {            if (!System.IO.Directory.Exists(logdirectory))            {                System.IO.Directory.CreateDirectory(logdirectory);            }            string name = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0');            string datapath = logdirectory + name + ".txt";//文件名格式为:20170606.txt            if (System.IO.File.Exists(datapath))//追加            {                using (FileStream fs = new FileStream(datapath, FileMode.Append))                {                    StreamWriter sw = new StreamWriter(fs);                    //开始写入                    sw.WriteLine("\n");                    sw.WriteLine("当前时间:" + DateTime.Now.ToString());                    sw.WriteLine("异常信息:" + ex.Message);                    sw.WriteLine("异常对象:" + ex.Source);                    sw.WriteLine("调用堆栈:" + ex.StackTrace);                    sw.WriteLine("触发方法:" + ex.TargetSite);                    //清空缓冲区                    sw.Flush();                    //关闭流                    sw.Close();                    fs.Close();                }            }            else//创建            {                using (FileStream fs = new FileStream(datapath, FileMode.Create))                {                    StreamWriter sw = new StreamWriter(fs);                    //开始写入                    sw.WriteLine("当前时间:" + DateTime.Now.ToString());                    sw.WriteLine("异常信息:" + ex.Message);                    sw.WriteLine("异常对象:" + ex.Source);                    sw.WriteLine("调用堆栈:" + ex.StackTrace);                    sw.WriteLine("触发方法:" + ex.TargetSite);                    //清空缓冲区                    sw.Flush();                    //关闭流                    sw.Close();                    fs.Close();                }            }        }    }


原创粉丝点击