So far I always used the Stopwatch class to track timings in my applications and just added the result to my log message. Until I discovered the SerilogTimings nuget package.
Usage is simple, after you have configured Serilog, you can use Operation.Time() to time an operation:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (Operation.Time("Submitting payment for {OrderId}", order.Id)) | |
{ | |
// Timed block of code goes here | |
} |
At the completion of the using
block(!), a message will be written to the log like:
[INF] Submitting payment for order-12345 completed in 456.7 ms
You can also use it directly on top of an ILogger instance:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (logger.TimeOperation("Submitting payment for {OrderId}", order.Id)) | |
{ | |
// Timed block of code goes here | |
} |
More info about this library can be found here: https://github.com/nblumhardt/serilog-timings