سؤال

We are in need to add performance monitoring to our application. For the prototype I've created a sample project, which I'm trying to get into work.

I'm trying to use policyInjection for the performance counters, so we'll be able to turn on and off the performance monitoring in the production environment.

So far I can see the actual category in perfmon, but I cannot see any instances (see image), even I'm pretty sure the application is running and instance exists, as you can see in attached sources.

No instance in perfmon

I've tried a lot of things, also googled around, but didn't find any usable solution or clue what to look for.

Application is created as consoleApplication

You can also download a zipped project for VS here: http://dl.dropbox.com/u/19457132/stackOverflow/Mpd.Instrumentation.PerformanceCounter.zip

Here are my sources.

Program.cs

using System;
using System.Collections;
using System.Configuration.Install;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Installers;

namespace Mpd.Instrumentation.PerformanceCounter
{
    class Program
    {
        static void Main(string[] args)
        {
           //RemoveCounters();   
           InstallCounters();

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

            for (int i = 0; i < 100000000; i++)
            {
                myCounter.SampleMethod(i);
            }

            Console.ReadLine();
        }

        public static void InstallCounters()
        {
            PerformanceCountersInstaller installer = new PerformanceCountersInstaller(new SystemConfigurationSource());
            IDictionary state = new 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();
        }

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

MyCounters.cs

using System;
using System.Threading;

namespace Mpd.Instrumentation.PerformanceCounter
{
    public class MyCounters : IPerformanceCounter
    {
        public void SampleMethod(int i)
        {
            Console.WriteLine(i);
            Thread.Sleep(50);
        }
    }
}

IPerformanceCounter.cs

using System;

namespace Mpd.Instrumentation.PerformanceCounter
{
    public class IPerformanceCounter : MarshalByRefObject
    {

    }
}

And finally app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="policyInjection" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.PolicyInjectionSettings, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
    <section name="instrumentationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration.InstrumentationConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
  </configSections>
  <policyInjection>
    <policies>
      <add name="SampleCountersPolicy">
        <matchingRules>
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MethodSignatureMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            match="SampleMethod" ignoreCase="true" name="Method Signature Matching Rule" />
        </matchingRules>
        <handlers>
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.PerformanceCounterCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            categoryName=".aaaTest" instanceName="Default" useTotalCounter="true"
            incrementNumberOfCalls="true" incrementCallsPerSecond="true"
            incrementAverageCallDuration="true" incrementTotalExceptions="true"
            incrementExceptionsPerSecond="true" order="1" name="Performance Counter Call Handler" />
        </handlers>
      </add>
    </policies>
  </policyInjection>
  <instrumentationConfiguration performanceCountersEnabled="true"
    applicationInstanceName="Default" />
</configuration>
هل كانت مفيدة؟

المحلول

Since the SampleMethod accepts a parameter you need to add that to the matching rules configuration:

    <matchingRules>
      <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MethodSignatureMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        match="SampleMethod" ignoreCase="true" name="Method Signature Matching Rule">
        <parameters>
          <parameter name="i" typeName="System.Int32" />
        </parameters>
      </add>
    </matchingRules>

Without the parameter the matching rule does not match so the call handler is not invoked. If you modify the configuration you should see the instances in perfmon.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top