Click here to Skip to main content
15,881,679 members
Articles / Desktop Programming / Win32
Article

.NET File Logging Library

Rate me:
Please Sign up or sign in to vote.
2.33/5 (6 votes)
17 Apr 2008CPOL1 min read 42.6K   434   18   14
A simple yet useful library for file based logging implemented in .NET using C#

Introduction

This is a simple yet useful file based logging library along with a sample client application demonstrating the usage of the library.

Background

Following are some of the features of this library.

  • Allows logging messages by severity levels. Library currently supports 3 severity levels like Error, Warning and Information.
  • Provides wrap around feature to wrap log files when maximum configured size is reached.
  • Library provides feature to recover from exceptions. It basically attempts once to close the current log file and re-open a new one.
  • Library is thread safe.

This library is more like a starter kit with all the base functions in place. Please feel free to customize it for your needs.

Using the code

Library usage is very simple. The object has to be initialized first using the Initialize method. This is where the log file name and location has to be specified. Optionally, the maximum log file size can be specified as well. The default maximum log file size is 20 MB. Log file will truncate automatically when the configured maximum size is reached.

CLogger TestLogger = new CLogger();
TestLogger.Initialize("C:", "Test.Log"); // Location and file name
// TestLogger.Initialize("C:", "Test.Log", 20); // Optional max log size param in MB 

Start logging information right away. Each severity level has 3 overloads.

  • Overload for logging message strings
  • Overload for logging error codes and message strings
  • Overload for logging class name, method name and message strings
C#
TestLogger.LogInformation("My First Test Log Message");
TestLogger.LogWarning("My First Test Log Message");
TestLogger.LogError("My First Test Log Message");
TestLogger.LogInformation(1005, "My First Test Log Message");
TestLogger.LogInformation("LogClient", "TestLog", "My First Test Log Message");

Finally, this is how the logged statements look like.

[4/5/2008 1:36:29 AM],[1],[Information],LogClient:TestLog:My First Test Log Message
[4/5/2008 1:36:29 AM],[1],[Warning],LogClient:TestLog:My First Test Log Message
[4/5/2008 1:36:29 AM],[1],[Error],LogClient:TestLog:My First Test Log Message

Each statement has the date time stamp, thread id, severity and the message string.

Terminate the logger library using the Terminate statement.

TestLogger.Terminate();  

Happy logging. As always, happy to hear any suggestions and criticisms.

History

Library Version 1.0 - April 05, 2008

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHi Performance Logging Pin
Shane Thurai8-May-08 21:09
Shane Thurai8-May-08 21:09 
Hi msvenc,

Does this do Hi-performance text file logging?

I want to implement this to the server which will be hammered by visits. Can this project handle this? Is there any bottle neck? Could you please tell me how I can multi thread this application, if you can?

Thanks in Advance!

Shane
AnswerRe: Hi Performance Logging Pin
msvcyc9-May-08 7:17
msvcyc9-May-08 7:17 
QuestionRe: Hi Performance Logging Pin
Shane Thurai9-May-08 13:10
Shane Thurai9-May-08 13:10 
GeneralRe: Hi Performance Logging Pin
Shane Thurai22-May-08 21:04
Shane Thurai22-May-08 21:04 
QuestionCould this log work in multithread? Pin
zjs__200018-Apr-08 0:03
zjs__200018-Apr-08 0:03 
AnswerRe: Could this log work in multithread? Pin
zjs__200018-Apr-08 0:06
zjs__200018-Apr-08 0:06 
GeneralRe: Could this log work in multithread? Pin
msvcyc22-Apr-08 5:53
msvcyc22-Apr-08 5:53 
QuestionWhat is RTLog Reference in LogClient and Why is it Not found? Pin
RonHat8-Apr-08 6:46
RonHat8-Apr-08 6:46 
AnswerRe: What is RTLog Reference in LogClient and Why is it Not found? Pin
RonHat8-Apr-08 8:27
RonHat8-Apr-08 8:27 
GeneralRe: What is RTLog Reference in LogClient and Why is it Not found? Pin
msvcyc17-Apr-08 7:56
msvcyc17-Apr-08 7:56 
QuestionComparisons to other logging libraries? Pin
Mike Lang7-Apr-08 3:40
Mike Lang7-Apr-08 3:40 
AnswerRe: Comparisons to other logging libraries? Pin
msvcyc7-Apr-08 6:56
msvcyc7-Apr-08 6:56 
AnswerRe: Comparisons to other logging libraries? Pin
Alexander Vasilyev8-Apr-08 8:16
Alexander Vasilyev8-Apr-08 8:16 
GeneralRe: Comparisons to other logging libraries? Pin
John Camaro10-Apr-08 23:07
John Camaro10-Apr-08 23:07 

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.