title: C# Singleton Logging Class
body: <p>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:</p>
  
  <pre><code>Util.Logger.GetInstance().LogAccess(str);
  </code></pre>
  
  <p>Or something like that.</p>
  
  <p>My class looks like this:</p>
  
  <pre><code>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);
     }
  }
  </code></pre>
  
  <p>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.</p>