Process a Log File - CSharp File IO

CSharp examples for File IO:Text File

Description

Process a Log File

Demo Code


using System;/*ww w .  j a  v  a 2s .  c o  m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class MainClass
    {
        static void Main(string[] args)
        {
            IEnumerable<string> alldata = File.ReadAllLines("all_entries.log");
            foreach (string entry in alldata)
            {
                Console.WriteLine("Entry: {0}", entry);
            }

            IEnumerable<string> somedata = File.ReadLines("all_entries.log").Where(e => e.StartsWith("Error"));
            foreach (string entry in somedata)
            {
                Console.WriteLine("Error entry: {0}", entry);
            }

            IEnumerable<char> chardata = File.ReadLines("all_entries.log").Where(e => !e.StartsWith("Error")).Select(e => e[0]);
            foreach (char entry in chardata)
            {
                Console.WriteLine("Character entry: {0}", entry);
            }
        }
    }

Result


Related Tutorials