Reputation: 401
Nicholas Blumhardt's Serilog timigs (or on the GitHub: serilog-timings) is a convenient tool for logging the duration of different operations. However - at least as far as I see - it is designed for logging only at the end of the operation (by Operation.Complete()
method, or at quitting from the using{} block due to an exception. Since nested operations end in reverse order than they start, logging nested operation would be clearer when we log the starts too. I'd like to get similar log items that can be achieved by this code:
using (var op = Operation.Begin("ended {OperationID}, {OperationName}, {User}", 123, "Main operation", "Benny Hill"))
{
Log.Information("started {OperationID}, {OperationName}, {User}", 123, "Main operation", "Benny Hill");
Log.Information("... some other log items....");
op.Complete("Returned", 1230);
}
, that is, the result should look like this:
Unfortunately, the code above isn't too convenient because the same properties must be given to the Log.Informatrion()
that already were given to Operation.Begin()
. Is there already a solution for using those parameters elsewhere than in Operation.Complete
, or should I add a new method (e.g. Operaton.LogStart()
) into serilog-timings
?
Upvotes: 1
Views: 1172
Reputation: 31832
SerilogTimings doesn't provide this, but you can wrap it in an extension of your own that does:
using Serilog;
using SerilogTimings;
using SerilogTimings.Extensions;
class OperationEx
{
public static Operation Begin(string messageTemplate, params object[] args)
{
var op = Log.Logger.BeginOperation(messageTemplate, args);
Log.Logger.Information($"{messageTemplate} started", args);
return op;
}
}
(Based on the code from your PR.)
Then in your app:
using var op = OperationEx.Begin("Adding {Count} successive integers", count));
// .. as before
Upvotes: 2