using System; using System.Collections.Generic; using System.Text; namespace Shared.Phone { /// /// Log逻辑 /// public class HdlLogLogic { #region ■ 变量声明___________________________ /// /// Log逻辑 /// private static HdlLogLogic m_Current = null; /// /// Log逻辑 /// public static HdlLogLogic Current { get { if (m_Current == null) { m_Current = new HdlLogLogic(); } return m_Current; } } /// /// 锁 /// private object objLock = new object(); #endregion #region ■ Log出力____________________________ /// /// 调试用,追加写入其他Log /// /// 全路径 /// 要写入的内容 /// 是否先删除文件 /// 写入的内容是否加密 public void WriteOtherText(string fullName, string i_text, bool deleteFile, bool encrypt) { if (deleteFile == true) { HdlFileLogic.Current.DeleteFile(fullName); } System.IO.StreamWriter sw = null; try { string strLog = "[" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "] " + i_text; if (encrypt == true) { strLog = HdlCommonLogic.Current.EncryptPassword(HdlUserCenterResourse.FileEncryptKey, strLog); } strLog = "\r\n" + strLog + "\r\n"; sw = new System.IO.StreamWriter(fullName, true, Encoding.UTF8); sw.WriteLine(strLog); } catch { } finally { sw?.Close(); sw = null; } } /// /// Log出力 /// /// /// 附加消息 public void WriteLog(Exception ex, string appendMsg = "") { //Log出力 string msg = appendMsg == "" ? string.Empty : appendMsg + "\r\n"; msg += ex.Message + "\r\n"; msg += ex.StackTrace; this.WriteLog(-1, msg); } /// /// Log出力 /// /// 1:普通Log,-1:致命错误Log,2,3:特殊Log /// Log内容 public void WriteLog(int div, string strLog) { lock (objLock) { if (div == 1 && HdlUserCenterResourse.HideOption.DetailedLog == 0) { //暂时只记录异常信息 return; } //Log文件 System.IO.StreamWriter sw = null; try { string fullName = string.Empty; if (div == 1 || div == -1) { //创建LOG文件夹 HdlFileLogic.Current.CreateDirectory(HdlFileNameResourse.LogDirectory); string fileName = this.GetLogFile(div); fullName = System.IO.Path.Combine(HdlFileNameResourse.LogDirectory, fileName); } else if (div == 2) { fullName = HdlFileNameResourse.SendAndReceveDataLog; } else if (div == 3) { fullName = HdlFileNameResourse.SocketReceiveDataLog; } strLog = "\r\n[" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "] " + strLog + "\r\n"; sw = new System.IO.StreamWriter(fullName, true, Encoding.UTF8); sw.WriteLine(strLog); } catch { } finally { sw?.Close(); sw = null; } } } /// /// 获取LOG文件 /// /// 1:普通Log,-1:致命错误Log /// private string GetLogFile(int div) { //加密,因为这是收集数据,最好不让别人知道这是什么最好 string fileName = DateTime.Now.ToString("yyyyMMdd") + "Log"; if (HdlUserCenterResourse.UserInfo.Account != string.Empty) { fileName = HdlUserCenterResourse.UserInfo.Account + "-" + fileName; } fileName = HdlCommonLogic.Current.EncryptPassword(HdlUserCenterResourse.FileEncryptKey, fileName); return fileName; } #endregion } }