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)); }
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); }
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); }