Skip to content

MSsql sink writes times as local time #392 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static class LoggerConfigurationMSSqlServerExtensions
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="storeTimestampInUtc">Store Timestamp In UTC</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration MSSqlServer(
Expand All @@ -45,14 +46,15 @@ public static LoggerConfiguration MSSqlServer(
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit,
TimeSpan? period = null,
IFormatProvider formatProvider = null)
IFormatProvider formatProvider = null,
bool storeTimestampInUtc = false)
{
if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration");

var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;

return loggerConfiguration.Sink(
new MSSqlServerSink( connectionString, tableName, storeProperties,batchPostingLimit, defaultedPeriod, formatProvider),
new MSSqlServerSink(connectionString, tableName, storeProperties, batchPostingLimit, defaultedPeriod, formatProvider, storeTimestampInUtc),
restrictedToMinimumLevel);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class MSSqlServerSink : PeriodicBatchingSink
readonly bool _includeProperties;
readonly string _tableName;
readonly CancellationTokenSource _token = new CancellationTokenSource();
readonly bool _storeTimestampInUtc;

/// <summary>
/// Construct a sink posting to the specified database.
Expand All @@ -57,8 +58,9 @@ public class MSSqlServerSink : PeriodicBatchingSink
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="storeTimestampInUtc">Store Timestamp In UTC</param>
public MSSqlServerSink(string connectionString, string tableName, bool includeProperties, int batchPostingLimit,
TimeSpan period, IFormatProvider formatProvider)
TimeSpan period, IFormatProvider formatProvider, bool storeTimestampInUtc)
: base(batchPostingLimit, period)
{
if (string.IsNullOrWhiteSpace(connectionString))
Expand All @@ -72,6 +74,7 @@ public MSSqlServerSink(string connectionString, string tableName, bool includePr
_tableName = tableName;
_includeProperties = includeProperties;
_formatProvider = formatProvider;
_storeTimestampInUtc = storeTimestampInUtc;

// Prepare the data table
_eventsTable = CreateDataTable();
Expand Down Expand Up @@ -177,7 +180,8 @@ void FillDataTable(IEnumerable<LogEvent> events)
row["Message"] = logEvent.RenderMessage(_formatProvider);
row["MessageTemplate"] = logEvent.MessageTemplate;
row["Level"] = logEvent.Level;
row["TimeStamp"] = logEvent.Timestamp.DateTime;
row["TimeStamp"] = (_storeTimestampInUtc) ? logEvent.Timestamp.DateTime.ToUniversalTime()
: logEvent.Timestamp.DateTime;
row["Exception"] = logEvent.Exception != null ? logEvent.Exception.ToString() : null;

if (_includeProperties)
Expand Down