Click here to Skip to main content
5,787,682 members and growing! (18,229 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Utilities     Beginner License: The Code Project Open License (CPOL)

.NET File Logging Library

By msvcyc

A simple yet useful library for file based logging implemented in .NET using C#
C#, Windows, .NET, Win32, Win64, Visual Studio, Architect, Dev, Design

Posted: 4 Apr 2008
Updated: 17 Apr 2008
Views: 7,010
Bookmarked: 8 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 0.60 Rating: 1.25 out of 5
2 votes, 66.7%
1
1 vote, 33.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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

About the Author

msvcyc



Location: United States United States

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
QuestionHi Performance LoggingmemberShaneNorman22:09 8 May '08  
AnswerRe: Hi Performance Loggingmembermsvenc8:17 9 May '08  
QuestionRe: Hi Performance LoggingmemberShane Thurai14:10 9 May '08  
GeneralRe: Hi Performance LoggingmemberShane Thurai22:04 22 May '08  
GeneralCould this log work in multithread?memberzjs__20001:03 18 Apr '08  
GeneralRe: Could this log work in multithread?memberzjs__20001:06 18 Apr '08  
GeneralRe: Could this log work in multithread?membermsvenc6:53 22 Apr '08  
QuestionWhat is RTLog Reference in LogClient and Why is it Not found?memberRonHat7:46 8 Apr '08  
AnswerRe: What is RTLog Reference in LogClient and Why is it Not found?memberRonHat9:27 8 Apr '08  
GeneralRe: What is RTLog Reference in LogClient and Why is it Not found?membermsvenc8:56 17 Apr '08  
GeneralComparisons to other logging libraries?memberMike Lang4:40 7 Apr '08  
GeneralRe: Comparisons to other logging libraries?membermsvenc7:56 7 Apr '08  
AnswerRe: Comparisons to other logging libraries?memberAlexander Vasilyev9:16 8 Apr '08  
GeneralRe: Comparisons to other logging libraries?memberJohn Camaro0:07 11 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Apr 2008
Editor:
Copyright 2008 by msvcyc
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project