/// <summary>
        /// Builds the ExceptionManagementSettings and PublisherSettings structures based on the configuration file.
        /// </summary>
        /// <param name="parent">Composed from the configuration settings in a corresponding parent configuration section.</param>
        /// <param name="configContext">Provides access to the virtual path for which the configuration section handler computes configuration values. Normally this parameter is reserved and is null.</param>
        /// <param name="section">The XML node that contains the configuration information to be handled. section provides direct access to the XML contents of the configuration section.</param>
        /// <returns>The ExceptionManagementSettings struct built from the configuration settings.</returns>
        public object Create(object parent,object configContext,XmlNode section)
        {
            try
            {
                ExceptionManagementSettings settings = new ExceptionManagementSettings();

                // Exit if there are no configuration settings.
                if (section == null) return settings;

                XmlNode currentAttribute;
                XmlAttributeCollection nodeAttributes = section.Attributes;

                // Get the mode attribute.
                currentAttribute = nodeAttributes.RemoveNamedItem(EXCEPTIONMANAGEMENT_MODE);
                if (currentAttribute != null && currentAttribute.Value.ToUpper(CultureInfo.InvariantCulture) == "OFF")
                {
                    settings.Mode = ExceptionManagementMode.Off;
                }

                #region Loop through the publisher components and load them into the ExceptionManagementSettings
                // Loop through the publisher components and load them into the ExceptionManagementSettings.
                PublisherSettings publisherSettings;
                foreach(XmlNode node in section.ChildNodes)
                {
                    if (node.Name == PUBLISHER_NODENAME)
                    {
                        // Initialize a new PublisherSettings.
                        publisherSettings = new PublisherSettings();

                        // Get a collection of all the attributes.
                        nodeAttributes = node.Attributes;

                        #region Remove the known attributes and load the struct values
                        // Remove the mode attribute from the node and set its value in PublisherSettings.
                        currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_MODE);
                        if (currentAttribute != null && currentAttribute.Value.ToUpper(CultureInfo.InvariantCulture) == "OFF") publisherSettings.Mode = PublisherMode.Off;

                        // Remove the assembly attribute from the node and set its value in PublisherSettings.
                        currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_ASSEMBLY);
                        if (currentAttribute != null) publisherSettings.AssemblyName = currentAttribute.Value;

                        // Remove the type attribute from the node and set its value in PublisherSettings.
                        currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_TYPE);
                        if (currentAttribute != null) publisherSettings.TypeName = currentAttribute.Value;

                        // Remove the exceptionFormat attribute from the node and set its value in PublisherSettings.
                        currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_EXCEPTIONFORMAT);
                        if (currentAttribute != null && currentAttribute.Value.ToUpper(CultureInfo.InvariantCulture) == "XML") publisherSettings.ExceptionFormat = PublisherFormat.Xml;

                        // Remove the include attribute from the node and set its value in PublisherSettings.
                        currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_INCLUDETYPES);
                        if (currentAttribute != null)
                        {
                            publisherSettings.IncludeTypes = LoadTypeFilter(currentAttribute.Value.Split(EXCEPTION_TYPE_DELIMITER));
                        }

                        // Remove the exclude attribute from the node and set its value in PublisherSettings.
                        currentAttribute = nodeAttributes.RemoveNamedItem(PUBLISHER_EXCLUDETYPES);
                        if (currentAttribute != null)
                        {
                            publisherSettings.ExcludeTypes = LoadTypeFilter(currentAttribute.Value.Split(EXCEPTION_TYPE_DELIMITER));
                        }

                        #endregion

                        #region Loop through any other attributes and load them into OtherAttributes
                        // Loop through any other attributes and load them into OtherAttributes.
                        for (int i = 0; i < nodeAttributes.Count; i++)
                        {
                            publisherSettings.AddOtherAttributes(nodeAttributes.Item(i).Name,nodeAttributes.Item(i).Value);
                        }
                        #endregion

                        // Add the PublisherSettings to the publishers collection.
                        settings.Publishers.Add(publisherSettings);
                    }
                }

                // Remove extra allocated space of the ArrayList of Publishers.
                settings.Publishers.TrimToSize();

                #endregion

                // Return the ExceptionManagementSettings loaded with the values from the config file.
                return settings;
            }
            catch(Exception exc)
            {
                throw new System.Configuration.ConfigurationErrorsException(resourceManager.GetString("RES_EXCEPTION_LOADING_CONFIGURATION"), exc, section);
            }
        }
 /// <summary>
 /// Adds a PublisherSettings to the arraylist of publishers.
 /// </summary>
 public void AddPublisher(PublisherSettings publisher)
 {
     publishers.Add(publisher);
 }
예제 #3
0
        /// <summary>
        /// Private static helper method to publish the exception information to a custom publisher.
        /// </summary>
        /// <param name="exception">The exception object whose information should be published.</param>
        /// <param name="additionalInfo">A collection of additional data that should be published along with the exception information.</param>
        /// <param name="publisher">The PublisherSettings that contains the values of the publishers configuration.</param>
        private static void PublishToCustomPublisher(Exception exception, NameValueCollection additionalInfo, PublisherSettings publisher)
        {
            try
            {
                // Check if the exception format is "xml".
                if (publisher.ExceptionFormat == PublisherFormat.Xml)
                {
                    // If it is load the IExceptionXmlPublisher interface on the custom publisher.

                    // Instantiate the class
                    IExceptionXmlPublisher XMLPublisher = (IExceptionXmlPublisher)Activate(publisher.AssemblyName, publisher.TypeName);

                    // Publish the exception and any additional information
                    XMLPublisher.Publish(SerializeToXml(exception, additionalInfo),publisher.OtherAttributes);
                }
                    // Otherwise load the IExceptionPublisher interface on the custom publisher.
                else
                {
                    // Instantiate the class
                    IExceptionPublisher Publisher = (IExceptionPublisher)Activate(publisher.AssemblyName, publisher.TypeName);

                    // Publish the exception and any additional information
                    Publisher.Publish(exception, additionalInfo, publisher.OtherAttributes);
                }
            }
            catch(Exception e)
            {
                CustomPublisherException publisherException = new CustomPublisherException(resourceManager.GetString("RES_CUSTOM_PUBLISHER_FAILURE_MESSAGE"), publisher.AssemblyName, publisher.TypeName, publisher.ExceptionFormat, e);
                publisherException.AdditionalInformation.Add(publisher.OtherAttributes);

                throw(publisherException);
            }
        }