Reputation: 623
I'm doing an applicaton that has different classes/objects.
I would like to print all log messages to the same Logger-Object (that handles all the file-open/close).
To be more accuate: It is not about the Logging-Class itself. It's more about how would I create one singel Logging-Object that is available in all other objects/classes in my app. In an efficient way (not passing my logger as parameter to all new created objects).
What would be the best way to do so?
Upvotes: 1
Views: 3857
Reputation: 1690
It fully depends on what you want it for really, if it was only for logging exceptions then I would have an application wide exception handler (dependant upon what type of application) and then log from there. However I think you're after a mechanism for logging anything from anywhere in the application, there are many libraries that do this but if you want to roll your own your could do something like:
public sealed class Logging
{
// Constructor is lazily called.
Logging()
{
//Setup reference to logging file.
}
public static Logging Instance
{
get
{
return Nested.instance;
}
}
/// <summary>
/// Writes debug information.
/// </summary>
/// <param name="text">The text to write.</param>
public void Debug(String text)
{
//Log to file
}
/// <summary>
/// Writes warning message.
/// </summary>
/// <param name="text">The message to write.</param>
public void Warn(String text)
{
//Log to file
}
/// <summary>
/// Writes error message.
/// </summary>
/// <param name="text">The message to write.</param>
public void Error(String text)
{
//Log to file
}
/// <summary>
/// Writes error message.
/// </summary>
/// <param name="text">The message to write.</param>
/// <param name="e">An inner exception.</param>
public void Error(String text, Exception e)
{
//Log to file
}
/// <summary>
/// Private class to hold the instance of the singleton.
/// </summary>
class Nested
{
static Nested()
{
}
internal static readonly Logging instance = new Logging();
}
}
And then to use :
Logging.Instance.Debug("Hello World");
Upvotes: 0
Reputation: 9209
I would suggest that whether you use an already existant library or write your own, that you run it in a separate application process to your main application, although it can be started by your own application, and send it data to be logged.
There are several reasons for doing it this way not least being that the actual writing of data to the log file won't have such a great impact on your own application, but more importantly you stand more of a chance of recording exceptions that cause your application to crash and it won't hold up application closure if it hasn't finished logging data.
Upvotes: 0
Reputation: 18013
I would create a single class with a static method to handle all logging.
This can then be called from anywhere and gives a single class to maintain.
/// <summary>
/// Log class
/// </summary>
public class Log
{
/// <summary>
/// Writes an entry to the log
/// </summary>
/// <param name="message">The message to Write</param>
public static void WriteToLog(string message)
{
//Write your log code here
}
}
/// <summary>
/// Test class
/// </summary>
public class Test()
{
/// <summary>
/// Constructor
/// </summary>
public Test()
{
Log.WriteToLog("Test constructor called");
}
}
Upvotes: 0