/// <summary> /// Creates a new <see cref="TimedScope"/>, logs start info and starts the stopwatch. /// </summary> /// <param name="loggingScope">Logging scope to be used - the reported scope name comes form <see cref="LoggingScope.Module"/> property.</param> /// <param name="startWith">Initial result status - use <see cref="Status.Failure"/> when the action may throw.</param> /// <remarks>Use a <c>using</c> block when possible, otherwise you have to remember to call <see cref="Dispose"/>.</remarks> public TimedScope(LoggingScope loggingScope, Status startWith = Status.Success) { LoggingScope = loggingScope; Result = startWith; ScopeStack = ScopeStack.Push(this); LoggingScope.Info("TimedScope started."); Stopwatch = Stopwatch.StartNew(); }
private void Stop() { Stopwatch.Stop(); var message = $"TimedScope finished with {Result} in {GetElapsedTime()}."; switch (Result) { case Status.Failure: LoggingScope.Error(message); break; case Status.Success: LoggingScope.Info(message); break; case Status.Warning: LoggingScope.Warning(message); break; default: throw new NotSupportedException($"Status value '{Result}' is not supported."); } }