public static void SetLoggerConfigurationManually() { var config = new NLog.Config.LoggingConfiguration(); // Создадим Targets для async обёртки var logRequests = new NLog.Targets.FileTarget("logRequests"); logRequests.FileName = "${basedir}/logs/httpRequests.txt"; logRequests.ArchiveFileName = "${basedir}/logs/archive/httpRequests.{####}.txt"; logRequests.ArchiveAboveSize = 10000240; var logTroubles = new NLog.Targets.FileTarget("logTroubles"); logTroubles.FileName = "${basedir}/logs/troubles.txt"; logTroubles.ArchiveFileName = "${basedir}/logs/archive/troubles.{####}.txt"; logTroubles.ArchiveAboveSize = 10240; var logConsole = new NLog.Targets.ColoredConsoleTarget("logconsole"); logConsole.UseDefaultRowHighlightingRules = true; var logDirectoryEvents = new NLog.Targets.FileTarget("logDirectoryEvents"); logTroubles.FileName = "${basedir}/logs/directoryEvents.txt"; logTroubles.ArchiveFileName = "${basedir}/logs/archive/directoryEvents.{####}.txt"; logTroubles.ArchiveAboveSize = 10240; // Создадим обёртки для асинхронных записей var asyncTargetWrapper1 = new NLog.Targets.Wrappers.AsyncTargetWrapper(logRequests); var asyncTargetWrapper2 = new NLog.Targets.Wrappers.AsyncTargetWrapper(logTroubles); var asyncTargetWrapper3 = new NLog.Targets.Wrappers.AsyncTargetWrapper(logConsole); var asyncTargetWrapper4 = new NLog.Targets.Wrappers.AsyncTargetWrapper(logDirectoryEvents); // Создадим sync targets var logBigTroubles = new NLog.Targets.FileTarget("logBigTroubles"); logBigTroubles.FileName = "${basedir}/logs/bigTroubles.txt"; logBigTroubles.ArchiveFileName = "${basedir}/logs/archive/bigTroubles.{####}.txt"; logBigTroubles.ArchiveAboveSize = 10240; // Зададим Rules config.AddRuleForOneLevel(LogLevel.Error, logBigTroubles); config.AddRuleForOneLevel(LogLevel.Warn, logTroubles); config.AddRuleForOneLevel(LogLevel.Info, logRequests); config.AddRuleForOneLevel(LogLevel.Debug, logConsole); config.AddRuleForOneLevel(LogLevel.Trace, logDirectoryEvents); // Применим Configuration NLog.LogManager.Configuration = config; }
public void CheckAllTargets() { var config = new LoggingConfiguration(); var fileTarget = new FileTarget {Name = "File", FileName = "file"}; config.AddRuleForOneLevel(LogLevel.Error, fileTarget, "*a"); config.AddTarget(fileTarget); Assert.Equal(1, config.AllTargets.Count); Assert.Equal(fileTarget, config.AllTargets[0]); config.InitializeAll(); Assert.Equal(1, config.AllTargets.Count); Assert.Equal(fileTarget, config.AllTargets[0]); }
public void AddRule_missingtarget() { var config = new LoggingConfiguration(); Assert.Throws<NLogConfigurationException>(() => config.AddRuleForOneLevel(LogLevel.Error, "File", "*a")); }
public void AddRule_with_target() { var config = new LoggingConfiguration(); var fileTarget = new FileTarget {Name = "File"}; config.AddRuleForOneLevel(LogLevel.Error, fileTarget, "*a"); Assert.NotNull(config.LoggingRules); Assert.Equal(1, config.LoggingRules.Count); config.AddTarget(new FileTarget {Name = "File"}); var allTargets = config.AllTargets; Assert.NotNull(allTargets); Assert.Equal(1, allTargets.Count); Assert.Equal("File", allTargets.First().Name); Assert.NotNull(config.FindTargetByName<FileTarget>("File")); }
public void AddRule_onelevel() { var config = new LoggingConfiguration(); config.AddTarget(new FileTarget {Name = "File"}); config.AddRuleForOneLevel(LogLevel.Error, "File", "*a"); Assert.NotNull(config.LoggingRules); Assert.Equal(1, config.LoggingRules.Count); var rule1 = config.LoggingRules.FirstOrDefault(); Assert.NotNull(rule1); Assert.Equal(false, rule1.Final); Assert.Equal("*a", rule1.LoggerNamePattern); Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Fatal)); Assert.Equal(true, rule1.IsLoggingEnabledForLevel(LogLevel.Error)); Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Warn)); Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Info)); Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Debug)); Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Trace)); Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Off)); }