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.
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:
Member(s) and Constants
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?
- Instantiate the class:
m_MyLogger = new clsFileLogger();
- Tell the class the fileName and Location:
strFileLogger = Directory.GetCurrentDirectory() + \\MyLog.log;
m_MyLogger.Filename = strFileLogger;
- Give a User Name as default, if you want to.
- Then Use function
LogError which has 3 overrides.
- 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.
- This is version 1.00. I have used it in 3 different projects so far, and so far so good.