static void Main(string[] args)
        {
            var config  = new NLog.Config.LoggingConfiguration();
            var logfile = new NLog.Targets.FileTarget("logfile")
            {
                FileName = "file.txt"
            };
            var logconsole = new NLog.Targets.ConsoleTarget("logconsole");

            config.AddRule(LogLevel.Trace, LogLevel.Fatal, logconsole);
            config.AddRule(LogLevel.Trace, LogLevel.Fatal, logfile);
            LogManager.Configuration = config;
            Options options = new Options();

            Parser.Default.ParseArguments <Options>(args).WithParsed((Options parsedOptions) => { options = parsedOptions; });
            try
            {
                ValidationProgramArguments.Validation(options);
                if (options.OutputFileFormat.Equals(""))
                {
                    options.OutputFileFormat = "Excel";
                }
                if (!options.OutputFile.EndsWith(".xlsx") && options.OutputFileFormat.Equals("Excel"))
                {
                    options.OutputFile += ".xlsx";
                }
                else if (!options.OutputFile.EndsWith(options.OutputFileFormat.ToLower()))
                {
                    options.OutputFile += "." + options.OutputFileFormat.ToLower();
                }
                List <string>  strList = new List <string>();
                List <Student> file    = Reader.Read(options.InputFile, out strList).ToList();
                if (file != null && file.Count != 0)
                {
                    if (options.OutputFileFormat.Equals("JSON"))
                    {
                        JsonWriter writer = new JsonWriter();
                        writer.Write(file, strList, options.OutputFile);
                    }
                    else if (options.OutputFileFormat.Equals("Excel"))
                    {
                        ExcelWriter writer = new ExcelWriter();
                        writer.Write(file, strList, options.OutputFile);
                    }
                    else
                    {
                        throw new IOSystemException("Wrong output format");
                    }
                }
                else if (file == null)
                {
                    throw new IOSystemException("File not found");
                }
                else
                {
                    throw new IOSystemException("File don't have any correct line.");
                }
            }
            catch (Exception ex) when(ex is IOSystemException || ex is FieldNameException || ex is FIOFieldException || ex is MarkFieldException)
            {
                Logger.Error(ex, ex.Message);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Unexepted Exception: " + ex.Message);
                throw;
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            Options options = new Options();

            Parser.Default.ParseArguments <Options>(args).WithParsed((Options parsedOptions) => { options = parsedOptions; });
            if (options.InputFile.Equals(""))
            {
                options.InputFile = @"...\File.csv";
            }
            if (options.OutputFile.Equals(""))
            {
                options.OutputFile = @"...\File_out";
            }
            if (options.OutputFileFormat.Equals(""))
            {
                options.OutputFileFormat = "Excel";
            }
            if (!options.OutputFile.EndsWith(".xlsx") && options.OutputFileFormat.Equals("Excel"))
            {
                options.OutputFile += ".xlsx";
            }
            else if (!options.OutputFile.EndsWith(options.OutputFileFormat.ToLower()))
            {
                options.OutputFile += "." + options.OutputFileFormat.ToLower();
            }
            try
            {
                ValidationProgramArguments.Validation(options);
                List <string>  strList = new List <string>();
                List <Student> file    = Reader.ReadFile(options.InputFile, out strList);
                if (file != null && file.Count != 0)
                {
                    if (options.OutputFileFormat.Equals("JSON"))
                    {
                        WriterJSON writer = new WriterJSON();
                        writer.Write(file, strList, options.OutputFile);
                    }
                    else if (options.OutputFileFormat.Equals("Excel"))
                    {
                        WriterExcel writer = new WriterExcel();
                        writer.Write(file, strList, options.OutputFile);
                    }
                    else
                    {
                        throw new IOSystemException("Wrong output format");
                    }
                }
                else if (file == null)
                {
                    throw new IOSystemException("File not found");
                }
                else
                {
                    throw new IOSystemException("File don't have any correct line.");
                }
            }
            catch (IOSystemException ex)
            {
                Logger.Log("Input/Output System exception: " + ex.Message);
            }
            catch (FieldNameException ex)
            {
                Logger.Log("Field Name Exception: " + ex.Message);
            }
            catch (FIOFieldException ex)
            {
                Logger.Log("FIO Exception: " + ex.Message);
            }
            catch (MarkFieldException ex)
            {
                Logger.Log("Mark Exception: " + ex.Message);
            }
            catch (Exception ex)
            {
                Logger.Log("Unexeptable Exception:" + ex.Message);
            }
        }