Exemple #1
0
        private static LoggingEvent[] LogInstanceMethodHelper(ILog loggingInstance, string debugMessage, Exception exceptionMessage = null)
        {
            //if (!LogManager.GetRepository().Configured)
            //    BasicConfigurator.Configure();  //If you need to know how to configure a repository

            //Use hierarchy to to get Repository and then get Root to access IAppenderAttachable
            var hierarchy = (Hierarchy)LogManager.GetRepository();
            //Remove all appenders and setup a memory appender so that we don't write to the database
            var attachable = hierarchy.Root as IAppenderAttachable;

            //if (attachable == null) return ("IappenderAttachable is null");
            attachable.RemoveAllAppenders();
            var appender = new MemoryAppender();

            attachable.AddAppender(appender);
            if (exceptionMessage == null)
            {
                EntLogger.LogDebug(loggingInstance, debugMessage);
            }
            else
            {
                EntLogger.LogException(loggingInstance, debugMessage, exceptionMessage);
            }

            var loggingEvents = appender.GetEvents();

            //Cleanup
            attachable.RemoveAppender(appender);
            return(loggingEvents);
        }
        static void Main(string[] args)
        {
            //Initialize Logger at startup
            var loggingInstance = EntLogger.Initialize();
            var logger          = new LoggerTest();

            logger.Logit(loggingInstance);
        }
Exemple #3
0
        public void VerifyLogDebugInvokedOnce()
        {
            // Arrange
            var moqEntLogger = new Mock <ILog>();

            // Act
            EntLogger.LogDebug(moqEntLogger.Object, "MockDebugMessage");

            // Assert
            moqEntLogger.Verify(log => log.Debug("MockDebugMessage"), Times.Once());
        }
Exemple #4
0
        public void TestLoggerApplicationNameIsSetAfterInitialize()
        {
            // Arrange
            var loggingInstance = EntLogger.Initialize();

            // Act
            var appName = GlobalContext.Properties["ApplicationName"];

            // Assert
            Assert.AreEqual("EnterpriseLogger.EntLogger", appName);

            //Display
            TestContextInstance.WriteLine("ApplicationName = " + appName);
        }
Exemple #5
0
        public void TestLoggerNameIsSetAfterInitialize()
        {
            // Arrange
            var loggingInstance = EntLogger.Initialize();

            // Act
            var name = loggingInstance.Logger.Name;

            // Assert
            Assert.AreEqual("EnterpriseLogger.EntLogger", name);

            //Display
            TestContextInstance.WriteLine("Logger.Name = " + name);
        }
            //Note that loggInstance is injected as a dependency of LoggingFromChildClass
            public void LoggingFromChildClass(ILog loggingInstance)
            {
                EntLogger.LogDebug(loggingInstance, "Entering LoggingFromChildClass");
                try
                {
                    throw new Exception("Test Message Text", new Exception("ChildClass Exception"));
                }
                catch (Exception ex)
                {
                    EntLogger.LogException(loggingInstance, ex.Message, ex);
                }

                EntLogger.LogDebug(loggingInstance, "Exiting LoggingFromChildClass");
            }
Exemple #7
0
        public void TestLoggerCorrelationIdIsSetAFterInitialize()
        {
            // Arrange
            var loggingInstance = EntLogger.Initialize();

            // Act
            var corrId = GlobalContext.Properties["CorrelationId"];

            // Assert
            Assert.IsNotNull(corrId);
            Assert.IsTrue(corrId.GetType() == typeof(Guid));

            //Display
            TestContextInstance.WriteLine("CorrelationId = " + corrId);
        }
            //Note that loggInstance is injected as a dependency of Logit
            public void Logit(ILog loggingInstance)
            {
                EntLogger.LogDebug(loggingInstance, "Entering LogIt");
                try
                {
                    throw new Exception("Test Message Text", new Exception("Inner Exception Text"));
                }
                catch (Exception ex)
                {
                    EntLogger.LogException(loggingInstance, ex.Message, ex);
                }

                EntLogger.LogDebug(loggingInstance, "Exiting LogIt");

                ChildClass childClass = new ChildClass();

                childClass.LoggingFromChildClass(loggingInstance);
            }
Exemple #9
0
        public void LogDebugGeneratesLoggingEvent()
        {
            //Helper class removes configured appender and replaces it with a memory appender
            //Allows unit test to generate and test that a log4net message was created (in memory)

            // Arrange
            var loggingInstance = EntLogger.Initialize();

            // Act
            var loggingEvents = LogInstanceMethodHelper(loggingInstance, "Test LogDebug Method");

            // Assert
            if (loggingEvents == null)
            {
                Assert.Fail("LoggingEvents is null");
            }
            foreach (var loggingEvent in loggingEvents)
            {
                Assert.AreEqual("Test LogDebug Method", loggingEvent.RenderedMessage);

                //Display
                TestContextInstance.WriteLine("RenderedMessage = " + loggingEvent.RenderedMessage);
            }
        }