Created
November 27, 2013 07:05
-
-
Save mikeminutillo/7671745 to your computer and use it in GitHub Desktop.
Regions to assist with debugging applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MemoryProfileRegion : IDisposable | |
{ | |
private static readonly ILog Log = LogManager.GetLogger("Profiling"); | |
private readonly string _name; | |
private readonly long _initialMemory; | |
private MemoryProfileRegion(string name) | |
{ | |
_name = name; | |
if(!Log.IsDebugEnabled) return; | |
Thread.MemoryBarrier(); | |
_initialMemory = System.GC.GetTotalMemory(true); | |
} | |
public void Dispose() | |
{ | |
if(!Log.IsDebugEnabled) return; | |
Thread.MemoryBarrier(); | |
var finalMemory = System.GC.GetTotalMemory(true); | |
var consumed = finalMemory - _initialMemory; | |
Log.DebugFormat("{0} consumed {1:###,###,###,##0} bytes of memory", _name, consumed); | |
} | |
public static MemoryProfileRegion Named(string name) | |
{ | |
return new MemoryProfileRegion(name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProfileRegion : IDisposable | |
{ | |
private static readonly ILog Log = LogManager.GetLogger("Profiling"); | |
private readonly string _name; | |
private readonly Stopwatch _stopwatch = new Stopwatch(); | |
private ProfileRegion(string name) | |
{ | |
_name = name; | |
_stopwatch.Start(); | |
Log.DebugFormat("Entered {0}", _name); | |
} | |
public void Dispose() | |
{ | |
_stopwatch.Stop(); | |
Log.DebugFormat("Exited {0}. Took {1} ms", _name, _stopwatch.ElapsedMilliseconds); | |
} | |
public static ProfileRegion Named(string name) | |
{ | |
return new ProfileRegion(name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment