Exemple #1
0
        public async Task InvAsync_Warning_Enabled_And_Warnings_in_Data_Enabled_Expected_Result_0()
        {
            TestConsole console = new TestConsole();
            State       warning = State.enabled;

            _data = _store.Read();
            _data.LoggerWarnings = State.enabled;
            _store.Write(_data);

            LoggerCommand command = new LoggerCommand(warning, _store, _logger, _data);

            var result = await command.InvAsync(console);

            _data = _store.Read();

            Assert.AreEqual(0, result);
        }
Exemple #2
0
        public static async Task <int> MainAsync(string[] args)
        {
            //Default data source.
            var dataFileName = ConfigurationManager.AppSettings["dataFilename"];

            // Default format of data storage (Xml, Json atm.)
            Type dataStoreFormat = typeof(Xmlformat);

            // Default data type
            Type dataType = typeof(AppData);

            // Initiate data manager class
            var store = new FileStore(dataFileName, dataStoreFormat, dataType);

            // Initiate Application logger
            var logger = new AppLogger("log.txt");

            // Reading data of default data type from default source.
            // If commands needs to operate with different data type and/or location,
            // store.Read() should be called at the begining of Command handler InvokeAsync method.
            var data = store.Read();

            if (data == null)
            {
                // Log warning and go on with code
                logger.Responses.Add(new LogResponse {
                    Logger          = "Main",
                    MessageLogged   = false,
                    ResponseMessage = "Can't load/missing application data needed for at least some commands."
                });
            }

            logger.Write(MsgCategory.Info, "Application started.", console: false, file: true);

            // Building command line commands and parsing provided arguments
            try
            {
                RootCommand command = new RootCommand
                {
                    ServerListCommand.Create(),
                            ConfigCommand.Create(),
                            ExportCommand.Create(),
                            LoggerCommand.Create()
                };
                Parser parser = BuildParser(command, logger, store, data);
                await parser.InvokeAsync(args);
            }
            catch (Exception ex)
            {
                logger.Write(MsgCategory.Error, "Application terminated: " + ex.Message, console: true, file: true);
                return(0);
            }

            // If app settings for warnings are enabled
            // Informing user about warnings before exit
            var appWarn = logger.Responses.Where(rs => rs.MessageLogged == false).Distinct().ToList();

            if (appWarn.Count > 0)
            {
                logger.Write(MsgCategory.Info, "Application completed with warnings:", console: true, file: false);
                foreach (var response in appWarn)
                {
                    logger.Write(MsgCategory.Warning, message: response.Logger + ": " + response.ResponseMessage, console: true, file: true);
                }
                return(1);
            }

            logger.Write(MsgCategory.Info, "Application completed.", console: false, file: true);
            return(1);
        }