mirror of
https://github.com/jcreek/Creek.HelpfulExtensions.git
synced 2026-07-13 03:03:45 +00:00
feat(*): Add DisposableStopWatchExtension class
This commit is contained in:
@@ -19,11 +19,12 @@
|
|||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<PackageTags>creek</PackageTags>
|
<PackageTags>creek</PackageTags>
|
||||||
<Version>1.3.0</Version>
|
<Version>1.4.0</Version>
|
||||||
<AssemblyVersion>1.3.0.0</AssemblyVersion>
|
<AssemblyVersion>1.4.0.0</AssemblyVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Creek.HelpfulExtensions
|
||||||
|
{
|
||||||
|
public class DisposableStopWatch : IDisposable
|
||||||
|
{
|
||||||
|
private ILogger _logger;
|
||||||
|
private Stopwatch _stopWatch;
|
||||||
|
private string _message;
|
||||||
|
private bool _useConsole;
|
||||||
|
|
||||||
|
public DisposableStopWatch(ILogger logger, string message = null, bool useConsole = false)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_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 void Dispose()
|
||||||
|
{
|
||||||
|
_stopWatch.Stop();
|
||||||
|
|
||||||
|
string endMessage = $"Complete:{_message}: Elapsed: {_stopWatch.Elapsed}";
|
||||||
|
|
||||||
|
if (_useConsole)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(endMessage);
|
||||||
|
Console.WriteLine(endMessage);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogInformation(endMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DisposableStopWatchExtension
|
||||||
|
{
|
||||||
|
public static DisposableStopWatch DisposableStopWatch(this ILogger logger, string msg = null) => new DisposableStopWatch(logger, msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user