Example #1
0
        public void CanChangeTraceLevel()
        {
            var loggerResult = new LoggerEventArgs();
            var testClass = new MockLoggingClass();
            var logger = testClass.GetLogger();

            testClass.LoggerEvent += (sender, args) =>
            {
                loggerResult = args;
            };

            const string expectedMessage = "- Hello World";

            // Default priority for LogVerbose is low - we'll change to high
            testClass.LogMessage(logger.LogVerbose,
                "Hello {0}", "World", Priority.High);
            Assert.AreEqual(Category.Debug, loggerResult.Category);
            Assert.AreEqual(Priority.High, loggerResult.Priority);
            Assert.AreEqual(expectedMessage, loggerResult.Message);
            Assert.IsFalse(loggerResult.IsLoggingPermitted);

            // Set trace level to verbose
            var appSettings = testClass.GetAppSettings();
            appSettings.TraceLevel = (int)GwnTraceLevel.Verbose;

            testClass.LogMessage(logger.LogVerbose, expectedMessage);
            Assert.IsTrue(loggerResult.IsLoggingPermitted);

            testClass.LogMessage(logger.LogInfo, expectedMessage);
            Assert.IsTrue(loggerResult.IsLoggingPermitted);

            testClass.LogMessage(logger.LogWarning, expectedMessage);
            Assert.IsTrue(loggerResult.IsLoggingPermitted);

            testClass.LogMessage(logger.LogError, expectedMessage);
            Assert.IsTrue(loggerResult.IsLoggingPermitted);

            // Set trace level to Info (level 3)
            appSettings.TraceLevel = (int)GwnTraceLevel.Info;

            // Verbose should be false (level 4)
            testClass.LogMessage(logger.LogVerbose, expectedMessage);
            Assert.IsFalse(loggerResult.IsLoggingPermitted);

            // Info should be true (level 3)
            testClass.LogMessage(logger.LogInfo, expectedMessage);
            Assert.IsTrue(loggerResult.IsLoggingPermitted);
        }
Example #2
0
        public void CanChangeDefaultPriority()
        {
            var loggerResult = new LoggerEventArgs();
            var testClass = new MockLoggingClass();
            var logger = testClass.GetLogger();

            testClass.LoggerEvent += (sender, args) =>
            {
                loggerResult = args;
            };

            // Default priority for LogVerbose is low - we'll change to high
            testClass.LogMessage(logger.LogVerbose,
                "Hello {0}", "World", Priority.High);
            Assert.AreEqual(Category.Debug, loggerResult.Category);
            Assert.AreEqual(Priority.High, loggerResult.Priority);
            Assert.AreEqual("- Hello World", loggerResult.Message);
            Assert.IsFalse(loggerResult.IsLoggingPermitted);
        }
Example #3
0
        /// <summary>
        /// Logs the specified message using standard Prism logger 
        /// (ILoggerFacade).  All of the other logging methods, which
        /// implement IGwnLogger, call this method - it ia a single
        /// entry point.   
        /// <br></br><br></br>
        /// The <see cref="GwnSwitch"/> is used to determine if logging
        /// is enabled for the specified category (debug, info, warn, error)
        /// based on the AppSettings.TraceLevel value.   If enabled the
        /// virtual method LogTheMessage(sender, LoggerEventArgs) is called
        /// so the message can be logged.   When logged the LoggerEvent
        /// will be published via the event aggregator.
        /// <br></br> <br></br>
        /// The LoggerEvent of this class, unlike the IOC Logger event (which
        /// uses the event aggregator), *always* publishes the logging contents 
        /// (mainly for TDD and display in diagnostic status controls). The 
        /// IsLoggingPermitted property of the logger event args will indicate 
        /// whether the logged entry was loggable by the <see cref="GwnSwitch"/>
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="category">The category.</param>
        /// <param name="priority">The priority.</param>
        public void Log(string message, Category category, Priority priority)
        {
            // Determine if the trace level will allow logging
            var isLoggingPermitted = Switch
                .TraceLevelAllowsLogging(category, priority);

            // Create event argument for logging and event
            var args = new LoggerEventArgs
            {
                Message = message,
                Category = category,
                Priority = priority,
                IsLoggingPermitted = isLoggingPermitted
            };

            // If permitted log the message
            if (isLoggingPermitted)
                LogTheMessage(this, args);

            // Raise event that message was requested (even if not logged)
            OnLoggerEvent(args);
        }
Example #4
0
 /// <summary>
 /// Handles the <see cref="E:LoggerEvent" /> event.
 /// </summary>
 /// <param name="e">The 
 /// <see cref="Gwn.Library.Common.Events.LoggerEventArgs" /> 
 /// instance containing the event data.</param>
 protected virtual void OnLoggerEvent(LoggerEventArgs e)
 {
     LoggerEvent?.Invoke(this, e);
 }
Example #5
0
 /// <summary>
 /// Logs the message using default logging behavior for category,
 /// priority, and trace level.  Intended to be overridden so that 
 /// derived classes can use the standard business logic, i.e., for 
 /// tracelevel, while being able to alter the output destination of
 /// the logged message.  By default this method logs to Debug.Writeline()
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="LoggerEventArgs"/> instance 
 /// containing the event data.</param>
 protected virtual void LogTheMessage(object sender, LoggerEventArgs e)
 {
     Debug.WriteLine(e.Message);            
 }
Example #6
0
        public void CanUpdateLoggingWithClassNamePrefix()
        {
            var loggerResult = new LoggerEventArgs();
            var testClass = new MockLoggingClass();
            var logger = testClass.GetLogger();

            testClass.LoggerEvent += (sender, args) =>
            {
                loggerResult = args;
            };
            const string expectedMessage =
                "- GwnLoggerFixture.MethodName: Hello World 100";
            //   ^^^^^^^^^^^^^^^^
            // Default priority for LogVerbose is low - we'll change to high
            // by placing Priority.High in the parameter list.  We'll also send
            // "this" which will cause the class name to prefix the logging result
            testClass.LogMessage(logger.LogVerbose,
                "MethodName: Hello {0} {1}", "World",
                100, Priority.High, this);

            Assert.AreEqual(Category.Debug, loggerResult.Category);
            Assert.AreEqual(Priority.High, loggerResult.Priority);
            Assert.AreEqual(expectedMessage, loggerResult.Message);
        }
Example #7
0
        public void CanOverridePriority()
        {
            var loggerResult = new LoggerEventArgs();
            var testClass = new MockLoggingClass();
            var logger = testClass.GetLogger();

            testClass.LoggerEvent += (sender, args) =>
            {
                loggerResult = args;
            };

            testClass.LogMessage(logger.LogError, "Hello {0}","World");
            Assert.AreEqual(Category.Exception, loggerResult.Category);
            Assert.AreEqual(Priority.High, loggerResult.Priority);
            Assert.AreEqual("X Hello World", loggerResult.Message);
            Assert.IsFalse(loggerResult.IsLoggingPermitted);

            testClass.LogMessage(logger.LogWarning, "TraceLevel = {0}", 2);
            Assert.AreEqual(Category.Warn, loggerResult.Category);
            Assert.AreEqual(Priority.Medium, loggerResult.Priority);
            Assert.AreEqual("! TraceLevel = 2", loggerResult.Message);
            Assert.IsFalse(loggerResult.IsLoggingPermitted);

            testClass.LogMessage(logger.LogInfo, "{0} Information", "Good");
            Assert.AreEqual(Category.Info, loggerResult.Category);
            Assert.AreEqual(Priority.Low, loggerResult.Priority);
            Assert.AreEqual("+ Good Information", loggerResult.Message);
            Assert.IsFalse(loggerResult.IsLoggingPermitted);

            testClass.LogMessage(logger.LogVerbose, "To much {0}", "Information");
            Assert.AreEqual(Category.Debug, loggerResult.Category);
            Assert.AreEqual(Priority.Low, loggerResult.Priority);
            Assert.AreEqual("- To much Information", loggerResult.Message);
            Assert.IsFalse(loggerResult.IsLoggingPermitted);
        }