public void LogToFile(LogFileType logType, string logMessage) { string logDirectory = LogPath + @"\" + DateTime.Today.ToString("yyyyMMdd"); string filePath = string.Empty; if (!Directory.Exists(logDirectory)) { Directory.CreateDirectory(logDirectory); switch (logType) { case LogFileType.TRACE: filePath = logDirectory + @"\TRACE.0.log"; break; case LogFileType.MESSAGE: filePath = logDirectory + @"\MESSAGE.0.log"; break; case LogFileType.EXCEPTION: filePath = logDirectory + @"\EXCEPTION.0.log"; break; default: filePath = logDirectory + @"\TRACE.0.log"; break; } } else { string[] filePaths = Directory.GetFiles(logDirectory, "*.log"); if (filePaths.Length == 0) { switch (logType) { case LogFileType.TRACE: filePath = logDirectory + @"\TRACE.0.log"; break; case LogFileType.MESSAGE: filePath = logDirectory + @"\MESSAGE.0.log"; break; case LogFileType.EXCEPTION: filePath = logDirectory + @"\EXCEPTION.0.log"; break; default: filePath = logDirectory + @"\TRACE.0.log"; break; } } else { List <string> fileList = new List <string>(); foreach (string fPath in filePaths) { FileInfo file = new FileInfo(fPath); string[] parts = file.Name.Split('.'); if (parts[0].ToUpper() == logType.ToString().ToUpper()) { fileList.Add(fPath); } } int lastestIndex = int.MinValue; string lastestFilePath = ""; if (fileList.Count <= 0) { filePath = logDirectory + @"\" + logType.ToString().ToUpper() + ".0.log"; } else { foreach (string fPath in fileList) { FileInfo file = new FileInfo(fPath); string[] parts = file.Name.Split('.'); //fPath.Split('.'); if (Convert.ToInt32(parts[1]) >= lastestIndex) { lastestIndex = Convert.ToInt32(parts[1]); lastestFilePath = fPath; } } filePath = lastestFilePath; } if (File.Exists(filePath)) { FileInfo lastestFile = new FileInfo(filePath); // check if file size be larger than 5MB then create new one if (((lastestFile.Length / 1024f) / 1024f) > 5) { lastestIndex++; filePath = logDirectory + @"\" + logType.ToString().ToUpper() + "." + lastestIndex + ".log"; } } } } logMessage = DateTime.Now.ToString("HH:mm:ss") + " " + logMessage; TextWriterTraceListener listener = new TextWriterTraceListener(filePath); listener.WriteLine(logMessage); listener.Flush(); listener.Close(); }
public static void LogToFile(LogFileType logType, string logMessage) { //string LogPath = Environment.CurrentDirectory; var logPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);//@"C:\VNPT-BHXH"; var logDirectory = logPath + @"\" + DateTime.Today.ToString("yyyyMMdd"); string filePath; if (!Directory.Exists(logDirectory)) { Directory.CreateDirectory(logDirectory); switch (logType) { case LogFileType.Trace: filePath = logDirectory + @"\TRACE.0.log"; break; case LogFileType.Message: filePath = logDirectory + @"\MESSAGE.0.log"; break; case LogFileType.Exception: filePath = logDirectory + @"\EXCEPTION.0.log"; break; default: filePath = logDirectory + @"\TRACE.0.log"; break; } } else { var filePaths = Directory.GetFiles(logDirectory, "*.log"); if (filePaths.Length == 0) { switch (logType) { case LogFileType.Trace: filePath = logDirectory + @"\TRACE.0.log"; break; case LogFileType.Message: filePath = logDirectory + @"\MESSAGE.0.log"; break; case LogFileType.Exception: filePath = logDirectory + @"\EXCEPTION.0.log"; break; default: filePath = logDirectory + @"\TRACE.0.log"; break; } } else { var fileList = new List<string>(); foreach (var fPath in filePaths) { var file = new FileInfo(fPath); var parts = file.Name.Split('.'); if (parts[0].ToUpper() == logType.ToString().ToUpper()) { fileList.Add(fPath); } } var lastestIndex = int.MinValue; var lastestFilePath = ""; if (fileList.Count <= 0) { filePath = logDirectory + @"\" + logType.ToString().ToUpper() + ".0.log"; } else { foreach (var fPath in fileList) { var file = new FileInfo(fPath); var parts = file.Name.Split('.'); //fPath.Split('.'); if (Convert.ToInt32(parts[1]) >= lastestIndex) { lastestIndex = Convert.ToInt32(parts[1]); lastestFilePath = fPath; } } filePath = lastestFilePath; } if (File.Exists(filePath)) { var lastestFile = new FileInfo(filePath); // check if file size be larger than 5MB then create new one if (((lastestFile.Length / 1024f) / 1024f) > 5) { lastestIndex++; filePath = logDirectory + @"\" + logType.ToString().ToUpper() + "." + lastestIndex + ".log"; } } } } logMessage = DateTime.Now.ToString("HH:mm:ss") + " " + logMessage; var listener = new TextWriterTraceListener(filePath); listener.WriteLine(logMessage); listener.Flush(); listener.Close(); }
public static void LogToFile(LogFileType logType, string logMessage) { //string LogPath = Environment.CurrentDirectory; var logPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);//@"C:\VNPT-BHXH"; var logDirectory = logPath + @"\" + DateTime.Today.ToString("yyyyMMdd"); string filePath; if (!Directory.Exists(logDirectory)) { Directory.CreateDirectory(logDirectory); switch (logType) { case LogFileType.Trace: filePath = logDirectory + @"\TRACE.0.log"; break; case LogFileType.Message: filePath = logDirectory + @"\MESSAGE.0.log"; break; case LogFileType.Exception: filePath = logDirectory + @"\EXCEPTION.0.log"; break; default: filePath = logDirectory + @"\TRACE.0.log"; break; } } else { var filePaths = Directory.GetFiles(logDirectory, "*.log"); if (filePaths.Length == 0) { switch (logType) { case LogFileType.Trace: filePath = logDirectory + @"\TRACE.0.log"; break; case LogFileType.Message: filePath = logDirectory + @"\MESSAGE.0.log"; break; case LogFileType.Exception: filePath = logDirectory + @"\EXCEPTION.0.log"; break; default: filePath = logDirectory + @"\TRACE.0.log"; break; } } else { var fileList = new List <string>(); foreach (var fPath in filePaths) { var file = new FileInfo(fPath); var parts = file.Name.Split('.'); if (parts[0].ToUpper() == logType.ToString().ToUpper()) { fileList.Add(fPath); } } var lastestIndex = int.MinValue; var lastestFilePath = ""; if (fileList.Count <= 0) { filePath = logDirectory + @"\" + logType.ToString().ToUpper() + ".0.log"; } else { foreach (var fPath in fileList) { var file = new FileInfo(fPath); var parts = file.Name.Split('.'); //fPath.Split('.'); if (Convert.ToInt32(parts[1]) >= lastestIndex) { lastestIndex = Convert.ToInt32(parts[1]); lastestFilePath = fPath; } } filePath = lastestFilePath; } if (File.Exists(filePath)) { var lastestFile = new FileInfo(filePath); // check if file size be larger than 5MB then create new one if (((lastestFile.Length / 1024f) / 1024f) > 5) { lastestIndex++; filePath = logDirectory + @"\" + logType.ToString().ToUpper() + "." + lastestIndex + ".log"; } } } } logMessage = DateTime.Now.ToString("HH:mm:ss") + " " + logMessage; var listener = new TextWriterTraceListener(filePath); listener.WriteLine(logMessage); listener.Flush(); listener.Close(); }
/// <summary> /// 写日志 /// </summary> /// <param name="logFileType">日志文件类型</param> /// <param name="msg">消息正文</param> public void WriteLog(LogFileType logFileType, string msg) { this.WriteLog(logFileType.ToString(), msg); }