Click here to Skip to main content
13,260,330 members (36,659 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 4 Apr 2008

.NET File Logging Library

, 17 Apr 2008
Rate this:
Please Sign up or sign in to vote.
A simple yet useful library for file based logging implemented in .NET using C#


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


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
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.


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


Library Version 1.0 - April 05, 2008


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


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHi Performance Logging Pin
ShaneNorman8-May-08 22:09
memberShaneNorman8-May-08 22:09 
AnswerRe: Hi Performance Logging Pin
msvenc9-May-08 8:17
membermsvenc9-May-08 8:17 
I am not sure what exactly your requirements are when you say if this does "high performance text file logging". I can certainly share some of the ideas behind the logger and you can very well decide if this is suitable for your project or not.

1. This logger does not write every single record to the file. In other words, it does not call flush repeatedly for each log entry. It relies on the internal IO stream buffers maintained by the .NET runtime. As soon as the buffer is full .NET will write those records to the file automatically. This technique has pros and cons. Pro being there is no repeated writes to your files. Con being you may miss the buffered entries that were not yet written to the file if the application crashes. When you say high performance, I take it to mean that you are expecting very high logging, in which case, bufferring would be the desired approach.

2. This logger is thread safe. You can call the same logger object from multiple threads within your application.

The logger design is very simple. There is nothing complex going underneath the hood. You can very easily tweak it to suit your needs.
QuestionRe: Hi Performance Logging Pin
Shane Thurai9-May-08 14:10
memberShane Thurai9-May-08 14:10 
GeneralRe: Hi Performance Logging Pin
Shane Thurai22-May-08 22:04
memberShane Thurai22-May-08 22:04 
QuestionCould this log work in multithread? Pin
zjs__200018-Apr-08 1:03
memberzjs__200018-Apr-08 1:03 
AnswerRe: Could this log work in multithread? Pin
zjs__200018-Apr-08 1:06
memberzjs__200018-Apr-08 1:06 
GeneralRe: Could this log work in multithread? Pin
msvenc22-Apr-08 6:53
membermsvenc22-Apr-08 6:53 
QuestionWhat is RTLog Reference in LogClient and Why is it Not found? Pin
RonHat8-Apr-08 7:46
memberRonHat8-Apr-08 7:46 
AnswerRe: What is RTLog Reference in LogClient and Why is it Not found? Pin
RonHat8-Apr-08 9:27
memberRonHat8-Apr-08 9:27 
GeneralRe: What is RTLog Reference in LogClient and Why is it Not found? Pin
msvenc17-Apr-08 8:56
membermsvenc17-Apr-08 8:56 
QuestionComparisons to other logging libraries? Pin
Mike Lang7-Apr-08 4:40
memberMike Lang7-Apr-08 4:40 
AnswerRe: Comparisons to other logging libraries? Pin
msvenc7-Apr-08 7:56
membermsvenc7-Apr-08 7:56 
AnswerRe: Comparisons to other logging libraries? Pin
Alexander Vasilyev8-Apr-08 9:16
memberAlexander Vasilyev8-Apr-08 9:16 
GeneralRe: Comparisons to other logging libraries? Pin
John Camaro11-Apr-08 0:07
memberJohn Camaro11-Apr-08 0: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 17 Apr 2008
Article Copyright 2008 by msvcyc
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid