stopwatch-manager
1.0.6
See the version list below for details.
dotnet add package stopwatch-manager --version 1.0.6
NuGet\Install-Package stopwatch-manager -Version 1.0.6
<PackageReference Include="stopwatch-manager" Version="1.0.6" />
paket add stopwatch-manager --version 1.0.6
#r "nuget: stopwatch-manager, 1.0.6"
// Install stopwatch-manager as a Cake Addin #addin nuget:?package=stopwatch-manager&version=1.0.6 // Install stopwatch-manager as a Cake Tool #tool nuget:?package=stopwatch-manager&version=1.0.6
Stopwatch Manager for .NET
Have you ever had to record timings for a multi-threaded application and realized you needed to record the length of so many events that you actually need a collection of stopwatches instead of a single stopwatch? Yeah, me too.
That's why I developed the Stopwatch Manager for .NET. The package supports .NET 6 & 7, and it creates a concurrent dictionary of stopwatches, each with its own unique key for lookup.
Simply start a new stopwatch with a unique key name you haven't previously used, and a stopwatch gets added to the collection and is started. Stop, reset, or remove any stopwatch using the same key you used to start it.
The stopwatch manager supports Microsoft.Extensions.Logging and Serilog loggers and, by default, logs when a stopwatch is started or stopped. Elapsed time is logged when the watch is stopped, by default.
There are also options to suppress logging, as stopping a stopwatch also returns the elapsed time via an out TimeSpan parameter.
This may not be a library you'll ship to production, but it will help you get to production faster by allowing complex time recordings for operations you perform in your source code.
You can get a copy of this NuGet from the following location: https://www.nuget.org/packages/stopwatch-manager/
Installation
Simply add the NuGet package to an existing .NET 6 or 7 project:
dotnet add package stopwatch-manager
or manually add the XML yourself to your .csproj file:
<PackageReference Include="stopwatch-manager" Version="<current_version>" />
The releases on this GitHub repo mirror the versions published on nuget.org, so feel free to use the current release version from the repo when adding xml manually to your project definition.
Usage
To use the stopwatch manager, create a logger instance, create a StopwatchManager instance, and start a stopwatch:
using var loggerFactory =
LoggerFactory.Create(builder =>
builder.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "HH:mm:ss";
}));
var logger = loggerFactory.CreateLogger<ClassName>();
var stopwatchManager = new StopwatchManager(logger);
bool started = stopwatchManager.TryStart(out var eventKey)
Note a unique key composed of <ClassName>_<LineNumber>
will be generated by the call
to TryStart() and will be returned as an out parameter. Use this value to reference the stopwatch
in subsequent calls.
You may also choose not to provide a logger, preventing Stopwatch Manager from logging at all. This is a good option if you plan to log results yourself:
var stopwatchManager = new StopwatchManager();
bool started = stopwatchManager.TryStart(out var eventKey)
Stopwatch manager works with any Microsoft.Extensions.Logging or Serilog logger instance and logs when a stopwatch is started or stopped. When stopped, the log includes the elapsed milliseconds of the stopwatch at the time it was stopped.
Use the generated event key you received from your TryStart() call to stop the stopwatch.
bool stopped = stopwatchManager.TryStop(eventKey);
You may also send in your own unique event key instead of allowing one to be generated for you. Stopwatch manager can also list all of the stopwatches currently in the collection, and you can reset or remove stopwatches that are already in the collection.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Logging (>= 6.0.0)
- Serilog (>= 2.12.0)
-
net7.0
- Microsoft.Extensions.Logging (>= 7.0.0)
- Serilog (>= 2.12.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- 1.0.0
Initial release
- 1.0.1
Updates to NuGet XML documentation
- 1.0.2
Automation of NuGet release notes
- 1.0.3
Added Restart(), LogStopwatchList(), and GetStopwatchKeys()
- 1.0.4
Added TryStart() that generates a stopwatch key using caller identity information
Updated inaccurate XML documentation
- 1.0.5
Augmented readme.md
Added constructor that lacks a logger parameter
- 1.0.6
Added TryStartNoLog() that generates a stopwatch key using caller identity information