Kentico CMS 7.0 Developer's Guide

Adding custom counters

Adding custom counters

Previous topic Next topic Mail us feedback on this topic!  

Adding custom counters

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Besides the default performance counters listed in the Performance counters overview topic, it is also possible to implement custom performance counters to monitor other values according to your specific needs.

 

In the following example, you will learn how to implement a custom performance counter to monitor the number of accesses to the Home.aspx page of any website running in the system. The counter will be available in both the General and the Sites counter categories, enabling you to monitor access to the Home.aspx page of each individual website or globally for all websites in the Kentico CMS instance.

 

1. Create a new XML file where the custom counter's definition will be located. Give it the name e.g. MyCounters.xpc and add it to ~\App_Data\CMSModules\HealthMonitoring (or any other folder under ~\App_Data\CMSModules\). Its content should be as follows:

 

<?xml version="1.0" encoding="utf-8"?>

<Counters>

 <Counter Key="requestshomepage" Name="Home.aspx page requests" Description="The number of Home.aspx page requests." Type="NumberOfItems32" Enabled="True" OnlyGlobal="False" />

</Counters>

 

2. We need to create a new module class in App_Code (or Old_App_Code if you installed the project as a web application), e.g. ~\App_Code\Custom\CustomCounterModule.cs, which will extend the CMSModuleLoader partial class. The code extract below shows what the class should look like for the purpose of this example.

 

At first, a private attribute mTotalHomePageRequests is added to the class, which will hold the value logged in the counter. Its value is made accessible by the TotalHomePageRequests public property. The code within the Init() method, which is executed on each application start, ensures registration of the method in the OnLogCustomCounter event. When the event occurs, the HealthMonitoringLogHelper_OnLogCustomCounter method is executed.

 

using System;

 

using CMS.GlobalHelper;

using CMS.SettingsProvider;

using CMS.SiteProvider;

 

[CustomCounterModuleLoader]

public partial class CMSModuleLoader

{

    /// <summary>

    /// Counter of total Home.aspx page requests.

    /// </summary>

    private static CMSPerformanceCounter mTotalHomePageRequests = null;

 

    /// <summary>

    /// Counter of total home page requests.

    /// </summary>

    public static CMSPerformanceCounter TotalHomePageRequests

    {

        get

        {

            if (mTotalHomePageRequests == null)

            {

                mTotalHomePageRequests = new CMSPerformanceCounter();

            }

 

            return mTotalHomePageRequests;

        }

    }

 

 

    /// <summary>

    /// Module registration

    /// </summary>

    private class CustomCounterModuleLoaderAttribute : CMSLoaderAttribute

    {

        /// <summary>

        /// Initializes the module

        /// </summary>

        public override void Init()

        {

            CMS.CMSHelper.HealthMonitoringLogHelper.OnLogCustomCounter += new CMS.CMSHelper.HealthMonitoringLogHelper.LogCustomCounterHandler(HealthMonitoringLogHelper_OnLogCustomCounter);

        }

 

        /// <summary>

        /// Handles custom event of health monitoring.

        /// </summary>

        /// <param name="counter">Counter</param>

        /// <returns>CMS performance counter</returns>

        private static CMSPerformanceCounter HealthMonitoringLogHelper_OnLogCustomCounter(Counter counter)

        {

            if (counter.Key.ToLower() == "requestshomepage")

            {

                return TotalHomePageRequests;

            }

 

            return null;

        }

 

    }

}

 

3. Add the OnLoad method below to ~\CMSPages\PortalTemplate.aspx.cs. The code ensures that the counter value is incremented each time a page with the /home node alias path is accessed.

 

    protected override void OnLoad(EventArgs e)

    {

        // Increment Home.aspx page requests counter

        if (CMS.CMSHelper.CMSContext.CurrentPageInfo.NodeAliasPath.Equals("/home", StringComparison.InvariantCultureIgnoreCase))

        {

            CMSModuleLoader.TotalHomePageRequests.Increment(CMS.CMSHelper.CMSContext.CurrentSiteName);

        }

       

        base.OnLoad(e);

    }

 

4. At this point, the process is different depending on if the Windows service was running at the time when you performed step 1 of this example.

 

4.a If it was running, the counters should be already registered in both categories as the Windows service performs automatic reload of counters when an .xpc file in the folder structure under ~\AppData\CMSModules\ is created, edited or deleted.

 

4.b If the Windows service was not running when you performed step 1, you will need to register the counters manually by executing the Windows service with respective parameters, as explained in the Registering performance counters topic. To do it, open Windows command line, navigate to the Bin folder inside the Kentico CMS installation folder (typically C:\Program Files\Kentico CMS\<version>\Bin) and execute the HealthMonitoringService.exe file with the following parameters:

 

HealthMonitoringService.exe /webpath="<disk path to web project root>" /createcounters

 

This action will reload all counters in both categories, including the newly added one.

 

 

 

Custom counters and Health Monitoring Windows service

 

Values of all custom counters can be logged only by the application itself, i.e. it is not possible for values of custom counters to be logged by the Windows service. The only purpose of using the service in this step is to register the added counter, as described in the Registering performance counters topic.

 

 

5. Verify that you have Health monitoring settings adjusted correctly in Site Manager -> Settings -> System -> Health monitoring, launch Performance monitor and open the dialog for adding monitored counters (as explained in Monitoring using Performance monitor). You should see the new counter present in both counter categories of the current Kentico CMS instance. Add the counter using the Add >> button, once from the General category and once for each website (if you have more than in your Kentico CMS instance) from the Sites category. Then click OK.

 

devguide_clip0214

 

6. Before trying out the functionality, go to Site Manager -> Administration -> System and restart the application using the Restart application button. Now try accessing the Home.aspx page multiple times and see how the value gets incremented after each access.

 

devguide_clip0217