/// <summary> /// This is the one for all Ctor, being called from any other Ctor /// </summary> /// <param name="pFileName">The file name fo the log file</param> /// <param name="pFilePath">The path to the log file</param> /// <param name="pComponent">The component to log this under</param> /// <param name="pLevel">The level the tracemanager is on</param> /// <param name="pLogCache">The number of log entries to cache before writing to disk</param> /// <param name="pReadMode">Indicates if the traceManager is instanciated to read a log file</param> /// <param name="pMaxFileSize">The maximum size of a log file before rolling over to a new one</param> /// <param name="pIndent">The indent string to apply</param> public TraceManager(string pFileName, string pFilePath, string pComponent, int pLevel, int pLogCache = 2000, bool pReadMode = false, long pMaxFileSize = 763363328, string pIndent = " ") { isReadMode = pReadMode; recentLogFileName = pFileName; logPath = pFilePath; logMessages = new List<LogMessage>(); if (!isReadMode) { dateFolderName = DateTime.Now.ToString("yyyyMMdd"); if (pFilePath.Length < 2) logPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\PlexByte\\" + dateFolderName; else logPath = pFilePath + "\\" + dateFolderName; SetTraceLevel(pLevel); if (pComponent.Length < 2) Component = Assembly.GetCallingAssembly().GetName().Name; else Component = pComponent; logCache = pLogCache; maxFileSize = pMaxFileSize; indentPrefix = pIndent; activeLog = InitLogFile(false, logFileIndex); logMessages.Add(new LogMessage("Logging started for component " + Component, MessageType.All, 0, Thread.CurrentThread.ManagedThreadId.ToString(), "Initializing")); } else { activeLog = InitLogFile(false, 0); maxFileSize = activeLog.Size; } }
/// <summary> /// Initializes the current log file and rolls over if required /// </summary> /// <param name="pIsRollover">Indicates whether to rollover or not</param> /// <param name="pLogIndex">The index of the current log</param> /// <returns>Return the LogFile created</returns> private LogFile InitLogFile(bool pIsRollover, int pLogIndex) { // Purge logs PurgeLogs(7); string lockMessage = string.Empty; LogFile log = null; if (pIsRollover) { try { // Try to get a lock if (AcquireLock(1000, out lockMessage)) { // Day rollover start reinitializing... logMessages.Add( new LogMessage("Trace day rollover end of day @ " + DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss.fff"), MessageType.All, 0, "")); activeLog.WriteLog(logMessages); int dummy = 0; if (ZipLogFiles) { // Call the zip log file method asynchronously AsyncZipLogFile logZipper = new AsyncZipLogFile(this.ZipLogFile); IAsyncResult result = logZipper.BeginInvoke(LogPath + "\\" + Path.GetFileNameWithoutExtension(LogPath + "\\" + RecentLogFileName) + ".zip", CompressionLevel.Optimal, out dummy, new AsyncCallback(AsyncLogZippedCallback), null); } // New day? if (DateFolderName != DateTime.Now.ToString("yyyyMMdd")) { logPath = logPath.Replace(dateFolderName, dateFolderName = DateTime.Now.ToString("yyyyMMdd")); recentLogFileName = recentLogFileName.Replace(pLogIndex.ToString(), (logFileIndex = 0).ToString()); OnLogFileRolledOver(RolloverType.DayRollover); } else { recentLogFileName = recentLogFileName.Replace(pLogIndex.ToString(), (++logFileIndex).ToString()); OnLogFileRolledOver(RolloverType.SizeRollover); } log = new LogFile(RecentLogFileName, LogPath); Log(new LogMessage("Trace file rolled over to " + RecentLogFileName + " in path " + LogPath + "[TraceLevel=" + TraceLevel.ToString() + "] [Component=" + Component + "] [IsReadMode=" + IsReadMode.ToString() + "] [Cache=" + LogCache.ToString() + "] [MaxSize=" + MaxFileSize.ToString() + "]")); } else throw new Exception("Failed to accuire lock when rolling over to new log file..."); } finally { if (isLocked) if (!ReleaseLock(out lockMessage)) throw new Exception(lockMessage); } } else { try { // Try to get a lock if (AcquireLock(1000, out lockMessage)) { log = new LogFile(RecentLogFileName, LogPath); logFileIndex = 0; } else throw new Exception("Failed to accuire lock when trying to create a new log file..."); } finally { if (isLocked) if (!ReleaseLock(out lockMessage)) throw new Exception(lockMessage); } } return log; }
/// <summary> /// This method reads the log file specified /// </summary> /// <param name="pFileFullPath">The full path including file name and extension</param> /// <returns>Returns the list of messages from the file</returns> public List<LogMessage> ReadLogFile(string pFileFullPath, out long pNumberOfMessages) { if(activeLog==null) activeLog = new LogFile(Path.GetFileName(pFileFullPath), Path.GetDirectoryName(pFileFullPath)); logMessages.AddRange(activeLog.ReadFile(out pNumberOfMessages)); return logMessages; }
/// <summary> /// Dtor, cleans up resources /// </summary> public void Dispose() { try { Monitor.TryEnter(_LockObject, 1000, ref isLocked); if (!isLocked) throw new Exception("Failed to accuire lock when rolling over to new log file..."); if (activeLog != null) { activeLog.WriteLog(logMessages); activeLog.WriteLog(new LogMessage("Logging stopped for component " + Component, MessageType.All, 0, Thread.CurrentThread.ManagedThreadId.ToString(), "Dispose")); activeLog.MessagesAdded -= ActiveLog_MessagesAdded; activeLog = null; } if (logMessages != null) { logMessages.Clear(); logMessages = null; } } finally { if (isLocked) { Monitor.Exit(_LockObject); isLocked = false; } } }