日志文件

xiaoxiao2021-02-28  132

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(); } } } }
转载请注明原文地址: https://www.6miu.com/read-24334.html

最新回复(0)