Hi all,
I am a complete Novice at C# and have been asked by my company to create a small application that performs the following.
we have several servers with log files on them, what we want to to is read the log files and once read search the log file for specific words.
for example we want to search the log files for mainly the words error and display how many errors are contained in the log file.
at the moment I have created a app that has a text box that just displays the log file then at the bottom I have a two text boxes and one button. one text box where you can specify the word you want to search for and the other displays the result once the button is clicked
I would like some help on getting the search to work and to display the amount of times the word was found in the log file
Any help much appreciated
thanks
DavePosted Feb 7, 2008, 1:35 AM
You could then write a small batch file to automate what you want to do. Using grep is very easy. There would be plenty of tutorials around that could get you up and running in minutes.
Lee MarsdenPosted Feb 6, 2008, 12:25 PM
Thanks for this, I will have a play.
Jan MontanoPosted Feb 6, 2008, 3:27 AM
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 WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// I added 3 textboxes
// txtLogFile, txtKeyword, txtKeyCount
// and 1 button
// btnSearch
private void btnSearch_Click(object sender, EventArgs e)
{
// opens the file
StreamReader streamReader = new StreamReader(txtLogFile.Text);
// puts all the contents of the file in a string
string fileContent = streamReader.ReadToEnd();
int index = -1;
int keyCount = 0;
do
{
index = fileContent.IndexOf(txtKeyword.Text, ++index);
if (index >= 0)
{
// keyword found. increment count
keyCount++;
}
} while (index >= 0);
// closes the file
streamReader.Close();
// save value in textbox
txtKeyCount.Text = keyCount.ToString();
}
}
}
CHeers,
Jan