0
我需要從網卡獲取性能計數器。爲了使事情變得容易,用戶通過在控制檯應用程序中輸入索引號來選擇所需的適配器。無法連接到一個VM上的網絡適配器,相同的代碼在另一個VM上工作
這是獲取用戶輸入並創建性能計數器實例的代碼
var connectionNames = NetworkCardLocator.GetConnectedCardNames().ToArray();
log.Debug("Please select one of the available connections");
log.Debug("--");
for (int i = 0; i < connectionNames.Count(); i++)
{
log.Debug(i + ". " + connectionNames[i]);
}
log.Debug("--");
var key = Console.ReadLine();
int idx = 0;
Int32.TryParse(key, out idx);
string connectionName = connectionNames[idx];
var networkBytesSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", connectionName);
var networkBytesReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", connectionName);
var networkBytesTotal = new PerformanceCounter("Network Interface", "Bytes Total/sec", connectionName);
這是我的選擇可用適配器
internal static IEnumerable<string> GetConnectedCardNames()
{
string query = String.Format(@"SELECT * FROM Win32_NetworkAdapter");
var searcher = new ManagementObjectSearcher
{
Query = new ObjectQuery(query)
};
try
{
log.Debug("Trying to select network adapters");
var adapterObjects = searcher.Get();
var names = (from ManagementObject o in adapterObjects
select o["Name"])
.Cast<string>();
return names;
}
catch (Exception ex)
{
log.Debug("Failed to get needed names, see Exception log for details");
log.Fatal(ex);
throw;
}
}
問題
鑑於我選擇了所需的適配器,代碼在我的機器上運行(Win 2008 R2 Ent x64)。它在我用於測試的某些虛擬機上不起作用(Win 2008 R1 DC x86)。任何選擇也給了我一個例外(仍然工作在我的電腦和VM贏2008 R1標準的x86上)
foreach (PerformanceCounter counter in counters)
{
float rawValue = counter.NextValue(); //thrown here
...
}
2011-06-10 11:08:20,505 [10] DEBUG TH.Exceptions Instance 'WAN Miniport (PPTP)' does not exist in the specified Category.
System.InvalidOperationException: Instance 'WAN Miniport (PPTP)' does not exist in the specified Category.
at System.Diagnostics.CounterDefinitionSample.GetInstanceValue(String instanceName)
at System.Diagnostics.PerformanceCounter.NextSample()
at System.Diagnostics.PerformanceCounter.NextValue()
at TH.PerformanceMonitor.API.Internal.PerformanceLogService.DoPerformanceLogging(IEnumerable`1 counters, Int32 interval, TimeSpan duration) in C:\Projects\...\PerformanceLogService.cs:line 122
at TH.PerformanceMonitor.API.PerformanceManager.DoPerformanceLogging() in C:\Projects\...\PerformanceManager.cs:line 294
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Q
可能是什麼問題,或者我怎麼可以跟蹤的原因爲何?