首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用EntLib的perfmon - perfmon中没有实例

使用EntLib的perfmon - perfmon中没有实例
EN

Stack Overflow用户
提问于 2012-05-16 10:06:15
回答 1查看 522关注 0票数 1

我们需要在应用程序中添加性能监视。对于原型,我已经创建了一个示例项目,我正在尝试进入工作。

我正在尝试使用policyInjection作为性能计数器,因此我们将能够在生产环境中打开和关闭性能监视。

到目前为止,我可以在perfmon中看到实际的类别,但是我看不到任何实例(见图),甚至我非常确定应用程序正在运行并且实例是存在的,就像您在附加的源代码中看到的那样。

我尝试过很多东西,也在谷歌上搜索过,但是没有找到任何可用的解决方案或者找不到什么线索。

应用程序创建为consoleApplication

您还可以下载VS的压缩项目:http://dl.dropbox.com/u/19457132/stackOverflow/Mpd.Instrumentation.PerformanceCounter.zip

这是我的消息来源。

Program.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
using System;

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

    }
}

最后是app.config

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-16 20:00:42

由于SampleMethod接受一个参数,所以需要将其添加到匹配的规则配置中:

代码语言:javascript
复制
    <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>

如果没有参数,则匹配规则不匹配,因此不会调用调用处理程序。如果您修改了配置,您应该会看到perfmon中的实例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10616322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档