VS 2005、C# 2.0、.NET 2.0/3.0、Win2003
我正在尝试为 MultiInstance 安装一组性能计数器。我注意到,即使没有其他实例,某些系统性能计数器类别也设法使“总数”保持活跃。ASP.NET Apps 2.0.50727 就是一个例子。
所以我一直在尝试复制这个。我在 Installer 类中创建了以下例程,然后将其添加到安装项目中的自定义操作中。
public override void Install(System.Collections.IDictionary stateSaver)
{
//Debugger.Break();
CounterCreationData data = new CounterCreationData("ZCounter", "ZCtrHelp", PerformanceCounterType.NumberOfItems32);
PerformanceCounterCategory.Create("ZCategory", "ZCatHelp", PerformanceCounterCategoryType.MultiInstance, new CounterCreationDataCollection(new CounterCreationData[] { data }));
PerformanceCounter counter = new PerformanceCounter();
counter.CategoryName = "ZCategory";
counter.CounterName = "ZCounter";
counter.InstanceName = "ZTotal";
counter.InstanceLifetime = PerformanceCounterInstanceLifetime.Global;
counter.ReadOnly = false;
counter.RawValue = 0;
base.Install(stateSaver);
}
如果我取消注释该Debugger.Break()
行并逐步执行,我可以看到计数器实际上是使用正确的实例名称创建的,并且 Visual Studio Server Explorer 会显示该实例以及设置为 Global 的 InstanceLifetime。我没有在安装程序中调用 RemoveInstance() 方法。
然而,在安装程序完成几秒钟后,该实例从性能监视器和 VS 服务器资源管理器中消失。我如何让它粘住?或者我可以吗?