【问题标题】:Remote Performance Counters System.InvalidOperationException远程性能计数器 System.InvalidOperationException
【发布时间】:2015-02-16 04:15:30
【问题描述】:

我有 2 段代码偶尔会抛出错误。如果我在 server.FullyQualifiedDomainName 中定义的服务器上本地运行应用程序,我不会收到任何错误,永远不会。如果我远程运行应用程序并通过网络读取计数器,我将得到下面定义的错误。错误不会一直发生。我在某处读过网络上的性能计数器有点不稳定...无论如何要获得更可靠的响应?

读取类别的代码:

var windowsCounterCategory = new PerformanceCounterCategory(counterCategory, server.FullyQualifiedDomainName); 
var serviceGuidsStr = windowsCounterCategory.GetInstanceNames();

错误:

System.InvalidOperationException: Could not Read Category Index: 8324.
   at System.Diagnostics.CategorySample..ctor(Byte[] data, CategoryEntry entry, PerformanceCounterLib library)
   at System.Diagnostics.PerformanceCounterLib.GetCategorySample(String category)
   at System.Diagnostics.PerformanceCounterLib.GetCategorySample(String machine, String category)
   at System.Diagnostics.PerformanceCounterCategory.GetCounterInstances(String categoryName, String machineName)
   at System.Diagnostics.PerformanceCounterCategory.GetInstanceNames()

读取计数器值的代码:

   using (var perfomanceCounter = new PerformanceCounter(categoryName, counterName, instanceId, Service.Server.FullyQualifiedDomainName))
   {
      var counterValue = perfomanceCounter.RawValue;
      log.Trace("Read counter value " + counterValue.ToString());
      return counterValue;
   }

错误:

System.InvalidOperationException: Could not Read Category Index: 8324.
   at System.Diagnostics.CategorySample..ctor(Byte[] data, CategoryEntry entry, PerformanceCounterLib library)
   at System.Diagnostics.PerformanceCounterLib.GetCategorySample(String category)
   at System.Diagnostics.PerformanceCounterLib.GetCategorySample(String machine, String category)
   at System.Diagnostics.PerformanceCounter.NextSample()
   at System.Diagnostics.PerformanceCounter.get_RawValue()

【问题讨论】:

    标签: c# .net performancecounter system.diagnostics


    【解决方案1】:

    似乎通过网络读取性能计数器根本不是超级可靠。我决定使用 Microsoft 瞬态故障处理库(v 6.0,在 nuget 上搜索黄玉)并实施重试策略。通常重试一次就足以得到有效答案(没有异常),有时可能需要重试 2 次。

    我实施了一种瞬态故障检测策略来检查特定的网络相关错误 (System.InvalidOperationException: Could not Read Category Index: 8324.)

    public class PerformanceCounterStrategy : ITransientErrorDetectionStrategy
    {
      public bool IsTransient(Exception ex)
      {
        return ex.GetType() == typeof (InvalidOperationException);
      }
    }
    

    这是我实现的重试策略:

    var retryStrategy = new Incremental(3, 2, 2);
    var retryPolicy = new RetryPolicy<PerformanceCounterTransientStrategy>(retryStrategy);
    
    retryPolicy.ExecuteAction(() =>
      {
        var windowsCounterCategory = new PerformanceCounterCategory(counterCategory, server.FQDN);
        var guidsStr = windowsCounterCategory.GetInstanceNames();
    
      });
    

    【讨论】:

      猜你喜欢
      • 2011-12-31
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多