Exemplo n.º 1
0
        public void SingleParameterException_OutputsSingleStackTrace()
        {
            // Arrange
            var logFactory = new LogFactory();
            var logConfig  = new LoggingConfiguration(logFactory);
            var logTarget  = new NLog.Targets.DebugTarget("debug")
            {
                Layout = "${message}|${exception}"
            };

            logConfig.AddRuleForAllLevels(logTarget);
            logFactory.Configuration = logConfig;
            var logger = logFactory.GetLogger("SingleParameterException");

            // Act
            try
            {
                logger.Info("Hello");
                throw new ArgumentException("Holy Moly");
            }
            catch (Exception ex)
            {
                logger.Fatal(ex);
            }

            // Assert
            Assert.StartsWith("System.ArgumentException: Holy Moly|System.ArgumentException", logTarget.LastMessage);
        }
Exemplo n.º 2
0
        public void AssertDebugLastMessage(string targetName, string msg)
        {
            NLog.Targets.DebugTarget debugTarget = (NLog.Targets.DebugTarget)LogManager.Configuration.FindTargetByName(targetName);

            Assert.NotNull(debugTarget);
            Assert.Equal(msg, debugTarget.LastMessage);
        }
Exemplo n.º 3
0
        public void TestReceivedLogEventContextTest()
        {
            using (var loggerScope = new InternalLoggerScope())
            {
                // Arrange
                var targetContext = new NLog.Targets.DebugTarget()
                {
                    Name = "Ugly"
                };
                var receivedArgs = new List <InternalLoggerMessageEventArgs>();
                InternalLogger.LogMessageReceived += (sender, e) =>
                {
                    receivedArgs.Add(e);
                };
                var exception = new Exception();

                // Act
                NLog.Internal.ExceptionHelper.MustBeRethrown(exception, targetContext);

                // Assert
                Assert.Single(receivedArgs);
                var logEventArgs = receivedArgs.Single();
                Assert.Equal(LogLevel.Error, logEventArgs.Level);
                Assert.Equal(exception, logEventArgs.Exception);
                Assert.Equal(targetContext.GetType(), logEventArgs.SenderType);
            }
        }
Exemplo n.º 4
0
        public void AssertDebugCounter(string targetName, int val)
        {
            NLog.Targets.DebugTarget debugTarget = (NLog.Targets.DebugTarget)LogManager.Configuration.FindTargetByName(targetName);

            Assert.IsNotNull(debugTarget, "Debug target '" + targetName + "' not found");
            Assert.AreEqual(val, debugTarget.Counter, "Unexpected counter value on '" + targetName + "'");
        }
 public void CaptureSingleParameterExceptionTest()
 {
     try
     {
         NLog.LogManager.ThrowExceptions = true; // Only use this for unit-tests
         var target = new NLog.Targets.DebugTarget()
         {
             Layout = "${exception:format=message}"
         };
         NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
         Logging.LogManager.LogFactory = new NLogger.NLogFactory();
         var log = Logging.LogManager.LogFactory.GetLogger(GetType());
         log.Debug(new ApplicationException("Debug"));
         Assert.AreEqual("Debug", target.LastMessage);
         log.Info(new ApplicationException("Info"));
         Assert.AreEqual("Info", target.LastMessage);
         log.Warn(new ApplicationException("Warn"));
         Assert.AreEqual("Warn", target.LastMessage);
         log.Error(new ApplicationException("Error"));
         Assert.AreEqual("Error", target.LastMessage);
         log.Fatal(new ApplicationException("Fatal"));
         Assert.AreEqual("Fatal", target.LastMessage);
     }
     finally
     {
         NLog.Common.InternalLogger.Reset();
         NLog.LogManager.Configuration = null;
     }
 }
Exemplo n.º 6
0
        public void AssertDebugLastMessageContains(string targetName, string msg)
        {
            NLog.Targets.DebugTarget debugTarget = (NLog.Targets.DebugTarget)LogManager.Configuration.FindTargetByName(targetName);

            // Console.WriteLine("lastmsg: {0}", debugTarget.LastMessage);

            Assert.NotNull(debugTarget);
            Assert.True(debugTarget.LastMessage.Contains(msg), "Unexpected last message value on '" + targetName + "'");
        }
Exemplo n.º 7
0
        public void AssertDebugLastMessage(string targetName, string msg)
        {
            NLog.Targets.DebugTarget debugTarget = (NLog.Targets.DebugTarget)LogManager.Configuration.FindTargetByName(targetName);

            // Console.WriteLine("lastmsg: {0}", debugTarget.LastMessage);

            Assert.IsNotNull(debugTarget, "Debug target '" + targetName + "' not found");
            Assert.AreEqual(msg, debugTarget.LastMessage, "Unexpected last message value on '" + targetName + "'");
        }
Exemplo n.º 8
0
        public void WhenMethodFilterApiTest()
        {
            // Stage
            var logFactory = new LogFactory();
            var logger1    = logFactory.GetLogger("Hello");
            var logger2    = logFactory.GetLogger("Goodbye");
            var config     = new LoggingConfiguration(logFactory);
            var target     = new NLog.Targets.DebugTarget()
            {
                Layout = "${message}"
            };

            config.AddRuleForAllLevels(target);
            config.LoggingRules.Last().Filters.Add(new WhenMethodFilter((l) => l.LoggerName == logger1.Name ? FilterResult.Ignore : FilterResult.Neutral));
            logFactory.Configuration = config;

            // Act 1
            logger1.Info("Hello World");
            Assert.Empty(target.LastMessage);

            // Act 2
            logger2.Info("Goodbye World");
            Assert.Equal("Goodbye World", target.LastMessage);
        }
Exemplo n.º 9
0
 public string GetDebugLastMessage(string targetName)
 {
     NLog.Targets.DebugTarget debugTarget = (NLog.Targets.DebugTarget)LogManager.Configuration.FindTargetByName(targetName);
     return(debugTarget.LastMessage);
 }