Esempio n. 1
0
        public CmdLineArgs ParseArguments(string[] args)
        {
            CmdLineArgs cmdLineArgs = new CmdLineArgs();

            if (args.Length != 0)
            {
                if (IfElementExist(args, "--help"))
                {
                    cmdLineArgs.isHelp = true;
                }
                else if (args.Length % 2 != 0)
                {
                    throw new ArgumentException("Error: Missing argument or value");
                }
                else
                {
                    for (var i = 0; i < args.Length; i += 2)
                    {
                        switch (args[i])
                        {
                        case "--log-level":
                            ValidateLogLevelValue(args[i + 1]);
                            cmdLineArgs.logLevel.Add(args[i + 1]);
                            break;

                        case "--log-dir":
                            ValidateLogDir(args[i + 1]);
                            cmdLineArgs.logDir = args[i + 1];
                            break;

                        case "--csv":
                            cmdLineArgs.csvFile = args[i + 1];
                            break;

                        default:
                            throw new ArgumentException($"Error: {args[i]} is not a valid argument!");
                        }
                    }
                }
            }
            else
            {
                throw new ArgumentException("Error: No arguments provided!");
            }
            return(cmdLineArgs);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            CmdLineArgsParser cmdArgParser = new CmdLineArgsParser();
            CmdLineArgs       cmdLineArgs  = new CmdLineArgs();

            try
            {
                cmdLineArgs = cmdArgParser.ParseArguments(args);
                if (cmdLineArgs.isHelp)
                {
                    Console.WriteLine(Usage.usage());
                }
                else
                {
                    LogsToCSVConvertor logsToCSVConvertor = new LogsToCSVConvertor();
                    try {
                        logsToCSVConvertor.ConvertLogsToCsv(cmdLineArgs);
                    }
                    catch (Exception e) {
                        Console.WriteLine(e.Message);
                    }
                }
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(Usage.usage());
            }
            catch (DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (InvalidLogLevelException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(Usage.usage());
            }
        }
Esempio n. 3
0
        // string debugRegex = @"([0-9]{2}\/[0-9]{2})\s([0-9]{2}:[0-9]{2}:[0-9]{2})\s(DEBUG).*";
        // string errorRegex = @"([0-9]{2}\/[0-9]{2})\s([0-9]{2}:[0-9]{2}:[0-9]{2})\s(ERROR).*";
        // string warnRegex = @"([0-9]{2}\/[0-9]{2})\s([0-9]{2}:[0-9]{2}:[0-9]{2})\s(WARN).*";

        public void ConvertLogsToCsv(CmdLineArgs args)
        {
            ParseFiles(args.logDir);

            if (logFiles.Count != 0)
            {
                int counter = 0;
                foreach (string file in logFiles)
                {
                    using (StreamReader inFile = new StreamReader(file)) {
                        string ln;

                        while ((ln = inFile.ReadLine()) != null)
                        {
                            Regex r = new Regex(infoRegex);
                            Match m = r.Match(ln);
                            if (m.Success)
                            {
                                Group date  = m.Groups[1];
                                Group time  = m.Groups[2];
                                Group level = m.Groups[3];
                                Group text  = m.Groups[4];
                                counter++;

                                using (StreamWriter outFile = File.AppendText(args.csvFile))
                                {
                                    outFile.WriteLine($"\"{counter}\",\"{date}\",\"{time}\",\"{level}\",\"{text}\"");
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                throw new Exception($"No log files found under directory {args.logDir}");
            }
        }