65.9K
CodeProject is changing. Read more.
Home

A heartbeat and monitoring class library

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.55/5 (6 votes)

Aug 11, 2008

CPOL

1 min read

viewsIcon

44222

downloadIcon

768

A C# library to add a heartbeat and one or more listeners to an application.

Introduction

An application heartbeat is a periodic event which is raised so long as an application is "alive", which can be monitored by one or more listeners which can then act on that heartbeat. This class library demonstrates one way of achieving this, and allows for listeners to be added via the application configuration. (This last is conceptually similar to the way Trace and Debug listeners work.)

Background

The main class is the static Heartbeat class that has a System.Threading.Timer that is initialised the first time the class is referred to, and which raises a "Beat" every second. This beat is then passed on to every listener class (which is a class inheriting from a base HeartbeatMonitorBase) to record or react to as they choose.

The heartbeat listeners collection is read from the application configuration file by the class HeartbeatCustomConfigSection which inherits from the .NET framework 2.0 class System.Configuration.ConfigurationSection, and each class listed in the <heartbeats> section is instantiated when read, by creating a System.Reflection.ConstructorInfo and executing it.

Using the code

At the start of your application, call the Monitoring.Heartbeat.ResetListensersFromConfig(); method to start the heartbeat and create the listeners per the application configuration file.

The application configuration for the heartbeat listeners should be something like:

<configSections>
  <section name ="heartbeatSettings" 
      type="Monitoring.HeartbeatCustomConfigSection, Monitoring, 
            Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/>
</configSections>

And, the individual listeners would be added thus:

<heartbeatSettings>
    <heartbeats>
      <clear />
      <add Name="HeartbeatTrace"  
            Type="Monitoring.TraceHeartbeatMonitor, Monitoring, 
                  Version=1.0.0.0,Culture=neutral,PublicKeyToken=null" />
      <add Name="HeartbeatTraceTwo" 
            Type="Monitoring.TraceHeartbeatMonitor, Monitoring, 
                  Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"  
            InitData="foo" />
    </heartbeats>
</heartbeatSettings>

History

  • 2008-08-11 - Created.