Log file parsing based on error level

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.IO;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //Log format: <Time stamp> <Error Level> <Message>

            StreamWriter sw = new StreamWriter(@"D:\Log.txt");

            sw.WriteLine(DateTime.Now + " Error " + "Error in reading the registry key");

            sw.WriteLine(DateTime.Now + " Warning " + "Device is running low on memory");

            sw.WriteLine(DateTime.Now + " Info " + "Current available physical memory is 30 MB");

            sw.WriteLine(DateTime.Now + " Verbose " + "Device state is now active");

            sw.Close();

        }

    }

}
 

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.IO;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            StreamReader sr = new StreamReader(@"D:\Log.txt");

            DateTime startTime = DateTime.Now;//userInput

            startTime = startTime.AddHours(-1);

            DateTime endTime = DateTime.Now;//userInput

            string logLevel = "Verbose";//userInput

            string fileLine = sr.ReadLine();

            while (fileLine != null)

            {

                Regex reg = new Regex(" ");

                string[] parsedLine = reg.Split(fileLine);

                DateTime currentDateTime = DateTime.Parse(parsedLine[0] + " " + parsedLine[1] + " " + parsedLine[2]);

                string currentLogLevel = parsedLine[3];

                string currentMessage = "";

                for (int i = 4; i < parsedLine.Length; i++)

                {

                    if (i < parsedLine.Length – 1)

                    {

                        currentMessage = currentMessage + parsedLine[i] + " ";

                    }

                    else

                    {

                        currentMessage = currentMessage + parsedLine[i];

                    }

                }

                if (currentDateTime >= startTime && currentDateTime <= endTime)

                {

                    switch (logLevel)

                    {

                        case "Error":

                            if (parsedLine[3] == "Error")

                            {

                                Console.WriteLine(currentMessage);

                            }

                            break;

                        case "Warning":

                            if (parsedLine[3] == "Error" || parsedLine[3] == "Warning")

                            {

                                Console.WriteLine(currentMessage);

                            }

                            break;

                        case "Info":

                            if (parsedLine[3] == "Error" || parsedLine[3] == "Warning" || parsedLine[3] == "Info")

                            {

                                Console.WriteLine(currentMessage);

                            }

                            break;

                        case "Verbose":

                            Console.WriteLine(currentMessage);

                            break;

                        default:

                            break;

                    }

                }

                else

                {

                    break;

                }

                fileLine = sr.ReadLine();

            }

            sr.Close();

        }

    }

}
 
Error in reading the registry key
Device is running low on memory
Current available physical memory is 30 MB
Device state is now active
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s