private void ThreadPoolSaveText(object obj) { // 进入文件操作的锁 m_fileSaveLock.Enter(); //获取要存储的文件名称 string LogSaveFileName = GetFileSaveName(); if (!string.IsNullOrEmpty(LogSaveFileName)) { // 保存 StreamWriter sw = null; try { sw = new StreamWriter(LogSaveFileName, true, Encoding.UTF8); string str = obj as string; sw.Write(str); } catch (Exception ex) { AddItemToCache(new HslMessageItem() { Degree = HslMessageDegree.FATAL, Text = LogNetManagment.GetSaveStringFromException(null, ex), }); } finally { sw?.Dispose(); } } // 释放锁 m_fileSaveLock.Leave(); }
private string HslMessageFormate(HslMessageItem hslMessage) { StringBuilder stringBuilder = new StringBuilder( ); if (hslMessage.Degree != HslMessageDegree.None) { stringBuilder.Append("\u0002"); stringBuilder.Append("["); stringBuilder.Append(LogNetManagment.GetDegreeDescription(hslMessage.Degree)); stringBuilder.Append("] "); stringBuilder.Append(hslMessage.Time.ToString("yyyy-MM-dd HH:mm:ss.fff")); stringBuilder.Append(" thread:["); stringBuilder.Append(hslMessage.ThreadId.ToString("D2")); stringBuilder.Append("] "); if (!string.IsNullOrEmpty(hslMessage.KeyWord)) { stringBuilder.Append(hslMessage.KeyWord); stringBuilder.Append(" : "); } } stringBuilder.Append(hslMessage.Text); return(stringBuilder.ToString()); }
private void ThreadPoolSaveFile(object obj) { // 获取需要存储的日志 HslMessageItem current = GetAndRemoveLogItem(); // 进入文件操作的锁 m_fileSaveLock.Enter(); // 获取要存储的文件名称 string LogSaveFileName = GetFileSaveName(); if (!string.IsNullOrEmpty(LogSaveFileName)) { // 保存 StreamWriter sw = null; try { sw = new StreamWriter(LogSaveFileName, true, Encoding.UTF8); while (current != null) { // 触发事件 OnBeforeSaveToFile(new HslEventArgs( ) { HslMessage = current }); sw.Write(HslMessageFormate(current)); sw.Write(Environment.NewLine); current = GetAndRemoveLogItem(); } } catch (Exception ex) { AddItemToCache(current); AddItemToCache(new HslMessageItem() { Degree = HslMessageDegree.FATAL, Text = LogNetManagment.GetSaveStringFromException("LogNetSelf", ex), }); } finally { sw?.Dispose(); } } // 释放锁 m_fileSaveLock.Leave(); Interlocked.Exchange(ref m_SaveStatus, 0); // 再次检测锁是否释放完成 if (m_WaitForSave.Count > 0) { StartSaveFile(); } }
private string HslMessageFormate(HslMessageItem hslMessage) { StringBuilder stringBuilder = new StringBuilder("\u0002"); stringBuilder.Append("["); stringBuilder.Append(LogNetManagment.GetDegreeDescription(hslMessage.Degree)); stringBuilder.Append("] "); stringBuilder.Append(hslMessage.Time.ToString("yyyy-MM-dd HH:mm:ss.fff")); stringBuilder.Append(" thread:["); stringBuilder.Append(hslMessage.ThreadId.ToString("D2")); stringBuilder.Append("] "); stringBuilder.Append(hslMessage.Text); return(stringBuilder.ToString()); }
/// <summary> /// 写入一条异常信息 /// </summary> /// <param name="text"></param> /// <param name="ex"></param> public void WriteException(string text, Exception ex) { RecordMessage(HslMessageDegree.FATAL, LogNetManagment.GetSaveStringFromException(text, ex)); }