Handle log rolling better by checking if filesize gets smaller or creation date is greater.

This commit is contained in:
Eric Fontana
2014-10-16 06:51:20 -04:00
parent 6a483946ef
commit 303d21d241
2 changed files with 54 additions and 32 deletions

View File

@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.5.0")]
[assembly: AssemblyFileVersion("1.3.5.0")]
[assembly: AssemblyVersion("1.3.6.0")]
[assembly: AssemblyFileVersion("1.3.6.0")]

View File

@@ -6,7 +6,7 @@ using System.Threading;
using System.Threading.Tasks;
using System.IO;
using Interop.MSUtil;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
@@ -26,10 +26,17 @@ namespace TimberWinR.Inputs
private int _pollingIntervalInSeconds;
private TimberWinR.Parser.Log _arguments;
private long _receivedMessages;
private Dictionary<string, Int64> _logFileMaxRecords;
private Dictionary<string, DateTime> _logFileCreationTimes;
private Dictionary<string, long> _logFileSizes;
public LogsListener(TimberWinR.Parser.Log arguments, CancellationToken cancelToken, int pollingIntervalInSeconds = 3)
: base(cancelToken, "Win32-FileLog")
{
_logFileMaxRecords = new Dictionary<string, Int64>();
_logFileCreationTimes = new Dictionary<string, DateTime>();
_logFileSizes = new Dictionary<string, long>();
_receivedMessages = 0;
_arguments = arguments;
_pollingIntervalInSeconds = pollingIntervalInSeconds;
@@ -55,8 +62,18 @@ namespace TimberWinR.Inputs
new JProperty("location", _arguments.Location),
new JProperty("codepage", _arguments.CodePage),
new JProperty("splitLongLines", _arguments.SplitLongLines),
new JProperty("recurse", _arguments.Recurse)
new JProperty("recurse", _arguments.Recurse),
new JProperty("files",
new JArray(from f in _logFileMaxRecords.Keys
select new JValue(f))),
new JProperty("fileIndices",
new JArray(from f in _logFileMaxRecords.Values
select new JValue(f))),
new JProperty("fileCreationDates",
new JArray(from f in _logFileCreationTimes.Values
select new JValue(f)))
)));
return json;
}
@@ -69,8 +86,6 @@ namespace TimberWinR.Inputs
recurse = _arguments.Recurse
};
Dictionary<string, Int64> logFileMaxRecords = new Dictionary<string, Int64>();
Dictionary<string, DateTime> logFileCreationTimes = new Dictionary<string, DateTime>();
// Execute the query
while (!CancelToken.IsCancellationRequested)
@@ -90,23 +105,30 @@ namespace TimberWinR.Inputs
fi.Refresh();
DateTime creationTime = fi.CreationTimeUtc;
bool logHasRolled = logFileCreationTimes.ContainsKey(logName) && creationTime > logFileCreationTimes[logName];
bool logHasRolled = (_logFileCreationTimes.ContainsKey(logName) &&
creationTime > _logFileCreationTimes[logName]) ||
(_logFileSizes.ContainsKey(logName) && fi.Length < _logFileSizes[logName]);
if (!logFileMaxRecords.ContainsKey(logName) || logHasRolled)
if (!_logFileMaxRecords.ContainsKey(logName) || logHasRolled)
{
logFileCreationTimes[logName] = creationTime;
_logFileCreationTimes[logName] = creationTime;
_logFileSizes[logName] = fi.Length;
var qcount = string.Format("SELECT max(Index) as MaxRecordNumber FROM {0}", logName);
var rcount = oLogQuery.Execute(qcount, iFmt);
var qr = rcount.getRecord();
var lrn = (Int64)qr.getValueEx("MaxRecordNumber");
if (logHasRolled)
lrn = 0;
logFileMaxRecords[logName] = lrn;
}
}
foreach (string fileName in logFileMaxRecords.Keys.ToList())
{
var lastRecordNumber = logFileMaxRecords[fileName];
LogManager.GetCurrentClassLogger().Info("Log {0} has rolled", logName);
lrn = 0;
}
_logFileMaxRecords[logName] = lrn;
}
}
foreach (string fileName in _logFileMaxRecords.Keys.ToList())
{
var lastRecordNumber = _logFileMaxRecords[fileName];
var query = string.Format("SELECT * FROM {0} where Index > {1}", fileName, lastRecordNumber);
var rs = oLogQuery.Execute(query, iFmt);
@@ -144,7 +166,7 @@ namespace TimberWinR.Inputs
}
var lrn = (Int64)record.getValueEx("Index");
logFileMaxRecords[fileName] = lrn;
_logFileMaxRecords[fileName] = lrn;
}
// Close the recordset