Global Logger instance
-
I've always passed around a reference to an instance of a Logger in most apps that I've developed. There has to be a better way. What do you guys do? I'd like to make a single instance that can be used anywhere in the program. Should I be considering a Singleton design pattern.... ??? I don't need anything fancy. All I need to do is: MyLogger.Log("Message"); Is it possible to make an instance global to a namespace?
-
I've always passed around a reference to an instance of a Logger in most apps that I've developed. There has to be a better way. What do you guys do? I'd like to make a single instance that can be used anywhere in the program. Should I be considering a Singleton design pattern.... ??? I don't need anything fancy. All I need to do is: MyLogger.Log("Message"); Is it possible to make an instance global to a namespace?
static
methods :) leppie::AllocCPArticle(Generic DFA State Machine for .NET); -
I've always passed around a reference to an instance of a Logger in most apps that I've developed. There has to be a better way. What do you guys do? I'd like to make a single instance that can be used anywhere in the program. Should I be considering a Singleton design pattern.... ??? I don't need anything fancy. All I need to do is: MyLogger.Log("Message"); Is it possible to make an instance global to a namespace?
Write a logger class that has a single static method: public class Logger { static public Log(string msg); } Then you would call the Log method like this: Logger.Log("Sent a message"); Of course the logger class would have a bunch of private methods and classes underneath to use a logging instance of some type that does the work. An alternative is to write a classic Singleton. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
-
I've always passed around a reference to an instance of a Logger in most apps that I've developed. There has to be a better way. What do you guys do? I'd like to make a single instance that can be used anywhere in the program. Should I be considering a Singleton design pattern.... ??? I don't need anything fancy. All I need to do is: MyLogger.Log("Message"); Is it possible to make an instance global to a namespace?
Inherit a class from System.Diagnostics.TraceListener. On application start, add an instance of this class to the System.Diagnostics.Trace.Listeners and/or System.Diagnostics.Debug.Listeners collections. Every time you use Trace.Writexxx or Debug.Writexxx, it will be logged through your class. If you change/expand your logging facilities at some time, you don't have to change any client code, just your TraceListener.