Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
My team introduces lots of time measurements for various code sections that might take longer than expected in a real-life flow. These rely on initializing a StopWatch, getting the elapsed time an...
#1: Initial revision
Time measurement in an ASP.NET Core application
My team introduces lots of time measurements for various code sections that might take longer than expected in a real-life flow. These rely on initializing a `StopWatch`, getting the elapsed time and sometimes ensuring that measurement is done even if an exception is raised. The following tries to make this as simple as possible. ## The factory ```c# /// <summary> /// allows to measure time in a disposable scope /// </summary> public interface IStopwatchHelperFactory { IStopwatchHelper Create(); IStopwatchHelper Create(Action<TimeSpan>? measurementAction); } public class StopwatchHelperFactory : IStopwatchHelperFactory { public IStopwatchHelper Create() { return new StopwatchHelper(); } /// <summary> /// creates a new instance of <see cref="IStopwatchHelper"/> and specifies what happens when the measurement scope is disposed /// </summary> /// <param name="measurementAction"></param> /// <returns></returns> public IStopwatchHelper Create(Action<TimeSpan>? measurementAction) { return new StopwatchHelper(measurementAction); } } ``` ## The measurement helper ```c# public interface IStopwatchHelper : IDisposable { void SetMeasurementAction(Action<TimeSpan>? value); } public sealed class StopwatchHelper : IStopwatchHelper { private readonly Stopwatch _stopwatch; private Action<TimeSpan>? _measurementAction; public StopwatchHelper() { _stopwatch = Stopwatch.StartNew(); } public StopwatchHelper(Action<TimeSpan>? measurementAction) : this() { _measurementAction = measurementAction; } public void SetMeasurementAction(Action<TimeSpan>? value) { _measurementAction = value; } public void Dispose() { _measurementAction?.Invoke(Elapsed); } public TimeSpan Elapsed => _stopwatch.Elapsed; } ``` ## Setup ```c# services.AddSingleton<IStopwatchHelperFactory, StopwatchHelperFactory>(); ``` ## Usage example The following shows how to measure time spent in a function from one point until the function returns (or exception is thrown + return). ```c# // this is constructor injected private readonly IStopwatchHelperFactory _stopwatchHelperFactory; public async Task DoStuff() { // init code // start measuring time using var sw = _stopwatchHelperFactory.Create(elapsed => _specificLoggingData.Foo.Duration = (int)elapsed.TotalMilliseconds); // other code might come here } ``` ## The review I am interested in potential issues with this solution, and possible edge cases I am missing. Coding comments, style, and bad practices are also welcomed.