即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

日志文件

编程语言 zkj126521 18℃ 0评论
    class alertLog
    {
        private static alertLog log = null;
        private string logFile = null;
        private string logPath = null;
        private alertLog()
        {
            DateTime now = DateTime.Now;
            logFile = now.ToLongDateString().ToString();
            logPath = Directory.GetCurrentDirectory() + "\\log";
            System.IO.Directory.CreateDirectory(logPath);
        }
        public static alertLog getInstance()
        {
            if (null == log)
            {
                lock(new object())
                {
                    if (null == log)
                    {
                        log = new alertLog();
                    }
                }
            }
            return log;
        }

        public void wrightLog(string str)
        {
            lock (new object())
            {
                DateTime now = DateTime.Now;
                string date = now.ToLongDateString().ToString();
                string time = now.ToLongTimeString().ToString();


                ///System.IO.FileStream fs = System.IO.File.Create(logPath);

                string strTobeWrighted = "[" + date + " " + time + "] " + str + "\r\n";

                using (System.IO.FileStream fs = System.IO.File.OpenWrite(string.Format("{0}\\{1}.txt", logPath, logFile)/*logPath + ""*/))
                {
                    byte[] data = System.Text.Encoding.Default.GetBytes(strTobeWrighted);
                    fs.Position = fs.Length; 
                    fs.Write(data, 0, data.Length);
                    fs.Close();
                }
            }

        }
    }

转载请注明:CodingBlog » 日志文件

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情