I am trying to find the optimal registration strategy for the async-IO web server I'm working on. I thought the easiest way is to have one classmate who keeps Filestreams open to the corresponding log files, so I could just do something like:
Util.Logger.GetInstance().LogAccess(str);
Or something like that.
My class is as follows:
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); } }
Itβs all in my head, and Iβm looking for suggestions on how to do it better, and also make sure that I am doing it right. Most importantly, I need it to be thread safe, which is clearly not in its current state. Not sure how to do this.
c # thread-safety logging singleton
The.Anti.9
source share