Пример #1
0
        private static bool Initialize()
        {
            //
            // on Win9x you have no PerfCounters
            // on NT4 PerfCounters are not writable
            //
            if (ComNetOS.IsWin9x || ComNetOS.IsWinNt4)
            {
                return(false);
            }

            //
            // this is an internal class, we need to update performance counters
            // on behalf of the user to log perf data on network activity
            //
            // Consider V.Next: Change to declarative form (10x faster) but
            // PerformanceCounterPermission must be moved out of System.dll
            PerformanceCounterPermission perfCounterPermission = new PerformanceCounterPermission(PermissionState.Unrestricted);

            perfCounterPermission.Assert();

            bool successStatus = false;

            try {
                //
                // create the counters, this will check for the right permissions (false)
                // means the counter is not readonly (it's read/write) and cache them while
                // we're under the Assert(), which will be reverted in the finally below.
                //
                GlobalConnectionsEstablished = new PerformanceCounter(CategoryName, ConnectionsEstablishedName, Global, false);

                //
                // if I created the first counter succesfully, then I'll return
                // true. otherwise I'll return false, since none of the counters will
                // be created, hence non of them should be updated.
                //
                successStatus = true;

                GlobalBytesReceived     = new PerformanceCounter(CategoryName, BytesReceivedName, Global, false);
                GlobalBytesSent         = new PerformanceCounter(CategoryName, BytesSentName, Global, false);
                GlobalDatagramsReceived = new PerformanceCounter(CategoryName, DatagramsReceivedName, Global, false);
                GlobalDatagramsSent     = new PerformanceCounter(CategoryName, DatagramsSentName, Global, false);
                ConnectionsEstablished  = new PerformanceCounter(CategoryName, ConnectionsEstablishedName, false);
                BytesReceived           = new PerformanceCounter(CategoryName, BytesReceivedName, false);
                BytesSent         = new PerformanceCounter(CategoryName, BytesSentName, false);
                DatagramsReceived = new PerformanceCounter(CategoryName, DatagramsReceivedName, false);
                DatagramsSent     = new PerformanceCounter(CategoryName, DatagramsSentName, false);
#if COMNET_HTTPPERFCOUNTER
// jruiz: If you need to use this counters, you will have to change the files
// _NetworkingPerfCounters.ini
// _NetworkingPerfCounters.h
                GlobalHttpWebRequestCreated   = new PerformanceCounter(CategoryName, HttpWebRequestCreatedName, false);
                GlobalHttpWebRequestCollected = new PerformanceCounter(CategoryName, HttpWebRequestCollectedName, Global, false);
                HttpWebRequestCreated         = new PerformanceCounter(CategoryName, HttpWebRequestCreatedName, false);
                HttpWebRequestCollected       = new PerformanceCounter(CategoryName, HttpWebRequestCollectedName, Global, false);
#endif // COMNET_HTTPPERFCOUNTER
            }
            catch (Exception exception) {
                GlobalLog.Print("NetworkingPerfCounters::NetworkingPerfCounters() instantiation failure:" + exception.Message);
            }
            finally {
                PerformanceCounterPermission.RevertAssert();
            }

            //
            // we will return false if none of the counters was created. true
            // if at least the first one was, ignoring subsequent failures.
            // we don't expect the counters to fail individually, if the first
            // one fails we'd expect all of them to fails, if the first one
            // succeeds, wed' expect all of them to succeed.
            //
            return(successStatus);
        }
        private void Initialize(object state)
        {
            if (Logging.On)
            {
                Logging.PrintInfo(Logging.Web, SR.GetString(SR.net_perfcounter_initialization_started));
            }

            PerformanceCounterPermission perfCounterPermission = new PerformanceCounterPermission(PermissionState.Unrestricted);

            perfCounterPermission.Assert();
            try
            {
                if (!PerformanceCounterCategory.Exists(categoryName))
                {
                    // if the perf. counter category doesn't exist, just log this information and exit.
                    if (Logging.On)
                    {
                        Logging.PrintError(Logging.Web, SR.GetString(SR.net_perfcounter_nocategory, categoryName));
                    }
                    return;
                }

                string instanceName = GetInstanceName();

                Debug.Assert(counterNames.Length == Enum.GetValues(typeof(NetworkingPerfCounterName)).Length,
                             "The number of NetworkingPerfCounterName items must match the number of CounterNames");

                // create the counters, this will check for the right permissions (false)
                // means the counter is not readonly (it's read/write) and cache them while
                // we're under the Assert(), which will be reverted in the finally below.
                counters = new CounterPair[counterNames.Length];
                for (int i = 0; i < counterNames.Length; i++)
                {
                    counters[i] = CreateCounterPair(counterNames[i], instanceName);
                }

                AppDomain.CurrentDomain.DomainUnload       += new EventHandler(UnloadEventHandler);
                AppDomain.CurrentDomain.ProcessExit        += new EventHandler(ExitEventHandler);
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionEventHandler);

                initSuccessful = true;
            }
            catch (Win32Exception e)
            {
                if (Logging.On)
                {
                    Logging.Exception(Logging.Web, "NetworkingPerfCounters", "Initialize", e);
                }
                Cleanup();
                return;
            }
            catch (InvalidOperationException e)
            {
                if (Logging.On)
                {
                    Logging.Exception(Logging.Web, "NetworkingPerfCounters", "Initialize", e);
                }
                Cleanup();
                return;
            }
            finally
            {
                PerformanceCounterPermission.RevertAssert();

                initDone = true;

                if (Logging.On)
                {
                    if (initSuccessful)
                    {
                        Logging.PrintInfo(Logging.Web, SR.GetString(SR.net_perfcounter_initialized_success));
                    }
                    else
                    {
                        Logging.PrintInfo(Logging.Web, SR.GetString(SR.net_perfcounter_initialized_error));
                    }
                }
            }
        }