Click here to Skip to main content
15,888,088 members
Articles / Programming Languages / C#
Article

Easy Log File Object

Rate me:
Please Sign up or sign in to vote.
1.44/5 (3 votes)
27 Mar 2008CPOL2 min read 22K   184   14   1
Add a log file to your code to assist troubleshooting

Introduction

LogFile gives you an easy interface to generate log files in your application. You can trigger log file creation from code, or by having the user create a trigger file. You can set the logging level (none, debug, brief, normal, verbose) for each log entry so that it only logs the entry at or below your specified logging level.

Background

Invariably, a user will encounter some intermittent bug that I can not reproduce. These can be a real headache to troubleshoot. Depending on the user, getting vital information can be an exercise in cultural anthropology. (Actual user quote: "I noticed that it popped up an error once when I hit the NumLock key on my keyboard, so I just use the number keys above the letters")

I needed a way that I could create log entries of what my program was doing and save it to a file that the user could email to me.

Using the code

Create a Logging object as a class-scoped object and set the logging level when your application starts.

Logging Level:
In this example, I am using the default trigger file path - a file called "MyApp.dbg" in My Documents. Have your user enter the debug level number (0 - 4) in the file to specify the logging level.

Log File Path:
The default location for the log file is My Documents\[appname].log. You can manually set the log file path using the log.LogFilePath(path) property, but you will want to vette your file path. The class does some basic checks to verify the path would be legitimate, but it will throw an exception if the file could not be created. The file does not have to exist - the class will create it if missing.

C#
namespace MyApp
{
    public partial class Form1 : Form
    {
        BearNakedCode.Logfile log = new BearNakedCode.Logfile();

        private void Form1_Load(object sender, EventArgs e)
        {
            //The default file path for the trigger file is 
            //My Documents\[application name].dbg.  You could 
            //also specify another trigger file by passing the file path.
            log.SetLoggingLevelFromFile();

            // Alternatively, you can set the logging level by code:
            // log.LoggingLevel = BearNakedCode.Logfile.enLoggingLevel.Verbose

            //You can set your log file path using
            //log.LogFilePath("c:\mydir\myapp.log");
        }
     } 

When you want to create a log entry, call the Log.LogEntry() method:

C#
private void YourMethod()
{
    //This entry will write to the log file if logging level
    //is set to Normal or higher:
    log.LogEntry("YourMethod: Test entry",
                  BearNakedCode.Logfile.enLoggingLevel.Normal);

    //This entry will only write to the log file if logging
    //level is set to Verbose:
    log.LogEntry("YourMethod: Started at " + DateTime.Now.ToString(),
                  BearNakedCode.Logfile.enLoggingLevel.Verbose);
}


Important: The logging class will continue to append to the log file. If you're making lengthy log entries, the file can grow to a large size quickly. You will want to clear the file or have the user remove the trigger file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
United States United States
I am a Systems Programmer/Analyst and System Administrator for a 400+ server Citrix ASP provider. I would call my job a "Slack Propagation Programmer" - I create applications that take 2 hours to write that automate a repetitive 10 minute task.

I also have a blog - BearNakedCode.com, where I post code samples and write on other topics of interest.

Comments and Discussions

 
QuestionWhy not use Trace? Pin
leppie27-Mar-08 23:46
leppie27-Mar-08 23:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.