Exemple #1
0
        static async Task Main(string[] args)
        {
            var logger               = _container.GetService <ILogger <Program> >();
            var consolePrinter       = _container.GetService <IConsolePrinter>();
            var controller           = _container.GetService <IController>();
            var mediator             = _container.GetService <IMediator>();
            var consoleFlagParser    = new ConsoleFlagParser();
            var consoleCommandParser = new ConsoleCommandParser(consoleFlagParser);

            try
            {
                await mediator.Send(new InitializeStorageCommand());

                var argsFlags   = consoleFlagParser.Parse(args);
                var credentials = GetCredentials(argsFlags);
                var isAuthQuery = new IsAuthenticatedQuery(credentials.Login, credentials.Password);

                if (!(await mediator.Send(isAuthQuery)))
                {
                    throw new ArgumentException("Incorrect login or password");
                }
                consolePrinter.PrintAuthenticationSuccessful();

                var command = new StorageCommand();
                while (command.CommandType != StorageCommands.Exit)
                {
                    try
                    {
                        command = GetCommand(consoleCommandParser, consolePrinter);
                        await controller.ExecuteConsoleCommand(command);
                    }
                    catch (AggregateException agEx)
                    {
                        foreach (var innerEx in agEx.InnerExceptions)
                        {
                            string logMessage = ConvertingHelper.GetLogMessage(innerEx.Message, innerEx.StackTrace);
                            logger.LogError(logMessage);
                            consolePrinter.PrintErrorMessage(innerEx.Message);
                        }
                    }
                    catch (Exception ex)
                    {
                        string logMessage = ConvertingHelper.GetLogMessage(ex.Message, ex.StackTrace);
                        logger.LogError(logMessage);
                        consolePrinter.PrintErrorMessage(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                string logMessage = ConvertingHelper.GetLogMessage(ex.Message, ex.StackTrace);
                logger.LogError(logMessage);
                consolePrinter.PrintErrorMessage(ex.Message);
                Environment.Exit(-1);
            }
        }
        private void LogInformationMessage(string message)
        {
            string logMessage = ConvertingHelper.GetLogMessage(message, string.Empty);

            _logger.LogInformation(logMessage);
        }