microsoft/healthcare-shared-components

Public

mirrored from https://github.com/microsoft/healthcare-shared-componentsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
main

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Microsoft.Health.Core/ActionTimer.cs

41lines · modecode

1// -------------------------------------------------------------------------------------------------
2// Copyright (c) Microsoft Corporation. All rights reserved.
3// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4// -------------------------------------------------------------------------------------------------
5
6using System;
7using System.Diagnostics;
8using EnsureThat;
9using Microsoft.Extensions.Logging;
10
11namespace Microsoft.Health.Core;
12
13public sealed class ActionTimer : IDisposable
14{
15 private readonly ILogger _logger;
16 private readonly string _componentName;
17 private readonly Stopwatch _stopwatch;
18 private readonly IDisposable _scope;
19
20 public ActionTimer(ILogger logger, string componentName)
21 {
22 EnsureArg.IsNotNull(logger, nameof(logger));
23 EnsureArg.IsNotNull(componentName, nameof(componentName));
24
25 _logger = logger;
26 _componentName = componentName;
27 _stopwatch = new Stopwatch();
28
29 _scope = _logger.BeginScope($"Beginning execution of {componentName}");
30 _stopwatch.Start();
31 }
32
33 public TimeSpan ElapsedTime => _stopwatch.Elapsed;
34
35 public void Dispose()
36 {
37 _stopwatch.Stop();
38 _logger.LogInformation("{Component} executed in {Duration}.", _componentName, _stopwatch.Elapsed);
39 _scope.Dispose();
40 }
41}
42