Title: Make a simple event logger in C#
Usually the debugger lets you examine how a program works quite effectively, but sometimes it's useful to use an event logger to record events for later study. For example, when you're working with mouse events, stopping execution at a break point often messes up the sequence of events that the program is trying to track. In that case you may want to record the event information in a file and look at it later.
This example uses the following simple event logger class to record text messages in a file.
public static class Logger
{
// Calculate the log file's name.
private static string LogFile =
Environment.GetFolderPath(
Environment.SpecialFolder.Desktop) +
"\\Log.txt";
// Write the current date and time plus
// a line of text into the log file.
public static void WriteLine(string txt)
{
File.AppendAllText(LogFile,
DateTime.Now.ToString() + ": " + txt + "\n");
}
// Delete the log file.
public static void DeleteLog()
{
File.Delete(LogFile);
}
}
The event logger class and its methods are declared static so the main program can use its methods without creating an instance of the class, much as you can use the Console and Debug classes.
The class begins by defining a string that holds the name of the log file. This example names the file Log.txt and uses the Environment class's GetFolderPath method and SpecialFolder enumeration to place the file on the current user's desktop.
The class's WriteLine method uses File.AppendAllText to add the current time plus a message string to the log file.
The DeleteLog method deletes the log file so the program can start a new one.
The following code shows the main program that demonstrates the event logger class.
// Start with a fresh log file.
private void Form1_Load(object sender, EventArgs e)
{
Logger.DeleteLog();
}
// Record MouseEnter and MouseLeave events.
private void picVolleyball_MouseEnter(object sender, EventArgs e)
{
Logger.WriteLine("MouseEnter");
}
private void picVolleyball_MouseLeave(object sender, EventArgs e)
{
Logger.WriteLine("MouseLeave");
}
When the form loads, the program uses the DeleteLog method to delete the existing log file and start a new one.
This example logs MouseEnter and MouseLeave events as the mouse moves over the picVolleyball PictureBox. It simply writes a message into the log indicating which event fired.
There are lots of customizations you could make to this example. You could put the log file in locations other than the desktop. You could give the file a different name or let the program set the name at run time so different programs could have their own log files. You could even make the Logger class non-static so a program could use more than one Logger object to write into multiple log files. You could also change the format of the date and time written into the file. For example, this example records the date and time to the minute. If you're tracking events that occur very closely together, you may want to display only the time and that to the 10th of a second.
However, the simple approach shown here is good enough for most debugging situations.
Note that appending to the file does take some time so you won't want to do it many times during a fast procedure. For example, if the user must move the mouse back and forth quickly across a PictureBox, you may notice a slow down if you log every MouseMove event.
Download the example to experiment with it and to see additional details.
|