Skip to content

Commit

Permalink
- Add 'finished in {0:0}ms' to end of operation logs so we can see ho…
Browse files Browse the repository at this point in the history
…w long they take (#1026)

Co-authored-by: George Kinsman <george.kinsman@justeattakeaway.com>
  • Loading branch information
gkinsman and George Kinsman authored Jun 15, 2022
1 parent 76f1550 commit 63a5976
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/JustSaying/Messaging/Monitoring/LogOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal sealed class LogOperation : IDisposable
private readonly Stopwatch _watch;
private readonly LogLevel _logLevel;

private const string MessageTemplate = " finished in {0:0}ms";

public LogOperation(ILogger logger, LogLevel logLevel, string message, params object[] args)
{
_logger = logger;
Expand All @@ -24,30 +26,31 @@ public void Dispose()
{
_watch.Stop();

var args = _args.Concat(new object[] { _watch.Elapsed }).ToArray();
var message = $"{_message}{MessageTemplate}";
var args = _args.Concat(new object[] { _watch.Elapsed.TotalMilliseconds }).ToArray();

switch (_logLevel)
{
case LogLevel.Trace:
_logger.LogTrace(_message, args);
_logger.LogTrace(message, args);
return;
case LogLevel.Debug:
_logger.LogDebug(_message, args);
_logger.LogDebug(message, args);
return;
case LogLevel.Information:
_logger.LogInformation(_message, args);
_logger.LogInformation(message, args);
return;
case LogLevel.Warning:
_logger.LogWarning(_message, args);
_logger.LogWarning(message, args);
return;
case LogLevel.Error:
_logger.LogError(_message, args);
_logger.LogError(message, args);
return;
case LogLevel.Critical:
_logger.LogCritical(_message, args);
_logger.LogCritical(message, args);
return;
case LogLevel.None:
return;
}
}
}
}

0 comments on commit 63a5976

Please sign in to comment.