Click here to Skip to main content
Licence CPOL
First Posted 29 Mar 2008
Views 14,137
Downloads 107
Bookmarked 40 times

MiniLogger

By | 29 Mar 2008 | Article
Lightweight C# Logger

Introduction

Quite simplistically, it is a lightweight logging API. If this is what you are looking for, read ON.

Yes there are tons of Logging tools out there and well most people either use one of the freely available ones (NLog, Log4Net, etc.) or write something ad-hoc for themselves; which is most times project specific as opposed to extrapolating the side-effects of Object-Orientated Programming’s (OOP) Model by having a re-usable CLASS.

So What is MiniLogger?

It is a simple lightweight logging API that aims to be as small as possible, while still remaining functional. The keyword here is lightweight; and in there lies ‘Chimera’ (dual-nature/dual-genetics). One to be fast and efficient; the second to be as small in size as possible (11Kb).

Why is file size important?

It is because sometimes you need to have logging capability in an existing application which is redistributable and which needs to be of smallest possible size for mass rollouts using Active Directory or something alike.

What does it give me in terms of Features?

  1. Logging to FILE or to EVENTLOG
  2. CLR Exceptions Supported
  3. Custom Error Logging
  4. Custom Log File Ceiling (empties the log file when ceiling reached)
  5. Configuration File Support
  6. Constructor Overload for Custom Configuration File or not
  7. Configuration File Override by using Custom Source Name
  8. Self-sustaining Error Logging for DLL itself
  9. COM-visible
  10. Well-documented source code (yes, it matters)

So How Can I Use It?

I was intending on using Unit Testing for this part, but I often find bringing other technologies into a tutorial (if we can call this that), adding layers of simplicity which spawn into complexity and a further broader array of understanding to implement something quickly and easily. I prefer just downloading software from the internet, clicking, installing and implementing; documentation can come later – yes I am lazy.

Quite simply, add a reference to the DLL into your existing project:

image001.png

The MiniLogger API should be available now in your project and should be accessible via Visual Studio’s IntelliSense.

Let's delve into the code, shall we?

// MiniLogger instantiated without a configuration file.
MiniLogger.Logger test = new MiniLogger.Logger();

// MiniLogger instantiated with configuration file
MiniLogger.Logger test = new MiniLogger.Logger("c:\\MiniLogger.config"); 

In the first section of code, MiniLogger is being instantiated for use. However, because no configuration file is being specified, it will use the default log file for all its error logging. However all configurations both default or specified will be overwritten if a SOURCE NAME is specified; more on this later.

The second section of code instantiates a custom application configuration file, which must be specified with location. The semantics of the Configuration with explanations are below:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!--Applications Name -->
<add key="ApplicationTitle" value="Dane's MiniLogger" />
<!--Header to Log All Messages With -->
<add key="ApplicationHeaderText" value="DLog" />
<!-- Supported Types (Application, System, Text) -->
<add key="LogType" value="Application" />
<!-- Level of Information -->
<add key="InformationType" value="Debug" />
<!-- Log Location -->
<add key="TextLogLocation" value="Dlog.log" />
<!-- Maximum Text Log Size before Cleanup -->
<add key="MaxTextLogSize" value="1024" />
<!-- API Logging -->
<add key="MiniLoggerLog" value="c:\\minilogger.log" />
</appSettings>
</configuration><

Now let's get Logging:

// Basic logging to location specified by configuration file
test.Error("Just log me now()");
// Advanced logging with passed in CLR Exception Type
test.ErrorException("Just log me now()", new OutOfMemoryException());
// Alternatively how it can be implemented
try
{
throw new Exception();
}
catch (Exception e)
{
test.ErrorException("Just log me now()", e);
}

From the above code samples, it’s pretty simple as you can see how quickly you can start dumping information on your project. In the first code section, we are just calling the Error method with a custom string.

In the second code section is the Logging capability with CLR Exception Types. Now that’s the meat of it, with one exception. Take a walk with me……..

image003.png

Now what we have here is the ability to override all existing configurations and log to a location of our choice; thus allowing the capability to log to multiple log files for different context types. The additive to this is that the application name you specify will result in the API logging to a file of that name. For example:

test.Error("Just log me now()", "codeprojectexample");

This would create a text file codeprojectexample.log and immediately start logging to it.

Points of Interest

If you require a basic logging facility that’s extremely lightweight while not retaining a lot of the robust functionality that exists in most of the Logging APIs out there, then this is a pretty good choice.

History

  • 29th March, 2008: Initial post

License

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

About the Author

Dane-Garrin Balia

Software Developer (Senior)
Sourcing
South Africa South Africa

Member

Passionately pursuing the understandings of the unimaginable!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCan't find the text log file Pinmembermgaert12:24 11 Feb '10  
GeneralRe: Can't find the text log file Pinmembermgaert9:16 14 Feb '10  
GeneralWell done! PinmemberJ Snyman19:16 30 Mar '08  
GeneralRe: Well done! PinmemberDane-Garrin Balia10:52 31 Mar '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 29 Mar 2008
Article Copyright 2008 by Dane-Garrin Balia
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid