Is using dispose pattern to log execution time of part of a code a clean code approach?

In our project we have a requirement of tracking execution time of some parts of code. In order to keep it DRY, we have implemented dispose pattern on "PerformanceTimer" class which logs the time on dispose. Code below.

public class PerformanceTimer : IDisposable
{
    private readonly Stopwatch _stopwatch;
    private readonly string _name;

    public PerformanceTimer(string name)
    {
        _stopwatch = Stopwatch.StartNew();
        _name = name;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposing)
            return;
        _stopwatch.Stop();        
        Logger.Information($"Execution time for {_name}: {_stopwatch.ElapsedMilliseconds} ms");
    }
}

Usage:

using (new PerformanceTimer("foo"))
{
    //code block
}

Now the issue is when the traffic is higher to the API, "Dispose" method is showing as hot spot in trace.

Is this a clean approach? How could we improve the performance?

Thanks in advance.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/anjoiype
πŸ“…︎ Jan 24 2021
🚨︎ report
The Dispose Pattern in C# by Example vkontech.com/the-dispose-…
πŸ‘︎ 12
πŸ’¬︎
πŸ‘€︎ u/Vasilkosturski
πŸ“…︎ Jan 09 2021
🚨︎ report
The Dispose Pattern in C# by Example vkontech.com/the-dispose-…
πŸ‘︎ 22
πŸ’¬︎
πŸ‘€︎ u/Vasilkosturski
πŸ“…︎ Jan 30 2021
🚨︎ report
Why does Microsoft recommend such an over-complicated dispose pattern for all cases?

Everyone knows that IDisposable is used for, and I've always used the simpler pattern of just implementing that one Dispose() method. However later versions of Visual Studio have started to recommend (via warnings, no less) and provide scaffolding for a much more involved implementation of that pattern that requires a Dispose(bool disposing) in addition to the Dispose().

Reading the documentation and the comments in the scaffolding, it makes sense when I would need to implement this more robust pattern: when I have unmannaged resources that absolutely must be freed at disposal or, failing that, at garbage collection.

Now, I've been working with .NET for 10 years now and I can count the number of times I've written a class that was responsible for managing a native resource on two hands and maybe a foot. Now, obviously, I've used files, streams, and various synchronization primitives, but always indirectly though framework or library classes who themselves are responsible for freeing those resources (when I call their Dispose methods from within my simple Dispose method).

But Visual Studio insists that I implement the full-blown dispose pattern every single time I implement IDisposable. My question is: why? If I'm always using wrappers around resources and I just want to call their Dispose methods, it's the responsibility of those wrappers holding the unmanaged resources to do the actual cleanup. Why do I need to make my class overly complicated and ugly just to make VS stop warning me?

Edit: un-escaped all the backticks.

πŸ‘︎ 46
πŸ’¬︎
πŸ“…︎ May 30 2020
🚨︎ report
An example-driven overview of the Dispose Pattern vkontech.com/the-dispose-…
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/Vasilkosturski
πŸ“…︎ Apr 10 2020
🚨︎ report
An example-driven overview of the Dispose Pattern vkontech.com/the-dispose-…
πŸ‘︎ 9
πŸ’¬︎
πŸ‘€︎ u/Vasilkosturski
πŸ“…︎ Apr 04 2020
🚨︎ report
Interested in what is a Dispose pattern and fReachable queue, how finalization works in .NET? Come and read: [.NET Internals 07] Unmanaged resources: finalization, fReachable queue and dispose pattern dsibinski.pl/2018/09/net-…
πŸ‘︎ 30
πŸ’¬︎
πŸ‘€︎ u/dsibinski
πŸ“…︎ Sep 05 2018
🚨︎ report
Eric Bischoff said "My bodily functions have more credibility than Dave Meltzer", to which Dave respond with "Those who have facts as their disposal, use them. Those who don't, name call. I think we see a pattern." twitter.com/davemeltzerWO…
πŸ‘︎ 593
πŸ’¬︎
πŸ‘€︎ u/SuplexCity-Mayor
πŸ“…︎ Oct 25 2021
🚨︎ report
Found in my community group; there seems to be a strong pattern of waste disposal workers being amazingly sweet.
πŸ‘︎ 34
πŸ’¬︎
πŸ‘€︎ u/KrazyKhajiitLady
πŸ“…︎ Jan 02 2022
🚨︎ report
A new study finds that the pattern of heat coming from volcanoes on Io's surface disposes of the generally-accepted model of internal heating. The heat pouring out of Io's hundreds of erupting volcanoes indicates a complex, multi-layer source.
πŸ‘︎ 17
πŸ’¬︎
πŸ‘€︎ u/peterabbit456
πŸ“…︎ Jun 11 2012
🚨︎ report
By generating a mosaic pattern on the leaves, the bean common mosaic virus stunts or kills plants. Rather than composting, afflicted plants should be removed and disposed from the garden. lifeandagri.com/planting-…
πŸ‘︎ 31
πŸ’¬︎
πŸ‘€︎ u/vidura_o7
πŸ“…︎ Dec 15 2021
🚨︎ report
A closer look at the Dispose pattern in the scope of the .NET framework haacked.com/archive/2005/…
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/redrobot5050
πŸ“…︎ Feb 05 2009
🚨︎ report
Is it a good idea to buy puts for Berger paints ? PE of 88 with rising wedge pattern in an unstable market.. Inflation, crude oil prices and the lack of disposable income among public are not favourable to paints in general.. I assume that demand for decorative paints will suffer in the short term.
πŸ‘︎ 7
πŸ’¬︎
πŸ‘€︎ u/Orthosurgeon1992
πŸ“…︎ Nov 26 2021
🚨︎ report
By generating a mosaic pattern on the leaves, the bean common mosaic virus stunts or kills plants. Rather than composting, afflicted plants should be removed and disposed from the garden. lifeandagri.com/planting-…
πŸ‘︎ 19
πŸ’¬︎
πŸ‘€︎ u/vidura_o7
πŸ“…︎ Dec 15 2021
🚨︎ report
By generating a mosaic pattern on the leaves, the bean common mosaic virus stunts or kills plants. Rather than composting, afflicted plants should be removed and disposed from the garden. lifeandagri.com/planting-…
πŸ‘︎ 23
πŸ’¬︎
πŸ‘€︎ u/vidura_o7
πŸ“…︎ Dec 15 2021
🚨︎ report
Meltzer: "Those who have facts at their disposal, use them. Those who don't, make them up and charge 11.99 a month for them in a poorly written newsletter. I think we see a pattern."

Fuck Vince

πŸ‘︎ 29
πŸ’¬︎
πŸ‘€︎ u/KilledByDeath
πŸ“…︎ Oct 25 2021
🚨︎ report
I was doing so well on the crazy lace icing pattern I was attempting on my bourbon pumpkin tart... Right up until my disposable piping bag ripped.
πŸ‘︎ 86
πŸ’¬︎
πŸ‘€︎ u/MichiganBelle_31
πŸ“…︎ Nov 26 2020
🚨︎ report
This water pattern in a mason jar when I turn the garbage disposal on and off. v.redd.it/fg4401y77nt41
πŸ‘︎ 2k
πŸ’¬︎
πŸ‘€︎ u/jmckny76
πŸ“…︎ Apr 18 2020
🚨︎ report
Crochet Swiffer mops. Wet refills add up with 2 dogs. No more! Made 2 for $3/ea and they work WAY better than the refills. reddit.com/gallery/rtcqmq
πŸ‘︎ 2k
πŸ’¬︎
πŸ‘€︎ u/Lumpriest
πŸ“…︎ Jan 01 2022
🚨︎ report

Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.