Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Logger for Applicative and ASP.NET

0.00/5 (No votes)
3 Apr 2010 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here