Beispiel #1
0
        public void LogRuleDisableLoggingLevels()
        {
            var rule = new LoggingRule();

            rule.EnableLoggingForLevels(LogLevel.MinLevel, LogLevel.MaxLevel);

            rule.DisableLoggingForLevels(LogLevel.Warn, LogLevel.Fatal);
            Assert.Equal(rule.Levels, new[] { LogLevel.Trace, LogLevel.Debug, LogLevel.Info });
        }
Beispiel #2
0
        public void SetLogLevels(string MinLevel, string MaxLevel = "")
        {
            if (!Initialized)
            {
                throw new InvalidOperationException("Not initialized.");
            }

            try {
                var min = LogLevel.FromString(MinLevel);
                var max = LogLevel.FromString(MaxLevel != String.Empty ? MaxLevel : MinLevel);

                rule.DisableLoggingForLevels(LogLevel.Trace, LogLevel.Fatal);
                rule.EnableLoggingForLevels(min, max);
                LogManager.ReconfigExistingLoggers();
            }
            catch (Exception ex) {
                ilogger.Error(ex, "Internal error");
                throw new InvalidOperationException(ex.Message);
            }
        }
Beispiel #3
0
        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);
        }