Click here to Skip to main content
Click here to Skip to main content
Go to top

Simple Logger for Applicative and ASP.NET

, 3 Apr 2010
Rate this:
Please Sign up or sign in to vote.
Limit the size of your logging

Introduction

I wrote a program for Pocket PC, and I needed to log the error in a file, but I did not want the file to grow and grow until there was no more room in the SD RAM key.

Background

I know... there is a lot of code on how to log errors in a file, XML, registry, etc.

What if you write services on a server that run every day, every hour and so on, are you going to log in a file that grows up every time. You are not the admin of the server and can't do the clean-up, but if something wrong happened then you need to see the log file in order to know what went wrong. What about if you write a Pocket PC application and you need to log what is going on, the resources are limited and the sky is not the limit. Yeah, use registry to log the stuff and it will grow up in size every time.

Using the Code

At the beginning of the class, you will see the following:

#region
Member(s) and Constants
// File Size max line 1000 

const long MAXFILE_LINE = 1000;

Each log is a line entry of text, and the constant MAXFILE_LINE is the maximum number of lines before the log starts over again to line 1 ... circular log. Increase or decrease, fit to your need.

How would the log file look?

   
2009/09/21 10:37:14 LDapSynchro--> Starting Process
2009/09/21 10:37:25 LDapSynchro--> Finished processing
2009/09/21 10:42:56 LDapSynchro--> Starting Process
2009/09/21 10:43:09 LDapSynchro--> Finished processing
2009-10-14 13:43:32 LDapSynchro--> Starting Process
2009-10-14 13:43:46 LDapSynchro--> Finished processing
2009-10-14 13:44:01 LDapSynchro--> Starting Process
2009-10-14 13:44:14 LDapSynchro--> Finished processing        

The file is simple text (the strange character at the beginning is a binary counter). In this example, I had no user but simply a service called LDapSynchro. Simple and effective.

How to use?

  1. Instantiate the class:
    m_MyLogger = new clsFileLogger(); 
  2. Tell the class the fileName and Location:
    strFileLogger = Directory.GetCurrentDirectory() + \\MyLog.log; 
    m_MyLogger.Filename = strFileLogger;
  3. Give a User Name as default, if you want to.
  4. Then Use function LogError which has 3 overrides.
  5. At the end, use the Close() function that also frees up allocation (Important on Pocket PC).

Points of Interest

There are a lot of complicated classes used for logging, but they are big and for logging sky is the limit, but in real life that is not the situation. And do not forget that is a circular logger and that means a lot. I can ask the customer to send me this simple LogFile so that I can take a look and see what is going on. It works with Pocket PC, Mobile, Applicative, and ASP.NET.

History

  • This is version 1.00. I have used it in 3 different projects so far, and so far so good.

License

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

Share

About the Author

Yves
Software Developer
Canada Canada
8 Year's experience programmer with VB and C/C++
Build many applications using DataBase (SQLsrv, Access, Sql Ce, MySQL, etc).
Most of my experiences is on building GUI applications for customers.
At ease with DirectShow and video Frame Grabber.
Now doing ASP Net for at least 2 years and having a lot of fun.
2 years experience Pocket PC and DevExpress SDK.

Comments and Discussions

 
GeneralArchitecture PinmemberJohn Brett6-Apr-10 2:19 
I like the basic premise. Loggers do seem to have become overly complex for the common requirements these days.
A few comments about your architecture:
cls prefix on class names - very VB6 Frown | :-(
const long MAXFILE_LINE - any reason this isn't either a property or a constructor parameter? Given the usage, making it a property (and therefore changeable on the fly) wouldn't be hard. Asking an end-user to actually modify your source to tweak it seems a little excessive.
The error handling strategy is a little weak. I can understand not wanting to throw exceptions from the logging functions, but the code doesn't attempt any sort of recovery or shut-down. Relying on the caller to a) notice and b) do something about an error seems like an optimistic plan.
The date/time format is hard-coded. Whilst you've chosen the best (IMHO) fixed option, I'd make the format string a property the caller can change.
Instead of hard-coding the characters CR and LF, there is the System.Environment.NewLine property to save you from doing so.
A nice technique for presenting a user-friendly interface to your logging is to expose the String.Format formatting string in conjunction with params, like so:
public bool LogError(string format, params object[] args)
{
return this.WriteToFile(string.Format(params, args));
}
Then the caller can write logger.LogError("At {0} the system reported a {1}", time, msg);
You might want to set the file sharing permissions when you open the log. Sometimes its useful to be able to read the log whilst the application is still running, and that can be hard if you've denied read access inadvertently.
 
Otherwise, a nice compact basic. logger.
GeneralRe: Architecture PinmemberYves6-Apr-10 12:06 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 3 Apr 2010
Article Copyright 2010 by Yves
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid