Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET

Simple Logger for Applicative and ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.31/5 (5 votes)
3 Apr 2010CPOL2 min read 18.4K   169   4   3
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:

C#
#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:
    C#
    m_MyLogger = new clsFileLogger(); 
  2. Tell the class the fileName and Location:
    C#
    strFileLogger = Directory.GetCurrentDirectory() + \\MyLog.log; 
    C#
    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)


Written By
Software Developer
Canada Canada
15 Year's experience programmer with VB and C/C++, C#
I am not really a fan of VB, i prefer by far C# (unsafe code closer to 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 5 years and having a lot of fun.
5 years experience Pocket PC and DevExpress SDK.
Having fun with Arduino and PLC (mostly HMI design)

Comments and Discussions

 
GeneralMy vote of 5 Pin
Volynsky Alex3-Dec-14 6:15
professionalVolynsky Alex3-Dec-14 6:15 
GeneralArchitecture Pin
John Brett6-Apr-10 2:19
John Brett6-Apr-10 2:19 
GeneralRe: Architecture Pin
Yves6-Apr-10 12:06
Yves6-Apr-10 12:06 

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.