Exemple #1
0
        static PerformanceCounters()
        {
            PerformanceCounterScope scope = GetPerformanceCountersFromConfig();

            if (PerformanceCounterScope.Off != scope)
            {
                try
                {
                    if (scope == PerformanceCounterScope.Default)
                    {
                        scope = OSEnvironmentHelper.IsVistaOrGreater ? PerformanceCounterScope.ServiceOnly : PerformanceCounterScope.Off;
                    }
                    PerformanceCounters.scope = scope;
                }
                catch (SecurityException securityException)
                {
                    //switch off the counters - not supported in PT
                    PerformanceCounters.scope = PerformanceCounterScope.Off;

                    // not re-throwing on purpose
                    DiagnosticUtility.TraceHandledException(securityException, TraceEventType.Warning);
                    if (DiagnosticUtility.ShouldTraceWarning)
                    {
                        TraceUtility.TraceEvent(System.Diagnostics.TraceEventType.Warning,
                                                TraceCode.PerformanceCounterFailedToLoad,
                                                SR.GetString(SR.PartialTrustPerformanceCountersNotEnabled));
                    }
                }
            }
            else
            {
                PerformanceCounters.scope = PerformanceCounterScope.Off;
            }
        }
Exemple #2
0
        static PerformanceCounters()
        {
            PerformanceCounterScope performanceCountersFromConfig = GetPerformanceCountersFromConfig();

            if (performanceCountersFromConfig != PerformanceCounterScope.Off)
            {
                try
                {
                    if (performanceCountersFromConfig == PerformanceCounterScope.Default)
                    {
                        performanceCountersFromConfig = OSEnvironmentHelper.IsVistaOrGreater ? PerformanceCounterScope.ServiceOnly : PerformanceCounterScope.Off;
                    }
                    scope = performanceCountersFromConfig;
                }
                catch (SecurityException exception)
                {
                    scope = PerformanceCounterScope.Off;
                    if (DiagnosticUtility.ShouldTraceWarning)
                    {
                        DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Warning);
                        TraceUtility.TraceEvent(TraceEventType.Warning, 0x80038, System.ServiceModel.SR.GetString("PartialTrustPerformanceCountersNotEnabled"));
                    }
                }
            }
            else
            {
                scope = PerformanceCounterScope.Off;
            }
        }
        static PerformanceCounters()
        {
            PerformanceCounterScope scope = GetPerformanceCountersFromConfig();
            if (PerformanceCounterScope.Off != scope)
            {
                try
                {
                    if (scope == PerformanceCounterScope.Default)
                    {
                        scope = OSEnvironmentHelper.IsVistaOrGreater ? PerformanceCounterScope.ServiceOnly : PerformanceCounterScope.Off;
                    }
                    PerformanceCounters.scope = scope;
                }
                catch (SecurityException securityException)
                {
                    //switch off the counters - not supported in PT
                    PerformanceCounters.scope = PerformanceCounterScope.Off;

                    // not re-throwing on purpose
                    DiagnosticUtility.TraceHandledException(securityException, TraceEventType.Warning);
                    if (DiagnosticUtility.ShouldTraceWarning)
                    {
                        TraceUtility.TraceEvent(System.Diagnostics.TraceEventType.Warning,
                                                    TraceCode.PerformanceCounterFailedToLoad,
                                                    SR.GetString(SR.PartialTrustPerformanceCountersNotEnabled));                        
                    }
                }
            }
            else
            {
                PerformanceCounters.scope = PerformanceCounterScope.Off;
            }
        }
 public static void Validate(PerformanceCounterScope value)
 {
     if (!IsDefined(value))
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnumArgumentException("value", (int)value, typeof(PerformanceCounterScope)));
     }
 }
 public static void Validate(PerformanceCounterScope value)
 {
     if (!IsDefined(value))
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnumArgumentException("value", (int) value, typeof(PerformanceCounterScope)));
     }
 }
 internal static bool IsDefined(PerformanceCounterScope value)
 {
     return
         (value == PerformanceCounterScope.Off ||
          value == PerformanceCounterScope.Default ||
          value == PerformanceCounterScope.ServiceOnly ||
          value == PerformanceCounterScope.All);
 }
 internal static bool IsDefined(PerformanceCounterScope value)
 {
     return
         value == PerformanceCounterScope.Off
         || value == PerformanceCounterScope.Default
         || value == PerformanceCounterScope.ServiceOnly
         || value == PerformanceCounterScope.All;
 }
 internal static bool IsDefined(PerformanceCounterScope value)
 {
     if (((value != PerformanceCounterScope.Off) && (value != PerformanceCounterScope.Default)) && (value != PerformanceCounterScope.ServiceOnly))
     {
         return(value == PerformanceCounterScope.All);
     }
     return(true);
 }
 internal static bool IsDefined(PerformanceCounterScope value)
 {
     if (((value != PerformanceCounterScope.Off) && (value != PerformanceCounterScope.Default)) && (value != PerformanceCounterScope.ServiceOnly))
     {
         return (value == PerformanceCounterScope.All);
     }
     return true;
 }
        public static void SetApplicationServiceModelPerformanceCounters(PerformanceCounterScope performanceCounterScope)
        {
            //Performance Counters: http://blogs.microsoft.co.il/idof/2011/08/11/wcf-scaling-check-your-counters/
            //Performance Counters: https://docs.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/performance-counters/
            Configuration            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ServiceModelSectionGroup serviceModelConfigurationGroup = ServiceModelSectionGroup.GetSectionGroup(config);

            serviceModelConfigurationGroup.Diagnostic.PerformanceCounters = performanceCounterScope;
            config.Save();
        }
Exemple #11
0
        static internal PerformanceCounter GetPerformanceCounterInternal(string categoryName, string perfCounterName, string instanceName, PerformanceCounterInstanceLifetime instanceLifetime)
        {
            PerformanceCounter counter = null;

            try
            {
                counter = new PerformanceCounter();
                counter.CategoryName     = categoryName;
                counter.CounterName      = perfCounterName;
                counter.InstanceName     = instanceName;
                counter.ReadOnly         = false;
                counter.InstanceLifetime = instanceLifetime;

                // We now need to access the counter raw data to
                // force the counter object to be initialized.  This
                // will force any exceptions due to mis-installation
                // of counters to occur here and be traced appropriately.
                try
                {
                    long rawValue = counter.RawValue;
                }
                catch (InvalidOperationException)
                {
                    counter = null;
                    throw;
                }
                catch (SecurityException securityException)
                {
                    // Cannot access performance counter due to partial trust scenarios
                    // Disable the default performance counters' access otherwise
                    // in PT the service will be broken
                    PerformanceCounters.scope = PerformanceCounterScope.Off;

                    DiagnosticUtility.TraceHandledException(new SecurityException(SR.GetString(
                                                                                      SR.PartialTrustPerformanceCountersNotEnabled), securityException), TraceEventType.Warning);

                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              new SecurityException(SR.GetString(
                                                        SR.PartialTrustPerformanceCountersNotEnabled)));
                }
            }
#pragma warning suppress 56500 // covered by FxCOP
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                if (null != counter)
                {
                    if (!counter.ReadOnly)
                    {
                        try
                        {
                            counter.RemoveInstance();
                        }
                        // Already inside a catch block for a failure case
                        // ok to ---- any exceptions here and trace the
                        // original failure.
#pragma warning suppress 56500 // covered by FxCOP
                        catch (Exception e1)
                        {
                            if (Fx.IsFatal(e1))
                            {
                                throw;
                            }
                        }
                    }

                    counter = null;
                }
                bool logEvent = true;
                if (categoryName == PerformanceCounterStrings.SERVICEMODELSERVICE.ServicePerfCounters)
                {
                    if (serviceOOM == false)
                    {
                        serviceOOM = true;
                    }
                    else
                    {
                        logEvent = false;
                    }
                }
                else if (categoryName == PerformanceCounterStrings.SERVICEMODELOPERATION.OperationPerfCounters)
                {
                    if (operationOOM == false)
                    {
                        operationOOM = true;
                    }
                    else
                    {
                        logEvent = false;
                    }
                }
                else if (categoryName == PerformanceCounterStrings.SERVICEMODELENDPOINT.EndpointPerfCounters)
                {
                    if (endpointOOM == false)
                    {
                        endpointOOM = true;
                    }
                    else
                    {
                        logEvent = false;
                    }
                }

                if (logEvent)
                {
                    DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                                        (ushort)System.Runtime.Diagnostics.EventLogCategory.PerformanceCounter,
                                                        (uint)System.Runtime.Diagnostics.EventLogEventId.FailedToLoadPerformanceCounter,
                                                        categoryName,
                                                        perfCounterName,
                                                        e.ToString());
                }
            }
            return(counter);
        }
Exemple #12
0
        internal static PerformanceCounter GetPerformanceCounterInternal(string categoryName, string perfCounterName, string instanceName, PerformanceCounterInstanceLifetime instanceLifetime)
        {
            PerformanceCounter counter = null;

            try
            {
                counter = new PerformanceCounter {
                    CategoryName     = categoryName,
                    CounterName      = perfCounterName,
                    InstanceName     = instanceName,
                    ReadOnly         = false,
                    InstanceLifetime = instanceLifetime
                };
                try
                {
                    long rawValue = counter.RawValue;
                }
                catch (InvalidOperationException)
                {
                    counter = null;
                    throw;
                }
                catch (SecurityException exception)
                {
                    if (MinimalPerformanceCountersEnabled)
                    {
                        scope = PerformanceCounterScope.Off;
                    }
                    if (DiagnosticUtility.ShouldTraceWarning)
                    {
                        DiagnosticUtility.ExceptionUtility.TraceHandledException(new SecurityException(System.ServiceModel.SR.GetString("PartialTrustPerformanceCountersNotEnabled"), exception), TraceEventType.Warning);
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityException(System.ServiceModel.SR.GetString("PartialTrustPerformanceCountersNotEnabled")));
                }
            }
            catch (Exception exception2)
            {
                if (Fx.IsFatal(exception2))
                {
                    throw;
                }
                if (counter != null)
                {
                    if (!counter.ReadOnly)
                    {
                        try
                        {
                            counter.RemoveInstance();
                        }
                        catch (Exception exception3)
                        {
                            if (Fx.IsFatal(exception3))
                            {
                                throw;
                            }
                        }
                    }
                    counter = null;
                }
                bool flag = true;
                if (categoryName == "ServiceModelService 4.0.0.0")
                {
                    if (!serviceOOM)
                    {
                        serviceOOM = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
                else if (categoryName == "ServiceModelOperation 4.0.0.0")
                {
                    if (!operationOOM)
                    {
                        operationOOM = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
                else if (categoryName == "ServiceModelEndpoint 4.0.0.0")
                {
                    if (!endpointOOM)
                    {
                        endpointOOM = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
                if (flag)
                {
                    DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, EventLogCategory.PerformanceCounter, (EventLogEventId)(-1073610742), new string[] { categoryName, perfCounterName, exception2.ToString() });
                }
            }
            return(counter);
        }
        static internal PerformanceCounter GetPerformanceCounterInternal(string categoryName, string perfCounterName, string instanceName, PerformanceCounterInstanceLifetime instanceLifetime)
        {
            PerformanceCounter counter = null;
            try
            {
                counter = new PerformanceCounter();
                counter.CategoryName = categoryName;
                counter.CounterName = perfCounterName;
                counter.InstanceName = instanceName;
                counter.ReadOnly = false;
                counter.InstanceLifetime = instanceLifetime;

                // We now need to access the counter raw data to
                // force the counter object to be initialized.  This
                // will force any exceptions due to mis-installation
                // of counters to occur here and be traced appropriately.
                try
                {
                    long rawValue = counter.RawValue;
                }
                catch (InvalidOperationException)
                {
                    counter = null;
                    throw;
                }
                catch (SecurityException securityException)
                {
                    // Cannot access performance counter due to partial trust scenarios
                    // Disable the default performance counters' access otherwise
                    // in PT the service will be broken
                    PerformanceCounters.scope = PerformanceCounterScope.Off;

                    DiagnosticUtility.TraceHandledException(new SecurityException(SR.GetString(
                                SR.PartialTrustPerformanceCountersNotEnabled), securityException), TraceEventType.Warning);
                    
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                        new SecurityException(SR.GetString(
                                SR.PartialTrustPerformanceCountersNotEnabled)));
                }
            }
#pragma warning suppress 56500 // covered by FxCOP
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                if (null != counter)
                {
                    if (!counter.ReadOnly)
                    {
                        try
                        {
                            counter.RemoveInstance();
                        }
                        // Already inside a catch block for a failure case
                        // ok to ---- any exceptions here and trace the
                        // original failure.
#pragma warning suppress 56500 // covered by FxCOP
                        catch (Exception e1)
                        {
                            if (Fx.IsFatal(e1))
                            {
                                throw;
                            }
                        }
                    }

                    counter = null;
                }
                bool logEvent = true;
                if (categoryName == PerformanceCounterStrings.SERVICEMODELSERVICE.ServicePerfCounters)
                {
                    if (serviceOOM == false)
                    {
                        serviceOOM = true;
                    }
                    else
                    {
                        logEvent = false;
                    }
                }
                else if (categoryName == PerformanceCounterStrings.SERVICEMODELOPERATION.OperationPerfCounters)
                {
                    if (operationOOM == false)
                    {
                        operationOOM = true;
                    }
                    else
                    {
                        logEvent = false;
                    }
                }
                else if (categoryName == PerformanceCounterStrings.SERVICEMODELENDPOINT.EndpointPerfCounters)
                {
                    if (endpointOOM == false)
                    {
                        endpointOOM = true;
                    }
                    else
                    {
                        logEvent = false;
                    }
                }

                if (logEvent)
                {
                    DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                             (ushort)System.Runtime.Diagnostics.EventLogCategory.PerformanceCounter,
                                             (uint)System.Runtime.Diagnostics.EventLogEventId.FailedToLoadPerformanceCounter,
                                             categoryName,
                                             perfCounterName,
                                             e.ToString());
                }
            }
            return counter;
        }