public ProcessLogFile(string file, string regexPattern, ProcessedLogs pLog)
 {
     this.successful   = 0;
     this.failed       = 0;
     this.file         = file;
     this.regexPattern = regexPattern;
     this.pLog         = pLog;
 }
Exemple #2
0
        /// <summary>
        /// This is where the program will start and it will begin by reading in command line arguments passed and setting the
        /// properties, where expected values are
        /// -h --help - Use this flag to display the different command line arguments that can be passed to the application
        /// -o --output - Provide the filepath to the Directory you would like to have the output generated as Output.txt, if this is not provided application will only output to console
        /// -f --filePath - Provide the filepath to the log file you require to analyse
        /// -r --regularExpression - If Default Regular Expression pattern is not providing the correct output provide updated Regular Expression pattern by passing this flag followed by the pattern
        /// </summary>
        /// <param name="args">Command Line Arguments passed</param>
        static void Main(string[] args)
        {
            string file         = "";
            string outputPath   = "";
            string regexPattern = "";

            Console.WriteLine("-- Log Parser Start --");
            try
            {
                // Only run if the -h --help flags are not passed as arguments
                if (CommandLineArgumentParser.ParseCommandLineArguments(args, out outputPath, out regexPattern, out file))
                {
                    ProcessedLogs pLogs = new ProcessedLogs();
                    // Process the log files
                    ProcessLogFile logger = new ProcessLogFile(file, regexPattern, pLogs);
                    logger.ReadFromFileAndStore();
                    // Report on success / failed lines
                    Console.WriteLine(string.Format("The file has been processed where {0} lines were matched by the regex provided and {1} failed", logger.successful, logger.failed));
                    // Sort the processed logs
                    SortProcessedLogs sort = new SortProcessedLogs(pLogs);
                    sort.BeginSort();
                    // Output the results either to console, or to ouput file
                    OutputResults.PrintToConsole(sort.sortedIPList, sort.sortedUrlList, numberOfResults: ApplicationConfig.NumberToDisplay(),
                                                 displayUniqueIP: ApplicationConfig.DisplayUniqueIPs(), displayUniqueURL: ApplicationConfig.DisplayUniqueURLs());

                    if (!string.IsNullOrEmpty(outputPath))
                    {
                        OutputResults.PrintToFile(sort.sortedIPList, sort.sortedUrlList, outputFilepath: outputPath,
                                                  fileName: ApplicationConfig.OutputFileName(), numberOfResults: ApplicationConfig.NumberToDisplay(),
                                                  append: ApplicationConfig.AppendToOutputFile(), displayUniqueIP: ApplicationConfig.DisplayUniqueIPs(),
                                                  displayUniqueURL: ApplicationConfig.DisplayUniqueURLs());
                    }
                }
                Console.WriteLine("-- Log Parser End --");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Something went wrong during execution, refer to exception for additional information");
                Console.WriteLine(ex.Message);
                Console.WriteLine("-- Log Parser End --");
            }
        }
 public SortProcessedLogs(ProcessedLogs pLogs)
 {
     sortedUrlList = new List <KeyValuePair <string, int> >();
     sortedIPList  = new List <KeyValuePair <string, int> >();
     this.pLogs    = pLogs;
 }