Click here to Skip to main content
15,890,527 members
Articles / Programming Languages / C#
Article

A heartbeat and monitoring class library

Rate me:
Please Sign up or sign in to vote.
1.55/5 (6 votes)
11 Aug 2008CPOL1 min read 43.5K   765   15   6
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)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
GeneralNice concept, but no details Pin
David B. Taylor11-Aug-08 21:29
David B. Taylor11-Aug-08 21:29 
GeneralRe: Nice concept, but no details Pin
Duncan Edwards Jones11-Aug-08 22:56
professionalDuncan Edwards Jones11-Aug-08 22:56 
GeneralRe: Nice concept, but no details Pin
Scott Bruno12-Aug-08 3:25
Scott Bruno12-Aug-08 3:25 
GeneralRe: Nice concept, but no details Pin
catsclaw25-Sep-08 11:29
catsclaw25-Sep-08 11:29 
GeneralRe: Nice concept, but no details Pin
Duncan Edwards Jones25-Sep-08 23:07
professionalDuncan Edwards Jones25-Sep-08 23:07 
GeneralRe: Nice concept, but no details [modified] Pin
taher bayat28-Jul-11 9:44
taher bayat28-Jul-11 9:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.