Пример #1
0
        public void WhenLogLevelTest()
        {
            var logFactory = new LogFactory().Setup().LoadConfigurationFromXml(@"
                <nlog>
                    <targets><target name='debug' type='debug' layout='${message}' /></targets>
                    <rules>
                        <logger name='*' minLevel='Debug' writeTo='debug'>
                            <filters defaultAction='ignore'>
                                <when condition=""level >= '${scopeproperty:filterlevel:whenEmpty=Off}'"" action='Log' />
                            </filters>
                        </logger>
                    </rules>
                </nlog>
            ").LogFactory;
            var logger     = logFactory.GetCurrentClassLogger();

            logger.Fatal("Hello Emptiness");
            logFactory.AssertDebugLastMessage("");

            using (logger.PushScopeProperty("filterLevel", LogLevel.Warn))
            {
                logger.Error("Hello can you hear me");
                logFactory.AssertDebugLastMessage("Hello can you hear me");
            }
        }
Пример #2
0
        public void WhenExceptionTest()
        {
            var logFactory = new LogFactory().Setup().LoadConfigurationFromXml(@"
                <nlog>
                    <targets><target name='debug' type='debug' layout='${message}' /></targets>
                    <rules>
                        <logger name='*' minLevel='Debug' writeTo='debug'>
                            <filters defaultAction='ignore'>
                                <when condition='exception != null' action='Log' />
                            </filters>
                        </logger>
                    </rules>
                </nlog>
            ").LogFactory;
            var logger     = logFactory.GetCurrentClassLogger();

            logger.Fatal("Hello missing Exception");
            logFactory.AssertDebugLastMessage("");

            logger.Error(new System.Exception("Oh no"), "Hello with Exception");
            logFactory.AssertDebugLastMessage("Hello with Exception");
        }
        public void Var_Layout_Target_CallSite()
        {
            var logFactory = new LogFactory().Setup()
                             .LoadConfigurationFromXml(@"<nlog throwExceptions='true'>
                    <variable name='myvar' value='${callsite}' />
                    <targets>
                        <target name='debug' type='Debug' layout='${var:myvar}' />
                    </targets>
                    <rules>
                        <logger name='*' minLevel='Debug' writeTo='debug' />
                    </rules>
                </nlog>").LogFactory;

            // Act
            logFactory.GetCurrentClassLogger().Info("Hello");

            // Assert
            logFactory.AssertDebugLastMessage(GetType().ToString() + "." + nameof(Var_Layout_Target_CallSite));
        }