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?
- Logging to FILE or to EVENTLOG
- CLR Exceptions Supported
- Custom Error Logging
- Custom Log File Ceiling (empties the log file when ceiling reached)
- Configuration File Support
- Constructor Overload for Custom Configuration File or not
- Configuration File Override by using Custom Source Name
- Self-sustaining Error Logging for DLL itself
- COM-visible
- 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:
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.Logger test = new MiniLogger.Logger();
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:
="1.0" ="utf-8"
<configuration>
<appSettings>
-->
<add key="ApplicationTitle" value="Dane's MiniLogger" />
-->
<add key="ApplicationHeaderText" value="DLog" />
-->
<add key="LogType" value="Application" />
-->
<add key="InformationType" value="Debug" />
-->
<add key="TextLogLocation" value="Dlog.log" />
-->
<add key="MaxTextLogSize" value="1024" />
-->
<add key="MiniLoggerLog" value="c:\\minilogger.log" />
</appSettings>
</configuration><
Now let's get Logging:
test.Error("Just log me now()");
test.ErrorException("Just log me now()", new OutOfMemoryException());
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……..
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