4 Commits

Author SHA1 Message Date
Josh Creek 4b8acdf4d9 feat(*): Update DisposableStopWatch to work without a logger 2021-12-14 22:52:16 +00:00
Josh Creek d8ab65334a fix(*): Add missing argument 2021-12-14 18:54:03 +00:00
Josh Creek a44420e81d feat(*): Update readme with disposable stopwatch extensions 2021-12-14 18:49:30 +00:00
Josh Creek 3940aaf966 feat(*): Add DisposableStopWatchExtension class 2021-12-14 18:49:03 +00:00
3 changed files with 116 additions and 3 deletions
@@ -19,11 +19,12 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryType>git</RepositoryType>
<PackageTags>creek</PackageTags>
<Version>1.3.0</Version>
<AssemblyVersion>1.3.0.0</AssemblyVersion>
<Version>1.4.3</Version>
<AssemblyVersion>1.4.0.3</AssemblyVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -0,0 +1,79 @@
using System;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
namespace Creek.HelpfulExtensions
{
public class DisposableStopWatch : IDisposable
{
private readonly ILogger _logger;
private Stopwatch _stopWatch;
private string _message;
private bool _useConsole;
private bool _useConsoleOnly;
public DisposableStopWatch(ILogger logger, string message = null, bool useConsole = false)
{
_logger = logger;
_useConsoleOnly = false;
_stopWatch = new Stopwatch();
_message = message ?? string.Empty;
_useConsole = useConsole;
string startMessage = $"Start: {message}";
if (_useConsole)
{
_logger.LogInformation(startMessage);
Console.WriteLine(startMessage);
}
else
{
_logger.LogInformation(startMessage);
}
_stopWatch.Start();
}
public DisposableStopWatch(string message = null)
{
_useConsoleOnly = true;
_stopWatch = new Stopwatch();
_message = message ?? string.Empty;
string startMessage = $"Start: {message}";
Console.WriteLine(startMessage);
_stopWatch.Start();
}
public void Dispose()
{
_stopWatch.Stop();
string endMessage = $"Complete:{_message}: Elapsed: {_stopWatch.Elapsed}";
if (_useConsoleOnly)
{
Console.WriteLine(endMessage);
}
else if (_useConsole)
{
_logger.LogInformation(endMessage);
Console.WriteLine(endMessage);
}
else
{
_logger.LogInformation(endMessage);
}
}
}
public static class DisposableStopWatchExtension
{
public static DisposableStopWatch DisposableStopWatch(this ILogger logger, string message = null, bool useConsole = false) => new DisposableStopWatch(logger, message, useConsole);
public static DisposableStopWatch DisposableStopWatch(this string message) => new DisposableStopWatch(message);
}
}
+34 -1
View File
@@ -133,4 +133,37 @@ catch (Exception ex)
List<string> allInnerExceptionMessagesAndStackTraces = ex.AllInnerExceptionMessagesAndStackTraces();
...
}
```
```
## Logging Extensions
### DisposableStopWatchExtension
This extends `ILogger` with a timer that can wrap code with a using statement. It was heavily inspired by James Curran's [blog post](https://honestillusion.com/2021/12/14/Simple-timings.html). With this extension you can quickly, easily and cleanly stick `StopWatch` objects around code that you wish to time.
```csharp
using (_logger.DisposableStopWatch())
{
...
}
```
It takes in additional parameters for a message and whether or not to also write to a Console for quick and dirty iterating.
```csharp
using (_logger.DisposableStopWatch("A message", true))
{
...
}
```
You can also make use of it without a logger for very barebones console applications.
```csharp
using ("A message".DisposableStopWatch())
{
...
}
```
You get a `Start:` log, with the message appended if you've passed it, and once your code within the using block has run you get `Complete:` with the messaged appended if you've passed it, then on the same line `Elapsed:` and the time elapsed in HH:MM:SS:MS..... format.