BeyondTech.Extensions.Logging.Timing 1.0.0

.NET 6.0 .NET Standard 2.0
dotnet add package BeyondTech.Extensions.Logging.Timing --version 1.0.0
NuGet\Install-Package BeyondTech.Extensions.Logging.Timing -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BeyondTech.Extensions.Logging.Timing" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BeyondTech.Extensions.Logging.Timing --version 1.0.0
#r "nuget: BeyondTech.Extensions.Logging.Timing, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install BeyondTech.Extensions.Logging.Timing as a Cake Addin
#addin nuget:?package=BeyondTech.Extensions.Logging.Timing&version=1.0.0

// Install BeyondTech.Extensions.Logging.Timing as a Cake Tool
#tool nuget:?package=BeyondTech.Extensions.Logging.Timing&version=1.0.0

Timing Extensions

This library is a direct port of @nblumhardt 's Serilog Timing for usage directly with Microsoft's ILogger. This library provides almost all the same benefits of the Serilog Timing, but expands the support for any implementation of ILogger.

Installation

dotnet add package BeyondTech.Extensions.Logging.Timing

Getting started

First, an ILogger must be available. Whether through a LoggerFactory, dependency injection, or registration of a logging provider. The extension methods are available in the BeyondTech.Extensions.Logging.Timing namespace.

using BeyondTech.Extensions.Logging.Timing;
using (logger.TimeOperation("Processing data for action: {ActionId}", action.Id))
{
    // Operation to time here
}

At the completion of the using block, a message will be written to the log like:

info: Processing data for action: 87654 completed in 109.4 ms

The operation description passed to TimeOperation() is a message template; the event written to the log extends it with " {Outcome} in {Elapsed} ms".

  • All messages logged have an Elapsed property in milliseconds
  • Outcome will always be "completed" when the TimeOperation() method is used

Operations that can either succeed or fail, or that produce a result, can be created with BeginOperation():

using (var op = logger.BeginOperation("Processing data for action: {ActionId}", action.Id))
{
	// Operation to time here

	op.Complete();
}

Using op.Complete() will produce the same kind of result as in the first example:

info: Processing data for action: 4506 completed in 22.1 ms

Additional methods on Operation allow more detailed results to be captured:

op.Complete("{AffectedRows} rows affected", rowCount);

This will create a logging scope around the outcome message with the log scope of "2 rows affected". For this to work effectively, be sure to enable logging scopes on your ILogger configuration.

If the operation is not completed by calling Complete(), it is assumed to have failed and a warning-level event will be written to the log instead:

warn: Processing data for action: 87654 abandoned in 109.4 ms

In this case the Outcome property will be "abandoned".

To suppress this message, for example when an operation turns out to be inapplicable, use op.Cancel(). Once Cancel() has been called, no event will be written by the operation on either completion or abandonment.

Leveling

Timings are most useful in production, so timing events are recorded at the Information level and higher, which should generally be collected all the time.

If you truly need Verbose- or Debug-level timings, you can trigger them with the OperationAt() extension method on ILogger:

using (logger.OperationAt(LogEventLevel.Debug).TimeOperation("Preparing zip archive"))
{
    // ...

When a level is specified, both completion and abandonment events will use it. To configure a different abandonment level, pass the second optional parameter to the OperationAt() method.

Caveats

One important usage note: because the event is not written until the completion of the using block (or call to Complete()), arguments to BeginOperation() or TimeOperation() are not captured until then; don't pass parameters to these methods that mutate during the operation.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 161 11/2/2022
1.0.0-pre.1 90 5/24/2022

Initial release