/// <summary> /// filePrefixName是文件名前缀,最好用中文,方便在程序Logs文件下查看。 /// </summary> /// <Param name="content">内容。如需换行可使用:\r\n</Param> /// <Param name="filePrefixName"></Param> /// <Param name="path"></Param> /// <Param name="logtype"></Param> private static void Write(LogHelperType logtype, string content, string filePrefixName = null, string path = null) { lock (Olock) { try { switch (logtype) { case LogHelperType.debug: { var dosDebug = ConfigurationManager.AppSettings["GSMMODEM.Debug"]; if (dosDebug == null || !"true".Equals(dosDebug)) { return; } else { break; } } case LogHelperType.error: { var dosError = ConfigurationManager.AppSettings["GSMMODEM.Error"]; if (dosError == null || !"true".Equals(dosError)) { return; } else { break; } } case LogHelperType.write: { var dosWrite = ConfigurationManager.AppSettings["GSMMODEM.Write"]; if (dosWrite == null || !"true".Equals(dosWrite)) { return; } else { break; } } } #region 日志文件 var fileName = filePrefixName + "_" + DateTime.Now.ToString("yyyyMMdd") + logtype.ToString() + ".txt"; if (string.IsNullOrWhiteSpace(path)) { var dosPath = ConfigurationManager.AppSettings["GSMMODEM.Path"]; if (string.IsNullOrWhiteSpace(dosPath)) { path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\" + fileName; } else { path = dosPath + fileName; } } else { path += fileName; } var di = new DirectoryInfo(path.Replace(fileName, "")); if (!di.Exists) { di.Create(); } //判断文件大小,需要新开文件 using (var fs = new FileStream(path, FileMode.Append, FileAccess.Write)) { var sw = new StreamWriter(fs); sw.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); sw.WriteLine(); sw.Write(content); sw.WriteLine(); sw.Write("-----------------------------------------------------------------------------"); sw.WriteLine(); sw.Flush(); sw.Close(); } #endregion } catch { } } }
/// <summary> /// filePrefixName是文件名前缀,最好用中文,方便在程序Logs文件下查看。 /// </summary> /// <param name="content"></param> /// <param name="filePrefixName"></param> /// <param name="path"></param> /// <param name="logtype"></param> private static void Write(LogHelperType logtype, string content, string filePrefixName = null, string path = null) { try { if (logtype == LogHelperType.debug) { var dosDebug = ConfigurationManager.AppSettings["DosLogHelperDebug"]; if (dosDebug != null && dosDebug != "1") { return; } } else { var dosError = ConfigurationManager.AppSettings["DosLogHelperError"]; if (dosError != null && dosError != "1") { return; } } #region 日志文件 var fileName = filePrefixName + DateTime.Now.ToString("yyyyMMdd") + logtype.ToString() + ".txt"; if (string.IsNullOrWhiteSpace(path)) { var dosPath = ConfigurationManager.AppSettings["DosLogHelperPath"]; if (string.IsNullOrWhiteSpace(dosPath)) { path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\" + fileName; } else { path = dosPath + fileName; } } else { path += fileName; } var di = new DirectoryInfo(path.Replace(fileName, "")); if (!di.Exists) { di.Create(); } //判断文件大小,需要新开文件 using (var fs = new FileStream(path, FileMode.Append, FileAccess.Write)) { var sw = new StreamWriter(fs); sw.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); sw.WriteLine(); sw.Write(content); sw.WriteLine(); sw.Write("-----------------------------------------------------------------------------"); sw.WriteLine(); sw.Flush(); sw.Close(); } #endregion } catch { } }