|
-
March 14th, 2011, 07:37 AM
#1
read/write text file (log parsing)
hi all,
i am a newbie in all the C# world. i have to create an application that will take a certain log and will do parsing to it. first of all it needs to read the lines that contains a certain string and write it to another log with just the hour and part of the line.
the logfile lines that need to be parsed look like this.
Code:
Thread#96( ...............)<<<2011-02-06 19:09:33.648<<<[ INFO]EF2:EFDevice.ValidateConnection calling Connect() ...
Thread#04(4C Server Main )<<<2011-02-06 19:09:49.148<<<[ INFO]EF2:EFDevice.m_Communication_Connected.
Thread#04(4C Server Main )<<<2011-02-06 19:09:49.148<<<[ INFO]EF2:EFDevice.Reset BEGIN Reset of the fence controller
Thread#04(4C Server Main )<<<2011-02-06 19:09:49.148<<<[ INFO]EF2:EFDevice.Reset Sent: 0xF
Thread#04(4C Server Main )<<<2011-02-06 19:09:49.258<<<[ INFO]EF2:EFDevice.Reset Sent: 0xF
Thread#96( ...............)<<<2011-02-06 19:09:49.367<<<[ INFO]EF2:EFDevice.ValidateConnection Connect() took:15.71875seconds
Thread#96( ...............)<<<2011-02-06 19:09:49.367<<<[ INFO]EF2: SND 0x0 took:0
Thread#04(4C Server Main )<<<2011-02-06 19:09:49.602<<<[ INFO]EF2:Default sector will be associated with the DefaultPresetName:Preset1
Thread#04(4C Server Main )<<<2011-02-06 19:09:49.883<<<[ INFO]EF17:EFDevice.m_Communication_Connected.
Thread#04(4C Server Main )<<<2011-02-06 19:09:49.883<<<[ INFO]EF17:EFDevice.Reset BEGIN Reset of the fence controller
Thread#04(4C Server Main )<<<2011-02-06 19:09:49.883<<<[ INFO]EF17:EFDevice.Reset Sent: 0xF
Thread#04(4C Server Main )<<<2011-02-06 19:09:49.992<<<[ INFO]EF17:EFDevice.Reset Sent: 0xF
Thread#04(4C Server Main )<<<2011-02-06 19:09:50.102<<<[ INFO]EF17:Default sector will be associated with the DefaultPresetName:Preset1
Thread#96( ...............)<<<2011-02-06 19:09:50.367<<<[ INFO]EF2: SND 0x0 took:0
Thread#22(ConfigurationSetManager )<<<2011-02-06 19:09:50.727<<<[ INFO]EF17:Default sector will be associated with the DefaultPresetName:Preset1
Thread#99( ...............)<<<2011-02-06 19:09:50.727<<<[ INFO]EF17:EFDevice.m_Communication_Disconnected
what i need is to write these kind of lines to a new log which should look like this.
hour EF__ SND hex number
currently i have a code that reads the text file and writes down to another log only the lines that contains "SND". this is currently my code.
I have no clue how to write to the new log from a certain place in the line (EF___)
this is my code
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Log_Parser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.IO.StreamWriter theWriter = null;
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string FilePath = @"C:\weather.txt";
if (!File.Exists(FilePath))
{
MessageBox.Show("The Text File could not be found", "File Missing");
return;
}
else
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(FilePath))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("SND"))
{
theWriter = new System.IO.StreamWriter(@"C:\maniplog.txt", true);
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
string sHour = DateTime.Now.Hour.ToString();
string sMinutes = DateTime.Now.Minute.ToString();
string sSeconds = DateTime.Now.Second.ToString();
theWriter.WriteLine(sYear + " " + sMinutes + " " + sSeconds + " " + line);
theWriter.Close();
}
else
{
Console.WriteLine(line);
}
}
}
}
catch
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
//Console.WriteLine(e.Message);
}
}
}
}
thanks alot for the help
Jonathan
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|