public void SetPersistParam(String setting) { _theStreamFileSplitManager = new StreamFileSplitManager(setting); _fileName = _theStreamFileSplitManager.GetNewFileName(1); }
/// <summary> /// /// </summary> /// <param name="logcfg"></param> public StreamFileLPS(LogServiceConfig logcfg) { LogConfig = logcfg; _theStreamFileSplitManager = new StreamFileSplitManager(TXTLogFactory.GetLogFileName(logcfg)); _fileName = _theStreamFileSplitManager.GetNewFileName(1); }
public void Save(List <LogRecord> logRecordList) { lock (logRecordList) { var filePath = TXTLogFactory.GetLogFilePath(LogConfig) + TXTLogFactory.GetLogSubFolder(LogConfig); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } var file = filePath + "\\" + _fileName; if (_theStreamFileSplitManager.IsFristRun) { bool rlt; do { file = filePath + "\\" + _fileName; rlt = File.Exists(file); if (!rlt) { continue; } if (_theStreamFileSplitManager.IsAppend) { var fi = new FileInfo(file); if ((UInt64)fi.Length >= _theStreamFileSplitManager.FileMaxSizeBytes) { _theStreamFileSplitManager.AddSeqNo(1); _fileName = _theStreamFileSplitManager.GetNewFileName(1); } else { break; } } else { _theStreamFileSplitManager.AddSeqNo(1); _fileName = _theStreamFileSplitManager.GetNewFileName(1); } } while (rlt); _theStreamFileSplitManager.IsFristRun = false; } var streamWrite = new StreamWriter(file, true); try { //多线程可能导致集合不同步 //foreach (var record in logRecordList //{ // var recByteSize = (UInt16)(Encoding.Default.GetByteCount(record.LogMessage) + 2); // var tempfileName = _theStreamFileSplitManager.GetNewFileName(recByteSize); // if (tempfileName != _fileName) // { // streamWrite.Flush(); // streamWrite.Close(); // _fileName = tempfileName; // file = filePath + "\\" + _fileName; // streamWrite = new StreamWriter(file, true); // } // streamWrite.WriteLine(record.LogMessage); //} do { var record = logRecordList[0]; if (record == null || record.LogMessage == null) { logRecordList.Remove(record); continue; } var recByteSize = (UInt16)(Encoding.Default.GetByteCount(record.LogMessage) + 2); var tempfileName = _theStreamFileSplitManager.GetNewFileName(recByteSize); if (tempfileName != _fileName) { streamWrite.Flush(); streamWrite.Close(); _fileName = tempfileName; file = filePath + "\\" + _fileName; streamWrite = new StreamWriter(file, true); } streamWrite.WriteLine(record.LogMessage); logRecordList.Remove(record); } while (logRecordList.Count > 0); //logRecordList.Clear(); streamWrite.Flush(); streamWrite.Close(); } catch (Exception ex) { //throw ExceptionHelper.GetException(20002001, "TXTLogService_Save", ex); } finally { streamWrite.Close(); } } }