/// <summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// Builds an <see cref="LoggingExceptionHandler"/> based on an instance of <see cref="LoggingExceptionHandlerData"/>.
        /// </summary>
        /// <seealso cref="ExceptionHandlerCustomFactory"/>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="LoggingExceptionHandlerData"/>.</param>
        /// <param name="configurationSource">The source for configuration objects.</param>
        /// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
        /// <returns>A fully initialized instance of <see cref="LoggingExceptionHandler"/>.</returns>
        public IExceptionHandler Assemble(IBuilderContext context,
                                          ExceptionHandlerData objectConfiguration,
                                          IConfigurationSource configurationSource,
                                          ConfigurationReflectionCache reflectionCache)
        {
            LoggingExceptionHandlerData castedObjectConfiguration
                = (LoggingExceptionHandlerData)objectConfiguration;

            LogWriter writer;

            if (castedObjectConfiguration.UseDefaultLogger)
            {
                writer = Logger.Writer;
            }
            else
            {
                IBuilderContext logWriterContext = context.CloneForNewBuild(NamedTypeBuildKey.Make <LogWriter>(), null);
                writer = (LogWriter)logWriterContext.Strategies.ExecuteBuildUp(logWriterContext);
            }

            LoggingExceptionHandler createdObject
                = new LoggingExceptionHandler(
                      castedObjectConfiguration.LogCategory,
                      castedObjectConfiguration.EventId,
                      castedObjectConfiguration.Severity,
                      castedObjectConfiguration.Title,
                      castedObjectConfiguration.Priority,
                      castedObjectConfiguration.FormatterType,
                      writer);

            return(createdObject);
        }
        public void CanUseLoggingHandlerWithXmlExceptionFormatter()
        {
            MockTraceListener.Reset();

            Exception exception = new Exception("test exception");
            Guid      testGuid  = Guid.NewGuid();

            LoggingExceptionHandler handler
                = new LoggingExceptionHandler(
                      "TestCat",
                      0,
                      TraceEventType.Warning,
                      "test",
                      10,
                      typeof(XmlExceptionFormatter),
                      Logger.Writer);

            handler.HandleException(exception, testGuid);

            Assert.AreEqual(1, MockTraceListener.Entries.Count);
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(MockTraceListener.Entries[0].Message);
            XmlNode element = doc.DocumentElement.SelectSingleNode("/Exception/@handlingInstanceId");

            Assert.IsNotNull(element);
            Assert.AreEqual(testGuid.ToString("D", CultureInfo.InvariantCulture), element.InnerText);
        }
        public void CreatesLoggingExceptionHandlerWithProgrammaticConfiguration()
        {
            var loggingConfiguration  = new LoggingConfiguration();
            var eventLog              = new EventLog("Application", ".", "ExceptionHandling.Bvt.Tests - Programmatic");
            var eventLogTraceListener = new FormattedEventLogTraceListener(eventLog, new TextFormatter("{message}"));

            loggingConfiguration.AddLogSource("Sample Category", SourceLevels.All, true).AddTraceListener(eventLogTraceListener);
            var logWriter = new LogWriter(loggingConfiguration);

            var excepionPolicies      = new List <ExceptionPolicyDefinition>();
            var excepionPolicyEntries = new List <ExceptionPolicyEntry>();
            var exceptionHandler      = new LoggingExceptionHandler("Sample Category", 100, TraceEventType.Transfer, "Sample Title", 4, typeof(TextExceptionFormatter), logWriter);
            var exceptionPolicy       = new ExceptionPolicyEntry(typeof(DivideByZeroException), PostHandlingAction.None, new IExceptionHandler[] { exceptionHandler });

            excepionPolicyEntries.Add(exceptionPolicy);
            excepionPolicies.Add(new ExceptionPolicyDefinition("Logging Policy", excepionPolicyEntries));

            var exceptionManager = new ExceptionManager(excepionPolicies);
            var exceptionTothrow = new DivideByZeroException("error message");

            exceptionManager.HandleException(exceptionTothrow, "Logging Policy");
            var entry = EventLogEntries.Last;

            Assert.IsTrue(entry.Message.Contains("Type : " + exceptionTothrow.GetType().FullName));
            Assert.IsTrue(entry.Message.Contains("Message : error message"));
        }
Exemple #4
0
        /// <summary>
        /// Logs exceptions encountered by the ClearView application.
        /// </summary>
        /// <param name="exception"></param>
        protected string LogException(Exception exception)
        {
            // Add information about the current web request
            AddExceptionInformation(exception);

            // Create GUID for this exception (for tracking purposes)
            System.Guid exceptionGUID = System.Guid.NewGuid();

            // Log error
            LoggingExceptionHandler loggingExceptionHandler = new LoggingExceptionHandler();

            loggingExceptionHandler.HandleException(exception, exceptionGUID);

            // Email error
            string    dsn            = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["DSN"]].ConnectionString;
            int       intEnvironment = Int32.Parse(ConfigurationManager.AppSettings["Environment"]);
            Variables variables      = new Variables(intEnvironment);
            Functions oFunction      = new Functions(0, dsn, intEnvironment);
            string    strEMailIdsTO  = oFunction.GetGetEmailAlertsEmailIds("EMAILGRP_DEVELOPER_ERROR");
            MessageExceptionHandler messageExceptionHandler = new MessageExceptionHandler(variables.FromEmail(), strEMailIdsTO, variables.SmtpServer());

            messageExceptionHandler.HandleException(exception, exceptionGUID);
            if (intEnvironment == (int)CurrentEnvironment.CORPDMN || intEnvironment == (int)CurrentEnvironment.PNCNT_PROD)
            {
                messageExceptionHandler.Email();
            }
            return(messageExceptionHandler.Message);
        }
Exemple #5
0
        public static void PrintException(Exception e, string exMsg)
        {
            LoggingExceptionHandler h = ExceptionReceivers;

            if (h != null)
            {
                h(e, exMsg);
            }
        }
            protected override void Act()
            {
                var data =
                    new LoggingExceptionHandlerData
                {
                    EventId           = 100,
                    FormatterTypeName = typeof(TestFormatter).AssemblyQualifiedName,
                    LogCategory       = "foo",
                    Priority          = 200,
                    Severity          = TraceEventType.Error
                };

                this.handler = (LoggingExceptionHandler)data.BuildExceptionHandler();
            }
Exemple #7
0
        /// <summary>
        /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// Builds an <see cref="LoggingExceptionHandler"/> based on an instance of <see cref="LoggingExceptionHandlerData"/>.
        /// </summary>
        /// <seealso cref="ExceptionHandlerCustomFactory"/>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="LoggingExceptionHandlerData"/>.</param>
        /// <param name="configurationSource">The source for configuration objects.</param>
        /// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
        /// <returns>A fully initialized instance of <see cref="LoggingExceptionHandler"/>.</returns>
        public IExceptionHandler Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
        {
            LoggingExceptionHandlerData castedObjectConfiguration
                = (LoggingExceptionHandlerData)objectConfiguration;

            LogWriter writer
                = (LogWriter)context.HeadOfChain.BuildUp(context, typeof(LogWriter), null, null);

            LoggingExceptionHandler createdObject
                = new LoggingExceptionHandler(
                      castedObjectConfiguration.LogCategory,
                      castedObjectConfiguration.EventId,
                      castedObjectConfiguration.Severity,
                      castedObjectConfiguration.Title,
                      castedObjectConfiguration.Priority,
                      castedObjectConfiguration.FormatterType,
                      writer);

            return(createdObject);
        }
        public void LoggingHandlerCreatesFormatterWithTheDocumentedConstructor()
        {
            MockTraceListener.Reset();
            ConstructorSensingExceptionFormatter.Instances.Clear();
            Exception exception = new Exception("test exception");
            Guid      testGuid  = Guid.NewGuid();
            LoggingExceptionHandler handler
                = new LoggingExceptionHandler(
                      "TestCat",
                      0,
                      TraceEventType.Warning,
                      "test",
                      0,
                      typeof(ConstructorSensingExceptionFormatter),
                      Logger.Writer);

            handler.HandleException(exception, testGuid);
            Assert.AreEqual(1, ConstructorSensingExceptionFormatter.Instances.Count);
            Assert.AreSame(exception, ConstructorSensingExceptionFormatter.Instances[0].Exception);
            Assert.AreEqual(testGuid, ConstructorSensingExceptionFormatter.Instances[0].HandlingInstanceId);
        }
            protected override void Act()
            {
                var data =
                    new LoggingExceptionHandlerData
                    {
                        EventId = 100,
                        FormatterTypeName = typeof(TestFormatter).AssemblyQualifiedName,
                        LogCategory = "foo",
                        Priority = 200,
                        Severity = TraceEventType.Error
                    };

                this.handler = (LoggingExceptionHandler)data.BuildExceptionHandler();
            }
        public void CanUseLoggingHandlerWithXmlExceptionFormatter()
        {
            MockTraceListener.Reset();

            Exception exception = new Exception("test exception");
            Guid testGuid = Guid.NewGuid();

            LoggingExceptionHandler handler
                = new LoggingExceptionHandler(
                    "TestCat",
                    0,
                    TraceEventType.Warning,
                    "test",
                    10,
                    typeof(XmlExceptionFormatter),
                    Logger.Writer);

            handler.HandleException(exception, testGuid);

            Assert.AreEqual(1, MockTraceListener.Entries.Count);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(MockTraceListener.Entries[0].Message);
            XmlNode element = doc.DocumentElement.SelectSingleNode("/Exception/@handlingInstanceId");
            Assert.IsNotNull(element);
            Assert.AreEqual(testGuid.ToString("D", CultureInfo.InvariantCulture), element.InnerText);
        }
        public void LoggingHandlerCreatesFormatterWithTheDocumentedConstructor()
        {
            MockTraceListener.Reset();
            ConstructorSensingExceptionFormatter.Instances.Clear();

            Exception exception = new Exception("test exception");
            Guid testGuid = Guid.NewGuid();

            LoggingExceptionHandler handler
                = new LoggingExceptionHandler(
                    "TestCat",
                    0,
                    TraceEventType.Warning,
                    "test",
                    0,
                    typeof(ConstructorSensingExceptionFormatter),
                    Logger.Writer);

            handler.HandleException(exception, testGuid);

            Assert.AreEqual(1, ConstructorSensingExceptionFormatter.Instances.Count);
            Assert.AreSame(exception, ConstructorSensingExceptionFormatter.Instances[0].Exception);
            Assert.AreEqual(testGuid, ConstructorSensingExceptionFormatter.Instances[0].HandlingInstanceId);
        }