Unable to see performance counters in action

Topics: Enterprise Library Core, General discussion, Policy Injection Application Block
Aug 4, 2010 at 10:23 PM
Edited Aug 4, 2010 at 10:24 PM
Hi I'm using Enterprise Library 4.1 - October 2008, and trying to get a sample custom performance counters working. I'm running Windows 7 / VS2010 and .NET 3.5 When I run the application (as an administrator) I can see the performance counter category get created along with its counters in the Performance Monitor. (Add Counters section) However when I add them nothing shows me in the Performance Monitor Graph. Could you please tell me what I'm doing wrong here? Below is my code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation; using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers; using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.Installers; using System.Collections; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using System.Configuration.Install; namespace SamplePerformanceCounters { class Program { private static decimal balance = 0.00M; static void Main(string[] args) { InstallCounters(); for (int i = 0; i < 1000000; i++) { if (i % 2 == 0) { Thread.Sleep(100); } else { Thread.Sleep(500); } SearchCompanies(i); } Console.ReadLine(); } [PerformanceCounterCallHandler("Company Search Counters", "Default")] public static void SearchCompanies(int i) { Console.WriteLine(i); } public static void InstallCounters() { PerformanceCountersInstaller installer = new PerformanceCountersInstaller(new SystemConfigurationSource()); IDictionary state = new System.Collections.Hashtable(); installer.Context = new InstallContext(); installer.Install(state); installer.Commit(state); Console.WriteLine("Performance counters have been successfully installed."); Console.ReadLine(); } } } //App.config <configuration> <configSections> <section name="policyInjection" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.PolicyInjectionSettings, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> <section name="instrumentationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration.InstrumentationConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </configSections> <policyInjection> <policies> <add name="CompanySearch"> <matchingRules> <add match="SearchCompanies" ignoreCase="true" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MethodSignatureMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Method Signature Matching Rule"> <parameters> <parameter name="i" typeName="System.Int32" /> </parameters> </add> </matchingRules> <handlers> <add categoryName="Company Search Counters" instanceName="Default" useTotalCounter="true" incrementNumberOfCalls="true" incrementCallsPerSecond="true" incrementAverageCallDuration="true" incrementTotalExceptions="true" incrementExceptionsPerSecond="true" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.PerformanceCounterCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Performance Counters Handler" /> </handlers> </add> </policies> </policyInjection> <instrumentationConfiguration performanceCountersEnabled="true" eventLoggingEnabled="false" wmiEnabled="false" applicationInstanceName="" /> </configuration> Note that I get the same problem when I run the sample app Enterprise Library - Quick Start.
Aug 5, 2010 at 9:49 AM

I'm still in progress looking at your code though after trying the quick start I was able to add the counters in Performance Monitor and was able to display the graph/report for the associated counters.

Can you tell us how have you done this using the quick start on your end. Thanks.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Aug 6, 2010 at 2:15 AM

Based on the code you posted, the problem is that you don't have a policy-enabled instance of the class whose method you want to apply the performance counter handler.  A policy-enabled class is created using PolicyInjection.Create or PolicyInjection.Wrap method.  That class must either implement an interface or inherit from MarshalByRefObj.    In your case, create a class which either implement an interface or inherits from MarshalByRefObj and put the SearchCompanies method in that class. 

public class SomeService : MarshalByRefObj
{
         public SomeService()
         {
         } 

         public void  SearchCompanies(int i)
         {
                //implementation 
         }
}

Note that you don't have to decorate the method with the PerformanceCounterCallHandler attribute since you already defined a policy in your config which uses this call handler and specified a matching rule which matches the SearchCompanies method.

You would call this then by first creating a policy-enabled instance of the SomeService class:

SomeService service = PolicyInjection.Create<SomeService>();
service.SearchCompanies(valueOfI);

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Aug 10, 2010 at 11:29 AM

Hi Gino

Thanks for the reply. Please see below for my steps.

Running Windows 7 and logged in asan  administrator.

1. Opened the C:\EntLib41Src\Quick Starts\PolicyInjection\CS\PolicyInjectionQuickStart in VS2010 as an administrator

2. Build the solution -build succeeded.

3. Run the application

4. Click on Install Performance counters

5. I get "Performance Counters has been successfully installed"

6. Open the performance monitor.

7. Click Add Counters

8. Select "Enterprice Library Policy Injection Quick Start" and this select all the counters.

9. Clicl on Add>> button

10.Now Click OK. The dialogue box closes and returns to the main performance monitor window.

11.Now if you check the list of counters below (with column names- Show, Color, Scale, Counter...etc)

12. There is no Entlib Quick Start counters. I can only see one counter which %Processor time


Am I still missing something here?

 

Aug 10, 2010 at 12:29 PM

Hi

I followed Sarah's instructions and  I managed to get it working my Company Search sample app.   I can see Company Search performance counters in the perf. montor and displaying in the graph.

However I'm more interested in attribute based performance counters as described in http://msdn.microsoft.com/en-us/library/ff648494.aspx.

Ideally my requirement is to add the attribute to the CompanySearch method.  It should give me the same result as I got it working before. Can please put me in the right direction?

  

 

 private static decimal balance = 0.00M;
        static void Main(string[] args)
        {
            InstallCounters();
            //RemoveCounters();            
           
            for (int i = 0; i < 1000000; i++)
            {
                if (i % 2 == 0)
                {
                    Thread.Sleep(100);
                }
                else
                {
                    Thread.Sleep(500);
                }

                SearchCompanyMain(i);           
            }

            
            Console.ReadLine();
        }

        private static void RemoveCounters()
        {
            PerformanceCountersInstaller installer  = new PerformanceCountersInstaller(new SystemConfigurationSource());
            installer.Context = new InstallContext();
            installer.Uninstall(null);
            Console.WriteLine("Performance counters have been successfully installed. Press enter to continue.");
            Console.ReadLine();
        }


        public static void SearchCompanyMain(int i)
        {
            CompanyService service = PolicyInjection.Create();
            service.SearchCompanies(i);
        }


        public static void  InstallCounters()
        {
            PerformanceCountersInstaller installer = new PerformanceCountersInstaller(new SystemConfigurationSource());
            IDictionary state = new System.Collections.Hashtable();
            installer.Context = new InstallContext();
            installer.Install(state);
            installer.Commit(state);
            Console.WriteLine("Performance counters have been successfully installed. Press enter to continue");
            Console.ReadLine();
        }




[HasInstallableResources]
    [PerformanceCountersDefinition(PerformanceCategory, "SampleInstrumentationCounterHelp")]
    public class SimplestPerformanceClass
    {
        //performance counter category name.
        private const string PerformanceCategory = "Simplest Performance Category";

        //performance counter name.
        private const string PerformanceCounterName = "Operations Started/sec";

        //Used by in the installers to prepare a performance counters for installation.
        [PerformanceCounter(PerformanceCounterName, "OperationStartedHelpResource", PerformanceCounterType.RateOfCountsPerSecond32)]
        private EnterpriseLibraryPerformanceCounter _operationStarted = new EnterpriseLibraryPerformanceCounter(PerformanceCategory, PerformanceCounterName);

        /// 
        /// The only method to say when operation started increment the counters.
        /// 
        public void OperationStarted()
        {
            _operationStarted.Increment();
        } 
    }

 public class CompanyService : MarshalByRefObject
    {
        public CompanyService()
        {                
        }

        public void SearchCompanies(int i)
        {
            Console.WriteLine(i);
        }
    }

 

Thanks a lot,

Raj

 


 

Aug 11, 2010 at 2:54 AM
Edited Aug 11, 2010 at 3:30 AM

Attribute-based just simply means using attributes rather than doing it in the config.  As you already mentioned, you'll only need to apply the attribute in the SearchCompanies method.  The sample code you posted doesn't implement that. 

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Aug 11, 2010 at 12:40 PM

Fantastic thanks alot. It works now.

Oct 4, 2011 at 11:53 PM

Hi,

 

I am trying to do the same. Create the performance counters using the app.config and not use the performance counter call handler.But it doesn't work with out decorating the method with the performancecountercallhandler. When go to Windows Performance monitor, i see the counters but all the instances are disabled.. Any help is appreciated.

 
Oct 5, 2011 at 7:36 AM

Hi,

Your matching rule might not triggering your performance call handler. Can you check if that is the case?

 

Noel Angelo Bolasoc
Avanade Software and Cloud Services
Avanade, Inc.
Contact 

Oct 5, 2011 at 4:55 PM

Hi,

Thanks for the reply. Here is my config file

  <configuration>
       <configSections>
           <section name="policyInjection" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.PolicyInjectionSettings, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
           <section name="instrumentationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration.InstrumentationConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
       </configSections>
       <policyInjection>
          <policies>
               <add name="Sample">
                  <matchingRules>
                      <add match="SampleMethod" ignoreCase="true" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MethodSignatureMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Method Signature Matching Rule">
                           <parameters>
                              <parameter name="i" typeName="int"/>
                           </parameters>
                       </add>
                  </matchingRules>
                  <handlers>
                      <add categoryName="Sample Counter" instanceName="Test" useTotalCounter="true" incrementNumberOfCalls="true" incrementCallsPerSecond="true" incrementAverageCallDuration="true" incrementTotalExceptions="false" incrementExceptionsPerSecond="false" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.PerformanceCounterCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Performance Counters Handler"/>
                   </handlers>
               </add>
          </policies>
      </policyInjection>
      <instrumentationConfiguration performanceCountersEnabled="true" eventLoggingEnabled="false" wmiEnabled="false" applicationInstanceName=""/>
   <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

 

And my method is simple SampleMethod. Please let me know what i may be doing wrong.

Thanks

Padma

 

Oct 5, 2011 at 5:33 PM

Hi,

 

I thought i will include my complete sample application , please let me know what i am doing wrong. Any help is appreciated

Program.cs

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.Installers;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;

using System.Threading;

using System.Collections;

using System.Configuration.Install;

  namespace CounterApplication

{

   class Program

   {

       static void Main(string[] args)

       {

         InstallCounters();

          MyCounters myCounter = PolicyInjection.Create<MyCounters>();

            for (int i = 0; i < 100000000; i++)

          {

              Thread.Sleep(500);

               myCounter.SampleMethod(i);

           }

           Console.ReadLine();

       }

       public static void InstallCounters()

       {

           PerformanceCountersInstaller installer = new PerformanceCountersInstaller(new SystemConfigurationSource());

           IDictionary state = new System.Collections.Hashtable();

           installer.Context = new InstallContext();

           installer.Install(state);

           installer.Commit(state);

           Console.WriteLine("Performance counters have been successfully installed. Press enter to continue");

          Console.ReadLine();

       }

   }

}

 MyCounter.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Reflection;

 namespace CounterApplication

{

   public class MyCounters :MarshalByRefObject

   {

      public void SampleMethod(int i)

       {           Console.WriteLine(i);

       }

   }

}

app.config

  <configuration>
       <configSections>
           <section name="policyInjection" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.PolicyInjectionSettings, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
           <section name="instrumentationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration.InstrumentationConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
       </configSections>
       <policyInjection>
          <policies>
               <add name="Sample">
                  <matchingRules>
                      <add match="SampleMethod" ignoreCase="true" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MethodSignatureMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Method Signature Matching Rule">
                           <parameters>
                              <parameter name="i" typeName="int"/>
                           </parameters>
                       </add>
                  </matchingRules>
                  <handlers>
                      <add categoryName="Sample Counter" instanceName="Test" useTotalCounter="true" incrementNumberOfCalls="true" incrementCallsPerSecond="true" incrementAverageCallDuration="true" incrementTotalExceptions="false" incrementExceptionsPerSecond="false" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.PerformanceCounterCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Performance Counters Handler"/>
                   </handlers>
               </add>
          </policies>
      </policyInjection>
      <instrumentationConfiguration performanceCountersEnabled="true" eventLoggingEnabled="false" wmiEnabled="false" applicationInstanceName=""/>
</Configuration>

 Thanks

Padma

 

Oct 6, 2011 at 11:02 AM
Edited Oct 6, 2011 at 11:04 AM

Hi Padma,

It seems this is a bug with MethodSignatureMatchingRule when a parameter is involved. I tried it on my side and it works when I removed the parameter from the matching rule and from the target method. Anyway, possible workaround for this is to use the MemberNameMatchingRule. The only issue I can see with this approach is targeting a specific method on a method overloading scenario. Can you verify if my findings are correct?

Thanks, 

Noel Angelo Bolasoc
Avanade Software and Cloud Services
Avanade, Inc.
Contact 

Oct 6, 2011 at 5:20 PM

Hi Noel,

I tried using MemberNameMatchingRule and i still have the same issue. I am enclosing the app.config and program, please let me know what i am doing wrong. I tried the MethodSignatureMatchingRule  without parameter as well and it doesn't seem to work either.

Program.cs

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

using System.Collections;

using System.Diagnostics;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.Installers;

using Microsoft.Practices.Unity.InterceptionExtension;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;

using System.Threading;

using System.Configuration.Install;

 

namespace CounterUse

{

   class Program

   {

       static void Main(string[] args)

       {

           PerformanceCountersInstaller PolicyInstall = new PerformanceCountersInstaller(new SystemConfigurationSource());

             IDictionary state = new System.Collections.Hashtable();

           PolicyInstall.Context = new InstallContext();

            PolicyInstall.Install(state);

          PolicyInstall.Commit(state);

            Console.WriteLine("Performance counters have been successfully installed. Press enter to continue");

       Console.ReadLine();

                   PerformCounterBal myCounter = PolicyInjection.Create<PerformCounterBal>();

 

           // int i = 0;

           for (int i = 0; i < 1000000; i++)

           {

 

               Thread.Sleep(1000);

               myCounter.CaluclateBal();

               Console.WriteLine(i);

           }

 

 

           Console.ReadLine ();

 

 

 

       }

   }

}

 

App.config

 

<configuration>

<configSections>

   <section name="policyInjection" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.PolicyInjectionSettings, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

   <section name="instrumentationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration.InstrumentationConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

</configSections>

<policyInjection>

   <policies>

     <add name="Policy">

       <matchingRules>

         <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MemberNameMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

           name="Member Name Matching Rule">

           <matches>

             <add match="CaluclateBal" ignoreCase="false" />

          </matches>

         </add>

       </matchingRules>

       <handlers>

         <add categoryName="DemoCounters" instanceName="Default" useTotalCounter="true"

           incrementNumberOfCalls="true" incrementCallsPerSecond="true"

           incrementAverageCallDuration="true" incrementTotalExceptions="false"

           incrementExceptionsPerSecond="false" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.PerformanceCounterCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

           name="Performance Counters Handler" />

       </handlers>

     </add>

   </policies>

</policyInjection>

<instrumentationConfiguration performanceCountersEnabled="true"

   eventLoggingEnabled="false" wmiEnabled="false" applicationInstanceName="" />

</configuration>

 

 

PerformCounterBal.cs

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers;

using System.Diagnostics;

using System.Configuration;

using System.Reflection;

using Microsoft.Practices.Unity.InterceptionExtension;

 

 

namespace CounterUse

{

     public class PerformCounterBal:MarshalByRefObject

   {

    

    

       public void CaluclateBal()

       {

        

           // i = i + 1;

          

        

         }

   }

}

 

 

 

Oct 9, 2011 at 6:55 PM

Hi,

I'm not sure but I copied your code and everything works fine at my side. I suggest to try out the matching rule on other handlers (like logging handler or exception handling handler) and see if it works?

 

Noel Angelo Bolasoc
Avanade Software and Cloud Services
Avanade, Inc.
Contact 

Oct 10, 2011 at 9:55 PM

Hi,

Thanks for the reply. Is it because i am using Virtual Machine development on Windows 2003. Can that be an issue.

 

Thanks

Padma