Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

A heartbeat and monitoring class library

1.55/5 (6 votes)
11 Aug 2008CPOL1 min read 1   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:

XML
<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:

XML
<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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)