我们正在为webapi开发oracle数据库性能计数器项目。获取实例名称的代码如下所示
string pid = GetCurrentProcessId().ToString();
string instanceName2 =AppDomain.CurrentDomain.FriendlyName.ToString().Replace('(', '[')
.Replace(')', ']').Replace('#', '_').Replace('/', '_').Replace('\\', '_');
instName = instanceName2 + "[" + pid + "]";
并在PerformanceCounter类中设置实例
PerformanceCounter perfCounter = new PerformanceCounter();
perfCounter.CategoryName = catagoryName;
perfCounter.CounterName = counterName;
perfCounter.InstanceName = instName;
用于读取下面显示的计数器值的代码
foreach (PerformanceCounter p in this.PerfCounters)
{
string counterValue = string.Join("=", p.CounterName, p.NextValue());
obj.textBox1.AppendText("\r\n" + counterValue);
}
当我尝试读取计数器值获取异常时,例如Instance' [Instance Name]'在指定的Category中不存在。打开数据库连接后无法检测实例名称。