I have a logger API that I can use to log to text. The logger has some properties that I can use to record envent types (Info, Err, etc...), source app (app1, app2, etc...) and mesg text.
The syntax is simple and is working OK:
Logger log = new Logger();
log.Write("Information", "SourceApplication", "Test text to log");
Now I m thinking creating 2 static classes, one for "General Purpose Logging" and other for "Debug Logging". The goal is to avoid to create a new instance of the logger object for each module utilization. Each class should be able to use these objects without instantiation (the static classes should take care of that automatically).
"Debug Logging" - Can be used by any project inside the solution and should be a singleton. The Caller Code to use should be:
LoggerDebug.Write("Debug", "Debugger", "Test text to log");
General Purpose Logging - To be used by all project modules but after each utilization the static class should dispose the Logger Object The Caller Code to use should be:
LoggerDebug.Write("Information", "App1", "Test text to log");
I tried to start with the "Debug Logging" static class, I read (http://csharpindepth.com/Articles/General/Singleton.aspx) but I`m not sure if this is the correct way to do this... Can you give me some suggestions?
using System;
using System.Diagnostics;
using System.Collections.Generic;
namespace Common
{
public sealed class LoggerDebug
{
private static LoggerDebug instance = null;
private static readonly object padlock = new object();
private static Logger log;
static LoggerDebug Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new LoggerDebug();
}
return instance;
}
}
}
public LoggerDebug()
{
log = new Logger();
}
public static void Write(String EventType, string appSource, string text)
{
log.Write(EventType, appSource, string.Format("Test {0}", text));
}
}
}
The caller code appears like this:
LoggerDebug.Write("Information", "App1", "Test text to log");
When I try to run the application it crashes with:
An unhandled exception of type 'System.NullReferenceException' occurred in Common.dll Additional information: Object reference not set to an instance of an object.
That said the ctor is not instantiating the log = new Logger();
So I can correct this problem with:
private static Logger log = new Logger();
And commenting the ctor code
public LoggerDebug()
{
//log = new Logger();
}
But I m confused, not sure if this is the correct way to do this, How to do this correctly?
Any examples that I can use?