Home > front end >  Adding results of a if loop into a String/List and print the output outside the loop
Adding results of a if loop into a String/List and print the output outside the loop

Time:07-12

The following code will go through all the .log files in a given directory and check if the logs contain the words "result=[false]" and print out the line that contains the matching words in each iteration. I don't want to print out results for each iteration, rather store them in another string or list and print out the final results outside of the condition loops.

The final goal is to use this string or list and send them as alerts to an email.

static void Main(string[] args)
        {
            string rootPath = @"C:\Logs";
            var files = Directory.GetFiles(rootPath, "*.log*", SearchOption.AllDirectories);
            

            foreach (string file in files)
            {
                

                StreamReader myReader = new StreamReader(file);
                string line = "";

                while (line != null)
                {
                    line = myReader.ReadLine();

                    if (line != null)
                    {
                        if (line.Contains("result=[false]") == true)
                        {
                            string datetime = line.Split(' ')[0]   " "   line.Split(' ')[1];
                            string tablename = line.Split(' ')[3];

                            line = String.Format("Date : {0} -- Table : {1} ", datetime, tablename);
                            Console.WriteLine(line); 
                            

                        }
                    else
                        {
                            continue;
                        }
                    }
                }
            
             myReader.Close();
              

            }
            Console.ReadLine();
        }

CodePudding user response:

Well, i thinck this is simple, not sure this deserve a question :)

static void Main(string[] args)
        {
            List<string> listToPrint = new List<string>();
            string rootPath = @"C:\Logs";
            var files = Directory.GetFiles(rootPath, "*.log*", SearchOption.AllDirectories);
            

            foreach (string file in files)
            {
                

                StreamReader myReader = new StreamReader(file);
                string line = "";

                while (line != null)
                {
                    line = myReader.ReadLine();

                    if (line != null)
                    {
                        if (line.Contains("result=[false]") == true)
                        {
                            string datetime = line.Split(' ')[0]   " "   line.Split(' ')[1];
                            string tablename = line.Split(' ')[3];

                            line = String.Format("Date : {0} -- Table : {1} ", datetime, tablename);
                            //Console.WriteLine(line); 
                            listToPrint.Add(line);

                        }
                    else
                        {
                            continue;
                        }
                    }
                }
            
             myReader.Close();
              

            }
            
            //print result
            listToPrint.ForEach(r => Console.WriteLine(r));
            
            Console.ReadLine();
        }

CodePudding user response:

This should work!

static void Main(string[] args)
{
    string rootPath = @"C:\Logs";
    var files = Directory.GetFiles(rootPath, "*.log*", SearchOption.AllDirectories);

    List<string> Results = new List<string>();

    foreach (string file in files)
    {
        StreamReader myReader = new StreamReader(file);
        string line = ""; // Consider changing this to string.Empty

        // Changed this to a do-while loop since line because line is initialized with ""
        do
        {
            line = myReader.ReadLine();
            if (line != null)
            {
                if (line.Contains("result=[false]") == true)
                {
                    string[] columns = line.Split(' ');
                    string datetime = columns[0]   " "   columns[1];
                    string tablename = columns[3];
                    Results.Add(string.Format("Date : {0} -- Table : {1} ", datetime, tablename));
                }
                else
                {
                    break;
                }
            }
        } while (line != null);

        myReader.Close();
    }

    // Results now contains all of the lines that contained "result=[false]" - Each string represents one line/match

    Results.ForEach((result) => Console.WriteLine(result));

    Console.ReadLine();
}
  • Related