缺少网络发送/接收 [英] Missing network sent/received

查看:86
本文介绍了缺少网络发送/接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里关注答案:

计算带宽

并像他所说的那样实施了一切.我的监视器初始化如下:

And implemented everything like he said. My monitor is initialized like so:

netSentCounter.CategoryName = ".NET CLR Networking";
netSentCounter.CounterName = "Bytes Sent";
netSentCounter.InstanceName = Misc.GetInstanceName();
netSentCounter.ReadOnly = true;

我可以看到Misc.GetInstanceName()返回"MyProcessName [id]".但是,我不断收到一个例外,该实例在指定的类别中不存在.

I can corrently see that Misc.GetInstanceName() returns "MyProcessName[id]". However I keep getting the exception that the instance doesn't exist in the specified category.

我的理解是,直到您实际发送或接收时,才创建发送/接收网络的类别.

My understanding is that the category for net sent/received isn't created until you actually send or receive.

我已经按照答案中的说明添加了app.config,如下所示:

I have added the app.config as described in the answer like so:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.net>
        <settings>
            <performanceCounters enabled="true" />
        </settings>
    </system.net>
</configuration>

为什么我仍然出现错误?

Why do I still get an error?

这是我的监控代码:

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;
    }
 }

我已经从Windows打开了资源监视器,以查看问题所在.尽管已将app.config设置为这样做,但计数器尚未启动.

I have opened the resource monitor from windows to see what the problem is. The counter isn't being started albeit having the app.config set to do so.

这是我所看到的(在我的应用发送网络活动之前和之后)

Here is what i see (this before and after my application sending network activity)

该名称不是我的方法返回的名称.我的方法返回"SuperScraper [appId]",而在资源中称为"Superscraper.vshost.exe".

And the name isn't what my method is returning. My method returns "SuperScraper[appId]" while in the resource it is called "Superscraper.vshost.exe".

所以我现在有两个问题:

So I have two problems now:

-我的计数器未在应用启动时启动 -名字是differnet

-My counter isn't starting on app startup -The name is differnet

推荐答案

好,我终于明白了. 计算带宽中列出的步骤似乎已经过时,并且不再适用于.Net 4.

Ok I have finally figured it out. The steps listed Calculating Bandwidth seem to be outdated and are no longer valid for .Net 4.

我将解释整个过程,以便您也可以这样做.

I am going to explain the whole process so you can do it aswell.

首先,您需要将其添加到您的app.config:

First off, you need to add this to your app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.net>
        <settings>
            <performanceCounters enabled="true" />
        </settings>
    </system.net>
</configuration>

在.Net 4.0之前,这使应用程序在启动时创建计数器(例如,当您的应用程序发送请求时,不创建网络计数器).在.Net 4.0中,这告诉它在使用计数器时创建它们. IE.如果您未设置,则不会创建任何计数器.

Before .Net 4.0, this made the application create the counters on startup (and not for example, create the network counters when your application sends a request). In .Net 4.0, this tells it to create the counters when they are used. Ie. if you don't set this no counters will ever be created.

现在这是我大部分时间都在浪费的地方.我有一个错误的假设:如果您创建一个性能计数器,其名称与自然创建时的名称相同,那么您将获得这些值.但是,所有这些操作都阻止了真实计数器的显示.

Now this is where I wasted most of my time. I was under the wrong assumption that if you create a performance counter with the same name that it would have had if it was created naturally, you would get the values. However all this does is block the real counter from showing up.

这样做:

//In .Net 4.0 the category is called .NET CLR Networking 4.0.0.0 and not .NET CLR Networking
netSentCounter.CategoryName = ".NET CLR Networking 4.0.0.0";
netSentCounter.CounterName = "Bytes Sent";
netSentCounter.InstanceName = Misc.GetInstanceName();
netSentCounter.ReadOnly = false; //<==
netSentCounter.RawValue = 0;     //<==

简单地阻止实数计数器.

Simply blocks the real counter.

您需要做的是自然地启动它.最好的方法是在应用程序启动时简单地发送一个欺骗请求,然后使用以下命令监听"性能计数器:

What you need to do is start it up in a natural way. The best way to do this is to simply send a spoof request at application start, and then "listen" to the performance counter using:

//Send spoof request here
netSentCounter.CategoryName = ".NET CLR Networking 4.0.0.0";
netSentCounter.CounterName = "Bytes Sent";
netSentCounter.InstanceName = Misc.GetInstanceName();
netSentCounter.ReadOnly = true;

最后一点.实例名称不再是ApplicationName[appId].现在是:

One final point. The instance name is no longer ApplicationName[appId]. Now it is:

[ApplicationName] .exe_p [appId] _r [the CLR id hosting your application] _ad [ApplicationDomain]

[ApplicationName].exe_p[appId]_r[the CLR id hosting your application]_ad[ApplicationDomain]

希望这可以节省一些时间!

Hope this saves someone time!!

这篇关于缺少网络发送/接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆