I am trying to figure out the best strategy for logging on the async-IO web server I am working on. I thought the easiest way was to have a singleton class that keeps Filestreams open for the appropriate log files so I could just do something like:
Util.Logger.GetInstance().LogAccess(str);
Or something like that.
My class looks like this:
public sealed class Logger {
private static StreamWriter sw;
private static readonly Logger instance = new Logger();
private Logger() {
sw = new StreamWriter(logfile);
}
public static Logger GetInstance() {
return instance;
}
public void LogAccess(string str) {
sw.WriteLine(str);
}
}
This is all just in my head really, and I'm looking for suggestions on how to make it better and also make sure I'm doing it properly. The biggest thing is I need it to be thread safe, which obviously it is not in its current state. Not sure of the best way to do that.