public void InsertErrorLog(string function, string logMessage, EnLogType logType) { LogTime = DateTime.Now.TimeOfDay; LogFunction = function; LogMessage = logMessage; LogType = logType; string json = JsonConvert.SerializeObject(this); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filePath = GetLogPath(); TextWriter twriter = new StreamWriter(filePath, true); if (!File.Exists(filePath)) { File.Create(filePath); } twriter.WriteLine(json); twriter.Close(); }
/// <summary> /// 记录日志至文本文件 /// </summary> /// <param name="logtype">日志类型</param> /// <param name="message">记录的内容</param> public static void WriteLog(EnLogType logtype, string message) { string filePrefix; switch (logtype) { case EnLogType.Common: filePrefix = "Access_"; break; case EnLogType.Error: filePrefix = "Error_"; break; case EnLogType.Success: filePrefix = "Sucess_"; break; case EnLogType.Warn: filePrefix = "Warn_"; break; case EnLogType.Run: filePrefix = "Run_"; break; default: filePrefix = "Error_"; break; } string path; try { //path = System.Web.HttpContext.Current.Server.MapPath("~/") + ConfigurationSettings.AppSettings["LogPath"] + "\\"; //F:\Web-单云龙\复件 WH\WHweb\ path = System.AppDomain.CurrentDomain.BaseDirectory + ConfigurationSettings.AppSettings["LogPath"]; //"c:\\Log\\"; } catch (Exception e) { return; } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } lock (privateObjectLock) { try { string FILE_NAME = path + filePrefix + DateTime.Now.ToString("yyyyMMdd") + ".txt"; if (File.Exists(FILE_NAME)) { StreamWriter sr = File.AppendText(FILE_NAME); sr.WriteLine(DateTime.Now.ToString() + ": " + message); sr.Close(); } else { StreamWriter sr = File.CreateText(FILE_NAME); sr.WriteLine(DateTime.Now.ToString() + ": " + message); sr.Close(); } } catch { } } }