public void ConfigLogRuleWithName() { var config = new LoggingConfiguration(); var rule = new LoggingRule("hello"); config.LoggingRules.Add(rule); var ruleLookup = config.FindRuleByName("hello"); Assert.Same(rule, ruleLookup); Assert.True(config.RemoveRuleByName("hello")); ruleLookup = config.FindRuleByName("hello"); Assert.Null(ruleLookup); Assert.False(config.RemoveRuleByName("hello")); }
public void FindRuleByName_AfterRename_FindNewOneAndDontFindOld() { // Arrange var config = new LoggingConfiguration(); var rule = new LoggingRule("hello"); config.LoggingRules.Add(rule); // Act var foundRule1 = config.FindRuleByName("hello"); foundRule1.RuleName = "world"; var foundRule2 = config.FindRuleByName("hello"); var foundRule3 = config.FindRuleByName("world"); // Assert Assert.Null(foundRule2); Assert.NotNull(foundRule1); Assert.Same(foundRule1, foundRule3); }
private void button1_Click(object sender, System.EventArgs e) { LoggingConfiguration config = logger1.Factory.Configuration; LoggingRule rule = config.FindRuleByName("All"); rule.DisableLoggingForLevel(LogLevel.Debug); logger1.Factory.Configuration = config; if (logger1.IsDebugEnabled) { logger1.Debug("App Log 1"); } else { logger1.Trace("App Log 1"); } }
/// <summary> /// Set the NLog logging level to Debug or Info /// </summary> /// <param name="debug">If true set level to Debug, otherwise set to Info</param> public static void SetLogLevel(bool debug) { LoggingConfiguration config = LogManager.Configuration; LoggingRule rule = config.FindRuleByName("LogToFile"); if (rule != null) { if (debug) { rule.SetLoggingLevels(LogLevel.Debug, LogLevel.Fatal); } else { rule.SetLoggingLevels(LogLevel.Info, LogLevel.Fatal); } LogManager.ReconfigExistingLoggers(); } }
private static int Main(string[] args) { LoggingConfiguration config = LogManager.Configuration; if (config == null) { Console.WriteLine("\n~~~~~~~~~~~ Failed to get log configuration ~~~~~~~~~~~\n"); return(-99); } LoggingRule fileRule = config.FindRuleByName("file rule"); LoggingRule consoleRule = config.FindRuleByName("console rule"); fileRule.DisableLoggingForLevels(LogLevel.Info, LogLevel.Fatal); LogManager.Configuration = config; string script; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); var p = new FluentCommandLineParser <AppArguments>(); p.Setup(arg => arg.ShowBuildVersion) .As('v', "version") .Callback(arg => { showBuildVersion = arg; }) .SetDefault(false) .WithDescription("Show build version"); p.Setup(arg => arg.ScriptFile) .As('s', "script") //.Required() .WithDescription("Script file."); p.Setup(arg => arg.UserName) .As('u', "user") .WithDescription("Run script against AD Account name."); p.Setup(arg => arg.ShowAlert) .As('a', "alert") .Callback(arg => showUserAlert = arg) .SetDefault(false) .WithDescription("Notify the user if error. Default: FALSE (Don't notify)"); p.Setup(arg => arg.HideConsole) .As('h', "hide") .Callback(arg => hideConsole = arg) .SetDefault(false) .WithDescription("Hide console window during execution. Default: FALSE (Don't hide console)"); p.Setup(arg => arg.GetFirstAvailableLetter) .As('f', "first") .Callback(arg => getFirstAvailableLetter = arg) .SetDefault(false) .WithDescription("Resolve drive letter conflict to first available. Default: FALSE (Resolve to next available drive letter)"); p.SetupHelp("?", "help") .Callback(text => Console.WriteLine(text)); var result = p.Parse(args); if (showBuildVersion) { log.Info(version); return(0); } log.Info(""); log.Info($"Hikari V{version} - created by Stefan Bazelkov"); if (result.HelpCalled) { stopwatch.Stop(); ShowUsage(); return(0); } if (result.EmptyArgs) { p.HelpOption.ShowHelp(p.Options); ShowUsage(); stopwatch.Stop(); return(0); } if (result.HasErrors) { log.Error(result.ErrorText); stopwatch.Stop(); log.Info($"Time elapsed: {stopwatch.Elapsed.TotalSeconds:00.00}s"); ShowUserAlert("Command line arguments parsing failed!"); return(-1); } if (hideConsole) { // do not show log if console is hidden consoleRule.DisableLoggingForLevels(LogLevel.Trace, LogLevel.Fatal); ShowWindow(GetConsoleWindow(), ERROR_SW_HIDE); log.Info("Start with console window hidden."); } AppArguments appArg = p.Object; if (!File.Exists(appArg.ScriptFile)) { log.Error($"\"{appArg.ScriptFile}\" doesn't exist!"); stopwatch.Stop(); log.Info($"Time elapsed: {stopwatch.Elapsed.TotalSeconds:00.00}s"); ShowUserAlert($"Missing or wrong script file '{appArg.ScriptFile}'!"); return(-2); } if (!string.IsNullOrEmpty(appArg.UserName)) { user = ActiveDirectoryHelper.FindPrincipal(appArg.UserName); if (user == null) { stopwatch.Stop(); log.Info($"Time elapsed: {stopwatch.Elapsed.TotalSeconds:00.00}s"); log.Error($"Error in resolving provided username: \"{appArg.UserName}\""); return(-6); } } if (user != null) { // Do not map network drives if username argument is present // Only check the network shares to be connected for that AD user doMapping = false; log.Warn($"Checking mappings for \"{user.SamAccountName}\"."); log.Warn("No network drives will be connected."); } else { // logging will be performed only in the console fileRule.EnableLoggingForLevels(LogLevel.Info, LogLevel.Fatal); LogManager.Configuration = config; doMapping = true; user = UserPrincipal.Current; DisconnectAllLocalNetworkDrives(); log.Info($"Connecting network drives for \"{user.SamAccountName}\"."); } groups = ActiveDirectoryHelper.GetMembership(user).ToList(); Console.WriteLine(); log.Info($"Processing \"{appArg.ScriptFile}\" script ..."); using (StreamReader file = new StreamReader(appArg.ScriptFile)) { script = file.ReadToEnd(); file.Close(); } var parser = new HikariScriptParser(groups); try { parser.UserName = user.SamAccountName; parser.Parse(script); log.Info(parser.Model); } catch (HikariParserException x) { Console.WriteLine(); log.Error(x.Message); Console.WriteLine(); log.Warn("No drives will be connected!"); Console.WriteLine(); return(-7); } // Resolve and map the network drives ResolveMappings(parser.Model); if (doMapping) { DisconnectAllLocalNetworkDrives(); ConnectNetworkDrives(parser.Model); ConnectNetworkPrinters(parser.Model); } stopwatch.Stop(); Console.WriteLine(); log.Info($"Total time elapsed: {stopwatch.Elapsed.TotalSeconds:00.00}s"); Console.WriteLine(); return(0); }