我在這裏以下答案:缺少網絡發送/接收
而且他說的那樣實現一切。我的顯示器被初始化像這樣:
netSentCounter.CategoryName = ".NET CLR Networking";
netSentCounter.CounterName = "Bytes Sent";
netSentCounter.InstanceName = Misc.GetInstanceName();
netSentCounter.ReadOnly = true;
我可以corrently看到Misc.GetInstanceName()
返回 「MyProcessName [ID]」。但是,我一直在獲取例外情況,即該實例不存在於指定的類別中。
我的理解是,發送/接收的網絡類別不會在您實際發送或接收之前創建。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<settings>
<performanceCounters enabled="true" />
</settings>
</system.net>
</configuration>
爲什麼我仍然得到一個錯誤:
像這樣的答案描述我已經添加了的app.config?
這裏是我的監管碼:
public static class Monitoring
{
private static PerformanceCounter netSentCounter = new PerformanceCounter();
//Static constructor
static Monitoring()
{
netSentCounter.CategoryName = ".NET CLR Networking";
netSentCounter.CounterName = "Bytes Sent";
netSentCounter.InstanceName = Misc.GetInstanceName();
netSentCounter.ReadOnly = true;
}
/// <summary>
/// Returns the amount of data sent from the current application in MB
/// </summary>
/// <returns></returns>
public static float getNetSent()
{
return (float)netSentCounter.NextValue()/1048576; //Convert to from Bytes to MB
}
}
我的雜項類:
public static class Misc
{
//Returns an instance name
internal static string GetInstanceName()
{
// Used Reflector to find the correct formatting:
string assemblyName = GetAssemblyName();
if ((assemblyName == null) || (assemblyName.Length == 0))
{
assemblyName = AppDomain.CurrentDomain.FriendlyName;
}
StringBuilder builder = new StringBuilder(assemblyName);
for (int i = 0; i < builder.Length; i++)
{
switch (builder[i])
{
case '/':
case '\\':
case '#':
builder[i] = '_';
break;
case '(':
builder[i] = '[';
break;
case ')':
builder[i] = ']';
break;
}
}
return string.Format(CultureInfo.CurrentCulture,
"{0}[{1}]",
builder.ToString(),
Process.GetCurrentProcess().Id);
}
/// <summary>
/// Returns an assembly name
/// </summary>
/// <returns></returns>
internal static string GetAssemblyName()
{
string str = null;
Assembly entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly != null)
{
AssemblyName name = entryAssembly.GetName();
if (name != null)
{
str = name.Name;
}
}
return str;
}
}
編輯:我從窗戶打開資源監視器,看看是什麼問題。計數器不會啓動,儘管app.config設置爲這樣做。
這是我所看到的(在此之前和之後我的應用程序發送的網絡活動)
和名字是不是我的方法是返回。我的方法返回「SuperScraper [appId]」,而在資源中它被稱爲「Superscraper.vshost.exe」。
所以我現在有兩個問題:
-My計數器沒有啓動應用程序啓動時 -The的名字是目前存在的
你能顯示堆棧跟蹤嗎?什麼是雜項? [Here](http://pastebin.com/f371375d6)是全部代碼,只是複製粘貼。 – Reniuz 2012-04-17 09:36:36
雜項只是我使用的圖書館。我會發布堆棧跟蹤。 – TheGateKeeper 2012-04-17 09:40:23
調用堆棧非常空。它所做的就是從我的庫中調用該方法。問題是因爲代碼位於庫中而不是我的主應用程序中?我的代碼非常像你的代碼。 – TheGateKeeper 2012-04-17 09:42:07