Beispiel #1
0
        /// <summary>
        /// Builds the ExceptionSettings 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 ExceptionSettings struct built from the configuration settings.</returns>
        public object Create(object parent, object configContext, XmlNode section)
        {
            try
            {
                ExceptionSettings settings = new ExceptionSettings();

                // 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 = ExceptionMode.Off;
                }

                #region Loop through the publisher components and load them into the ExceptionSettings
                // Loop through the publisher components and load them into the ExceptionSettings.
                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 ExceptionSettings loaded with the values from the config file.
                return(settings);
            }
            catch (Exception exc)
            {
                throw new ConfigurationException(resourceManager.GetString("RES_EXCEPTION_LOADING_CONFIGURATION"), exc, section);
            }
        }
        /// <summary>
        /// Static method to publish the exception information and any additional information.
        /// </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>
        public static void Publish(Exception exception, NameValueCollection additionalInfo)
        {
            try
            {
                #region Load the AdditionalInformation Collection with environment data.
                // Create the Additional Information collection if it does not exist.
                if (null == additionalInfo)
                {
                    additionalInfo = new NameValueCollection();
                }

                // Add environment information to the information collection.
                try
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".MachineName", Environment.MachineName);
                }
                catch (SecurityException)
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".MachineName", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED"));
                }
                catch
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".MachineName", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION"));
                }

                try
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".TimeStamp", DateTime.Now.ToString());
                }
                catch (SecurityException)
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".TimeStamp", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED"));
                }
                catch
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".TimeStamp", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION"));
                }

                try
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".FullName", Assembly.GetExecutingAssembly().FullName);
                }
                catch (SecurityException)
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".FullName", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED"));
                }
                catch
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".FullName", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION"));
                }

                try
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".AppDomainName", AppDomain.CurrentDomain.FriendlyName);
                }
                catch (SecurityException)
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".AppDomainName", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED"));
                }
                catch
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".AppDomainName", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION"));
                }

                try
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".ThreadIdentity", Thread.CurrentPrincipal.Identity.Name);
                }
                catch (SecurityException)
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".ThreadIdentity", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED"));
                }
                catch
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".ThreadIdentity", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION"));
                }

                try
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".WindowsIdentity", WindowsIdentity.GetCurrent().Name);
                }
                catch (SecurityException)
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".WindowsIdentity", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_PERMISSION_DENIED"));
                }
                catch
                {
                    additionalInfo.Add(EXCEPTIONMANAGER_NAME + ".WindowsIdentity", resourceManager.GetString("RES_EXCEPTIONMANAGEMENT_INFOACCESS_EXCEPTION"));
                }

                #endregion

                #region Publish the exception based on Configuration Settings
                // Check for any settings in config file.

                if (ConfigurationSettings.GetConfig(EXCEPTIONMANAGEMENT_CONFIG_SECTION) == null)
                {
                    // Publish the exception and additional information to the default publisher if no settings are present.
                    PublishToDefaultPublisher(exception, additionalInfo);
                }
                else
                {
                    // Get settings from config file
                    ExceptionSettings config = (ExceptionSettings)ConfigurationSettings.GetConfig(EXCEPTIONMANAGEMENT_CONFIG_SECTION);

                    // If the mode is not equal to "off" call the Publishers, otherwise do nothing.
                    if (config.Mode == ExceptionMode.On)
                    {
                        // If no publishers are specified, use the default publisher.
                        if (config.Publishers == null || config.Publishers.Count == 0)
                        {
                            // Publish the exception and additional information to the default publisher if no mode is specified.
                            PublishToDefaultPublisher(exception, additionalInfo);
                        }
                        else
                        {
                            #region Iterate through the publishers
                            // Loop through the publisher components specified in the config file.
                            foreach (PublisherSettings Publisher in config.Publishers)
                            {
                                // Call the Publisher component specified in the config file.
                                try
                                {
                                    // Verify the publishers mode is not set to "OFF".
                                    // This publisher will be called even if the mode is not specified.
                                    // The mode must explicitly be set to OFF to not be called.
                                    if (Publisher.Mode == PublisherMode.On)
                                    {
                                        if (exception == null || !Publisher.IsExceptionFiltered(exception.GetType()))
                                        {
                                            // Publish the exception and any additional information
                                            PublishToCustomPublisher(exception, additionalInfo, Publisher);
                                        }
                                    }
                                }
                                // Catches any failure to call a custom publisher.
                                catch (Exception e)
                                {
                                    // Publish the exception thrown when trying to call the custom publisher.
                                    PublishInternalException(e, null);

                                    // Publish the original exception and additional information to the default publisher.
                                    PublishToDefaultPublisher(exception, additionalInfo);
                                } // End Catch block.
                            }     // End foreach loop through publishers.
                            #endregion
                        }         // End else statement when custom publishers are in the config settings.
                    }             // End else statement where config settings are not set to "OFF"
                }                 // End else statement when config settings are provided.
                #endregion
            }
            catch (Exception e)
            {
                // Publish the exception thrown within the ExceptionManager.
                PublishInternalException(e, null);

                // Publish the original exception and additional information to the default publisher.
                PublishToDefaultPublisher(exception, additionalInfo);
            }
        }         // End Publish(Exception exception, NameValueCollection AdditionalInfo)