示例#1
0
        private static void InitializingSwitch(object sender, InitializingSwitchEventArgs e)
        {
            Switch sw = e.Switch;

            // Ported from https://referencesource.microsoft.com/#System/compmod/system/diagnostics/Switch.cs,173
            SwitchElementsCollection switchSettings = DiagnosticsConfiguration.SwitchSettings;

            if (switchSettings != null)
            {
                SwitchElement mySettings = switchSettings[sw.DisplayName];

                if (mySettings != null)
                {
                    if (mySettings.Value != null)
                    {
                        sw.Value = mySettings.Value;
                    }
                    else
                    {
                        sw.Value = sw.DefaultValue;
                    }

                    TraceUtils.CopyStringDictionary(sw.Attributes, mySettings.Attributes);
                }
            }
        }
示例#2
0
        private static void InitializingTraceSource(object sender, InitializingTraceSourceEventArgs e)
        {
            TraceSource traceSource = e.TraceSource;

            // Ported from https://referencesource.microsoft.com/#System/compmod/system/diagnostics/TraceSource.cs,176
            SourceElementsCollection sources = DiagnosticsConfiguration.Sources;

            if (sources != null)
            {
                SourceElement sourceElement = sources[traceSource.Name];
                if (sourceElement != null)
                {
                    e.WasInitialized = true;

                    // First check if the type changed.
                    if (HasSourceSwitchTypeChanged())
                    {
                        if (!string.IsNullOrEmpty(sourceElement.SwitchName))
                        {
                            CreateSwitch(sourceElement.SwitchType, sourceElement.SwitchName);
                        }
                        else
                        {
                            CreateSwitch(sourceElement.SwitchType, traceSource.Name);

                            if (!string.IsNullOrEmpty(sourceElement.SwitchValue))
                            {
                                traceSource.Switch.Level = (SourceLevels)Enum.Parse(typeof(SourceLevels), sourceElement.SwitchValue);
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(sourceElement.SwitchName))
                    {
                        // Create a new switch if the name changed, otherwise just refresh.
                        if (sourceElement.SwitchName != traceSource.Switch.DisplayName)
                        {
                            CreateSwitch(sourceElement.SwitchType, sourceElement.SwitchName);
                        }
                        else
                        {
                            traceSource.Switch.Refresh();
                        }
                    }
                    else
                    {
                        // The SwitchValue changed; just update our internalSwitch.
                        if (!string.IsNullOrEmpty(sourceElement.SwitchValue))
                        {
                            traceSource.Switch.Level = (SourceLevels)Enum.Parse(typeof(SourceLevels), sourceElement.SwitchValue);
                        }
                        else
                        {
                            traceSource.Switch.Level = SourceLevels.Off;
                        }
                    }

                    TraceListener[] newListenerCollection = new TraceListener[sourceElement.Listeners.Count];
                    int             listnerOffset         = 0;
                    foreach (ListenerElement listenerElement in sourceElement.Listeners)
                    {
                        TraceListener listener = traceSource.Listeners[listenerElement.Name];
                        if (listener != null)
                        {
                            newListenerCollection[listnerOffset++] = listenerElement.RefreshRuntimeObject(listener);
                        }
                        else
                        {
                            newListenerCollection[listnerOffset++] = listenerElement.GetRuntimeObject();
                        }
                    }

                    TraceUtils.CopyStringDictionary(sourceElement.Attributes, traceSource.Attributes);

                    traceSource.Listeners.Clear();
                    traceSource.Listeners.AddRange(newListenerCollection);
                }
                else
                {
                    // There was no config, so clear whatever we have.
                    traceSource.Switch.Level = traceSource.DefaultLevel;
                    traceSource.Listeners.Clear();
                    traceSource.Attributes.Clear();
                }

                bool HasSourceSwitchTypeChanged()
                {
                    string sourceTypeName = sourceElement.SwitchType;
                    Type   currentType    = traceSource.Switch.GetType();

                    if (string.IsNullOrEmpty(sourceTypeName))
                    {
                        // SourceSwitch is the default switch type.
                        return(currentType != typeof(SourceSwitch));
                    }

                    if (sourceTypeName == currentType.FullName)
                    {
                        return(false);
                    }

                    // Since there can be more than one valid AssemblyQualifiedName for a given Type this
                    // check can return true for some cases which can cause a minor side effect of a new
                    // Switch being created instead of just being refreshed.
                    return(sourceElement.SwitchType != currentType.AssemblyQualifiedName);
                }
            }

            void CreateSwitch(string typeName, string name)
            {
                if (!string.IsNullOrEmpty(typeName))
                {
                    traceSource.Switch = (SourceSwitch)TraceUtils.GetRuntimeObject(typeName, typeof(SourceSwitch), name);
                }
                else
                {
                    traceSource.Switch = new SourceSwitch(name, traceSource.DefaultLevel.ToString());
                }
            }
        }