我在这里以下答案:缺少网络发送/接收
而且他说的那样实现一切。我的显示器被初始化像这样:
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