Ejemplo n.º 1
0
 /// <summary>
 /// Writes a group of <see cref="ILogEntry"/>s to the log.
 /// </summary>
 /// <param name="logEntries">The list of <see cref="ILogEntry"/>s to write.</param>
 public override void Write(IEnumerable <ILogEntry> logEntries)
 {
     if (logEntries == null)
     {
         throw new ArgumentNullException(nameof(logEntries));
     }
     if (logEntries.FirstOrDefault() != default(ILogEntry))
     {
         lock (SyncObject)
         {
             using (var sw = new System.IO.StreamWriter(LogFilename, true))
             {
                 foreach (var entry in logEntries)
                 {
                     if (entry != null)
                     {
                         sw.WriteLine(LoggingHelper.FormatLogLine(entry, _WriteLogEntriesUsingLocalTime));
                         ++_LogStats.LogWrites;
                     }
                 }
             }
             if ((AutoTruncateEnabled) && ((new System.IO.FileInfo(LogFilename)).Length > _MaxLogSizeBytes))
             {
                 TruncateLogs();
             }
         }
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Will truncate log entries from the beginning of the log file.
 /// </summary>
 /// <returns>The number of logs removed.</returns>
 public int TruncateLogs()
 {
     if (Exists)
     {
         if (_LogsToRetain <= 0)
         {
             LastTruncatedCount = LogCount;
             Create();
             LastTruncatedDate = DateTime.Now;
             ++AutoTruncateGeneration;
         }
         else
         {
             lock (SyncObject)
             {
                 var fileInfo   = new System.IO.FileInfo(LogFilename);
                 var totalLines = LogCount;
                 if (totalLines > _LogsToRetain)
                 {
                     int skipLines = totalLines - _LogsToRetain;
                     LastTruncatedCount   = skipLines;
                     _TotalTruncatedLogs += LastTruncatedCount;
                     var tempFilename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(LogFilename), Guid.NewGuid().ToString());
                     using (var sw = new System.IO.StreamWriter(tempFilename, false))
                     {
                         foreach (var entry in Read())
                         {
                             if (--skipLines < 0)
                             {
                                 if (entry != null)
                                 {
                                     sw.WriteLine(LoggingHelper.FormatLogLine(entry, _WriteLogEntriesUsingLocalTime));
                                 }
                             }
                         }
                     }
                     Delete();
                     System.IO.File.Move(tempFilename, LogFilename);
                 }
                 LastTruncatedDate = DateTime.Now;
                 ++AutoTruncateGeneration;
             }
         }
     }
     if (LastTruncatedCount > LargestTruncatedCount)
     {
         LargestTruncatedCount = LastTruncatedCount;
     }
     return(LastTruncatedCount);
 }