Skip to content

Commit

Permalink
Merge pull request saleem-mirza#14 from steronydh/dev
Browse files Browse the repository at this point in the history
Timestamps written in SQLite-compatible format. Fixes saleem-mirza#13.
  • Loading branch information
saleem-mirza authored Jun 11, 2018
2 parents 9c1bbda + b23089c commit d68b8ec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static LoggerConfiguration SQLite(
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null,
bool storeTimestampInUtc = false,
TimeSpan? retentionPeriod = null)
TimeSpan? retentionPeriod = null,
TimeSpan? retentionCheckInterval = null)
{
if (loggerConfiguration == null)
{
Expand Down Expand Up @@ -82,7 +83,8 @@ public static LoggerConfiguration SQLite(
tableName,
formatProvider,
storeTimestampInUtc,
retentionPeriod),
retentionPeriod,
retentionCheckInterval),
restrictedToMinimumLevel);
}
catch (Exception ex)
Expand Down
19 changes: 13 additions & 6 deletions src/Serilog.Sinks.SQLite/Sinks/SQLite/SQLiteSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,29 @@ internal class SQLiteSink : BatchProvider, ILogEventSink
private readonly string _tableName;
private readonly TimeSpan? _retentionPeriod;
private readonly Stopwatch _retentionWatch = new Stopwatch();
private readonly TimeSpan? _retentionCheckInterval;

public SQLiteSink(string sqlLiteDbPath,
string tableName,
IFormatProvider formatProvider,
bool storeTimestampInUtc,
TimeSpan? retentionPeriod)
TimeSpan? retentionPeriod,
TimeSpan? retentionCheckInterval)
{
_connString = CreateConnectionString(sqlLiteDbPath);
_tableName = tableName;
_formatProvider = formatProvider;
_storeTimestampInUtc = storeTimestampInUtc;

if (retentionPeriod.HasValue)
{
// impose a min retention period of 1 minute
_retentionPeriod = new[] { retentionPeriod.Value, TimeSpan.FromMinutes(1) }.Max();


// check for retention at this interval - or use retentionPeriod if not specified
_retentionCheckInterval = retentionCheckInterval ?? _retentionPeriod.Value;
}

InitializeDatabase();
}

Expand Down Expand Up @@ -131,8 +138,8 @@ protected override void WriteLogEvent(ICollection<LogEvent> logEventsBatch)
foreach (var logEvent in logEventsBatch)
{
sqlCommand.Parameters["@timeStamp"].Value = _storeTimestampInUtc
? logEvent.Timestamp.ToUniversalTime()
: logEvent.Timestamp;
? logEvent.Timestamp.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss")
: logEvent.Timestamp.ToString("yyyy-MM-ddTHH:mm:ss");
sqlCommand.Parameters["@level"].Value = logEvent.Level.ToString();
sqlCommand.Parameters["@exception"].Value = logEvent.Exception?.ToString() ?? string.Empty;
sqlCommand.Parameters["@renderedMessage"].Value = logEvent.MessageTemplate.ToString();
Expand Down Expand Up @@ -162,7 +169,7 @@ private void ApplyRetentionPolicy(SQLiteConnection sqlConnection)
// there is no retention policy
return;

if (_retentionWatch.IsRunning && _retentionWatch.Elapsed < _retentionPeriod.Value)
if (_retentionWatch.IsRunning && _retentionWatch.Elapsed < _retentionCheckInterval.Value)
// Besides deleting records older than X
// let's only delete records every X often
// because of the check whether the _retentionWatch is running,
Expand All @@ -186,7 +193,7 @@ private SQLiteCommand CreateSqlDeleteCommand(SQLiteConnection sqlConnection, Dat
cmd.CommandText = $"DELETE FROM {_tableName} WHERE Timestamp < @epoch";
cmd.Parameters.Add(new SQLiteParameter("@epoch", DbType.DateTime2)
{
Value = _storeTimestampInUtc ? epoch.ToUniversalTime() : epoch
Value = (_storeTimestampInUtc ? epoch.ToUniversalTime() : epoch).ToString("yyyy-MM-ddTHH:mm:ss")
});
return cmd;
}
Expand Down

0 comments on commit d68b8ec

Please sign in to comment.