Exemplo n.º 1
0
        private static ILoggerFactory GetNLogLoggerFactory()
        {
            LoggingConfiguration loggerConfig = new NLog.Config.LoggingConfiguration();
            FileTarget           fileTarget   = new NLog.Targets.FileTarget()
            {
                Name     = "logfile",
                FileName = "log.txt",
                Layout   = "${longdate}|${level:uppercase=true}|${logger}|${event-context:item=EventId}|${message}|${ndc}"
            };

            loggerConfig.AddTarget(fileTarget);
            loggerConfig.LoggingRules.Add(new NLog.Config.LoggingRule("*", NLog.LogLevel.Info, fileTarget));
            //NLogLoggerFactory test = new NLogLoggerFactory(new NLogLoggerProvider(new NLogProviderOptions()));
            NLogLoggerFactory   logLoggerFactory = new NLogLoggerFactory();
            NLogProviderOptions op = new NLogProviderOptions
            {
                CaptureMessageProperties = true,
                CaptureMessageTemplates  = true,
                EventIdSeparator         = "|",
                IgnoreEmptyEventId       = false,
                IncludeScopes            = true,
                //ParseMessageTemplates = true
                //ShutdownOnDispose = true
            };

            NLogLoggerProvider p = new NLogLoggerProvider(op, loggerConfig.LogFactory);

            logLoggerFactory.AddProvider(p);
            return(logLoggerFactory);
            //loggerFactory.AddNLog(new NLog.LogFactory(loggerConfig));
        }
Exemplo n.º 2
0
        public void AddProvider_StateUnderTest_ExpectedBehavior()
        {
            // Arrange
            var             unitUnderTest = new NLogLoggerFactory();
            ILoggerProvider provider      = new NLogLoggerProvider();

            var internalLogWriter = CaptureInternalLog();

            // Act
            unitUnderTest.AddProvider(provider);

            // Assert
            var internalLog = internalLogWriter.ToString();

            Assert.Contains("AddProvider will be ignored", internalLog, StringComparison.OrdinalIgnoreCase);
        }
Exemplo n.º 3
0
        public void AddProvider_StateUnderTest_ExpectedBehavior()
        {
            // Arrange
            var logFactory = new LogFactory();
            var logConfig  = new Config.LoggingConfiguration(logFactory);

            logConfig.AddRuleForAllLevels(new Targets.MemoryTarget("output"));
            logFactory.Configuration = logConfig;
            var provider      = new NLogLoggerProvider(null, logFactory);
            var loggerFactory = new NLogLoggerFactory(provider);

            // Act
            ILoggerProvider newProvider = new NLogLoggerProvider();

            loggerFactory.AddProvider(newProvider);
            loggerFactory.CreateLogger("test").LogInformation("Hello");

            // Assert
            Assert.Single(logFactory.Configuration.FindTargetByName <Targets.MemoryTarget>("output").Logs);
        }