| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 | using System;using System.IO;using System.Windows.Forms;namespace WCS_Client{    public class Log    {        /// 日志文件的路径        private string fileLogPath;        /// 日志文件的名称        private string dataFileName;        public Log(string FileName, string filepath)        {            this.fileLogPath = filepath;            this.dataFileName = FileName + ".txt";            //this.dataFileName = DateTime.Now.ToString("yyyyMMdd") + FileName + ".txt";        }        /// <summary>        /// 获取或设置日志文件的路径        /// </summary>        public string FileLogPath        {            set            {                this.fileLogPath = value;            }            get            {                return this.fileLogPath;            }        }        /// <summary>        /// 获取或设置日志文件的名称        /// </summary>        public string DataFileName        {            set            {                this.dataFileName = value;            }            get            {                return this.dataFileName;            }        }        /// <summary>        /// 向指定目录中的指定的文件中追加日志文件        /// </summary>        /// <param name="Message">要写入的内容</param>        public void WriteLog(string Message)        {            this.WriteLog(this.dataFileName, Message);        }        /// <summary>        /// 向指定目录中的文件中追加日志文件,日志文件的名称将由传递的参数决定.        /// </summary>        /// <param name="LogFileName">日志文件的名称,如:mylog.txt ,如果没有自动创建,如果存在将追加写入日志</param>        /// <param name="Message">要写入的内容</param>        public void WriteLog(string LogFileName, string Message)        {            //如果日志文件目录不存在,则创建            if (!Directory.Exists(this.fileLogPath))            {                Directory.CreateDirectory(this.fileLogPath);            }            FileInfo finfo = new FileInfo(this.fileLogPath + LogFileName);            try            {                FileStream fs = new FileStream(this.fileLogPath + LogFileName, FileMode.Append);                StreamWriter strwriter = new StreamWriter(fs);                try                {                    strwriter.WriteLine(System.DateTime.Now.ToString() + " " + DateTime.Now.Millisecond.ToString().PadLeft(3, '0') + ": " + Message);                    strwriter.Flush();                }                catch (Exception ee)                {                    Console.WriteLine("日志文件写入失败信息:" + ee.ToString());                }                finally                {                    strwriter.Close();                    strwriter = null;                    fs.Close();                    fs = null;                }            }            catch (Exception ee)            {                Console.WriteLine("日志文件没有打开,详细信息如下:" + ee.ToString());            }        }    }    public class LogHelper    {        //private static Log _sys_Log = new Log("系统", "C:\\webapi日志\\");//Application.StartupPath +        //private static string mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");        private static string mappedPath = Application.StartupPath;        private static string dt = DateTime.Now.ToString("yyyyMMdd");        private static string fileName = dt + " 系统";        private static Log _sys_Log = new Log(fileName, mappedPath + "\\Logs\\");        public static Log Sys_Log        {            get            {                if (dt != DateTime.Now.ToString("yyyyMMdd"))                {                    dt = DateTime.Now.ToString("yyyyMMdd");                    _sys_Log = new Log(fileName, mappedPath + "\\Logs\\");                }                return _sys_Log;            }        }    }}
 |