System.Diagnostics.ListenerElement.GetRuntimeObject C# (CSharp) Method

GetRuntimeObject() public method

public GetRuntimeObject ( ) : TraceListener
return TraceListener
        public TraceListener GetRuntimeObject() {
            if (_runtimeObject != null)
                return (TraceListener) _runtimeObject;

            try {
                string className = TypeName;
                if (String.IsNullOrEmpty(className)) {
                    // Look it up in SharedListeners
                    Debug.Assert(_allowReferences, "_allowReferences must be true if type name is null");

                    if (_attributes != null || ElementInformation.Properties[_propFilter.Name].ValueOrigin == PropertyValueOrigin.SetHere || TraceOutputOptions != TraceOptions.None || !String.IsNullOrEmpty(InitData))
                        throw new ConfigurationErrorsException(SR.GetString(SR.Reference_listener_cant_have_properties, Name));
                        
                    if (DiagnosticsConfiguration.SharedListeners == null)
                        throw new ConfigurationErrorsException(SR.GetString(SR.Reference_to_nonexistent_listener, Name));
                    
                    ListenerElement sharedListener = DiagnosticsConfiguration.SharedListeners[Name];
                    if (sharedListener == null)
                        throw new ConfigurationErrorsException(SR.GetString(SR.Reference_to_nonexistent_listener, Name));
                    else {
                        _runtimeObject = sharedListener.GetRuntimeObject();
                        return (TraceListener) _runtimeObject;
                    }
                }
                else {
                    // create a new one
                    TraceListener newListener = (TraceListener) BaseGetRuntimeObject();
                    newListener.initializeData = InitData;
                    newListener.Name = Name;
                    newListener.SetAttributes(Attributes);
                    newListener.TraceOutputOptions = TraceOutputOptions;
                    
                    if ((Filter != null) && (Filter.TypeName != null) && (Filter.TypeName.Length != 0)) 
                        newListener.Filter = Filter.GetRuntimeObject();

                    _runtimeObject = newListener;
                    return newListener;
                }
            }
            catch (ArgumentException e) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_create_listener, Name), e);
            }
        }
        

Usage Example

        public TraceListener GetRuntimeObject()
        {
            if (_runtimeObject != null)
            {
                return((TraceListener)_runtimeObject);
            }

            try {
                string className = TypeName;
                if (String.IsNullOrEmpty(className))
                {
                    // Look it up in SharedListeners
                    Debug.Assert(_allowReferences, "_allowReferences must be true if type name is null");

                    if (_attributes != null || ElementInformation.Properties[_propFilter.Name].ValueOrigin == PropertyValueOrigin.SetHere || TraceOutputOptions != TraceOptions.None || !String.IsNullOrEmpty(InitData))
                    {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Reference_listener_cant_have_properties, Name));
                    }

                    if (DiagnosticsConfiguration.SharedListeners == null)
                    {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Reference_to_nonexistent_listener, Name));
                    }

                    ListenerElement sharedListener = DiagnosticsConfiguration.SharedListeners[Name];
                    if (sharedListener == null)
                    {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Reference_to_nonexistent_listener, Name));
                    }
                    else
                    {
                        _runtimeObject = sharedListener.GetRuntimeObject();
                        return((TraceListener)_runtimeObject);
                    }
                }
                else
                {
                    // create a new one
                    TraceListener newListener = (TraceListener)BaseGetRuntimeObject();
                    newListener.initializeData = InitData;
                    newListener.Name           = Name;
                    newListener.SetAttributes(Attributes);
                    newListener.TraceOutputOptions = TraceOutputOptions;

                    if ((Filter != null) && (Filter.TypeName != null) && (Filter.TypeName.Length != 0))
                    {
                        newListener.Filter = Filter.GetRuntimeObject();
                    }

                    _runtimeObject = newListener;
                    return(newListener);
                }
            }
            catch (ArgumentException e) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Could_not_create_listener, Name), e);
            }
        }
All Usage Examples Of System.Diagnostics.ListenerElement::GetRuntimeObject