Click here to Skip to main content
Click here to Skip to main content

Simple Logger for Applicative and ASP.NET

, 3 Apr 2010 CPOL
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 
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
Web03 | 2.8.141015.1 | Last Updated 3 Apr 2010
Article Copyright 2010 by Yves
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid