diff --git a/src/Marten/DefaultMartenLogger.cs b/src/Marten/DefaultMartenLogger.cs index 0d53880d76..22860005e5 100644 --- a/src/Marten/DefaultMartenLogger.cs +++ b/src/Marten/DefaultMartenLogger.cs @@ -35,7 +35,7 @@ public void LogSuccess(NpgsqlCommand command) { if (Inner.IsEnabled(LogLevel.Debug)) { - var duration = _timestamp.HasValue ? Stopwatch.GetElapsedTime(_timestamp!.Value) : 0.Seconds(); + var duration = GetDuration(); var parameters = command.Parameters .Select(p => $" {p.ParameterName}: {p.Value}") .Join(Environment.NewLine); @@ -47,7 +47,7 @@ public void LogSuccess(NpgsqlBatch batch) { if (Inner.IsEnabled(LogLevel.Debug)) { - var duration = Stopwatch.GetElapsedTime(_timestamp!.Value); + var duration = GetDuration(); foreach (var command in batch.BatchCommands) { var parameters = command.Parameters.OfType() @@ -88,7 +88,7 @@ public void RecordSavedChanges(IDocumentSession session, IChangeSet commit) { if (Inner.IsEnabled(LogLevel.Debug)) { - var duration = _timestamp.HasValue ? Stopwatch.GetElapsedTime(_timestamp!.Value) : 0.Seconds(); + var duration = GetDuration(); _loggerOutput.RecordSavedChanges(commit.Updated.Count(), duration.TotalMilliseconds, commit.Inserted.Count(), @@ -111,6 +111,16 @@ public void OnBeforeExecute(NpgsqlBatch batch) _timestamp = Stopwatch.GetTimestamp(); } } + + private TimeSpan GetDuration() + { + if (_timestamp.HasValue) + { + return Stopwatch.GetElapsedTime(_timestamp.Value); + } + + return 0.Seconds(); + } } internal sealed partial class DefaultLoggingWriter