Wednesday, March 14, 2012

Selectively writing performance log


public class PerfMetricsLoggingFlag
{
#region Data
  // PerformanceMetrics is an enum with Off, Always, Sampled members
  public PerformanceMetrics PerfMetricsLogging { get; set; }
  private Int32 LastTickCount = Environment.TickCount;
  // turn on performance logging every 5 minutes for sampled logging
  private Int32 LoggingIntervalInMilliSeconds = 5 * 60 * 1000;        
#endregion

public PerfMetricsLoggingFlag(PerformanceMetrics perfMetricsLogging)
{
  this.PerfMetricsLogging = perfMetricsLogging;
}

#region public API
public bool DoWrite()
{
 if (PerfMetricsLogging == PerformanceMetrics.Off)
 {
 return false;
 }
 else if (PerfMetricsLogging == PerformanceMetrics.Sampled)
 {
 int elapsedTicks = Math.Abs(this.LastTickCount - Environment.TickCount);
 this.LastTickCount = Environment.TickCount;
 if (elapsedTicks < LoggingIntervalInMilliSeconds)
 return false;
 }
 return true;

}
#endregion
}