示例#1
0
        public void ContextPropertiesILoggerTest()
        {
            var logFactory = new NLog.LogFactory();
            var logConfig  = new NLog.Config.LoggingConfiguration();
            var ilogger    = new TestLogger();
            var target     = new MicrosoftILoggerTarget(ilogger)
            {
                Layout = "${message}"
            };

            target.ContextProperties.Add(new Targets.TargetPropertyWithContext()
            {
                Name = "ThreadId", Layout = "${threadid}"
            });
            logConfig.AddRuleForAllLevels(target);
            logFactory.Configuration = logConfig;
            var logger = logFactory.GetCurrentClassLogger();

            logger.Info("Hello {Planet}", "Earth");
            Assert.Equal("Hello \"Earth\"", ilogger.LastLogMessage);
            Assert.Equal(3, ilogger.LastLogProperties.Count);
            Assert.Equal("Planet", ilogger.LastLogProperties[0].Key);
            Assert.Equal("Earth", ilogger.LastLogProperties[0].Value);
            Assert.Equal("ThreadId", ilogger.LastLogProperties[1].Key);
            Assert.Equal(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(), ilogger.LastLogProperties[1].Value);
            Assert.Equal("Hello {Planet}", ilogger.LastLogProperties[2].Value);
        }
示例#2
0
        private static (Logger, LoggerFactoryMock) CreateLoggerFactoryMock(out MicrosoftILoggerTarget target)
        {
            var logFactory        = new LogFactory();
            var logConfig         = new Config.LoggingConfiguration();
            var loggerFactoryMock = new LoggerFactoryMock();

            target = new MicrosoftILoggerTarget(loggerFactoryMock)
            {
                Layout = "${message}"
            };
            logConfig.AddRuleForAllLevels(target);
            logFactory.Configuration = logConfig;
            var logger = logFactory.GetCurrentClassLogger();

            return(logger, loggerFactoryMock);
        }