public void Write(string strLogDir, string strText) { if (string.IsNullOrEmpty(strLogDir)) { throw new ArgumentException("strLogDir 不应为空", "strLogDir"); } _lock.EnterWriteLock(); try { DateTime now = DateTime.Now; // 每天一个日志文件 string strFileName = Path.Combine(strLogDir, "log_" + DateTimeUtil.DateTimeToString8(now) + ".txt"); if (strFileName.ToLower() != _fileName || _sw == null) { OpenFile(strFileName); } string strTime = now.ToString(); _sw.WriteLine(strTime + " " + strText); } finally { _lock.ExitWriteLock(); } }
// 写入日志文件。每天创建一个单独的日志文件 public static void WriteErrorLog( object lockObj, string strLogDir, string strText, string strPrefix = "log_", string strPostfix = ".txt") { lock (lockObj) { DateTime now = DateTime.Now; // 每天一个日志文件 string strFilename = Path.Combine(strLogDir, strPrefix + DateTimeUtil.DateTimeToString8(now) + strPostfix); string strTime = now.ToString(); StreamUtil.WriteText(strFilename, strTime + " " + strText + "\r\n"); } }
// 写入日志文件。每天创建一个单独的日志文件 // Exception: // 可能会抛出异常。 // System.UnauthorizedAccessException 对路径的访问被拒绝。 public static void WriteErrorLog( object lockObj, string strLogDir, string strText, string strPrefix = "log_", string strPostfix = ".txt") { lock (lockObj) { DateTime now = DateTime.Now; // 每天一个日志文件 string strFilename = Path.Combine(strLogDir, strPrefix + DateTimeUtil.DateTimeToString8(now) + strPostfix); string strTime = now.ToString(); // Exception: // 可能会抛出异常。 // System.UnauthorizedAccessException 对路径的访问被拒绝。 StreamUtil.WriteText(strFilename, strTime + " " + strText + "\r\n"); } }