title: Lock in Singleton + Builder pattern in ASP.NET. Make builder pattern safe threaded
body: <p>I am facing a strange situation that is driving me crazy.
  I have a Class <strong>MyApplication.cs</strong></p>
  
  <pre><code>public class MyApplication 
  {
      private static volatile MyApplication _instance;
      private static object syncRoot = new Object();
      private Logger logger=new Logger();
  
      static public MyApplication Instance
      {
          get
          {
              if (_instance == null)
              {
                  lock (syncRoot)
                  {
                      if (_instance == null)
                          _instance = new MyApplication ();
                  }
              }
              return _instance;
  
          }
  
        public void Load(){
           logger.setId("100").setMessage("Load succesful").Write();
        }
  
  
  }
  </code></pre>
  
  <p>It has a singleton pattern.</p>
  
  <p>Also  I have a <strong>Logger.cs</strong> class with Builder pattern:</p>
  
  <pre><code>    public class Logger
      {
           private string Id="NOT_SET";
           private string Message;
  
           public Logger setId(int id){
               this.Id=id;
           }
           public Logger setMessage(string message){
               this.Message=message;
           }
           public void Write(){
                if(Id="NOT_SET"){
                  throw Exception("NOT SET EXCEPCION");  
                }
                //write output staff
           }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Problem</strong>: Every page load of my ASP.NET app, the MyApplication Load() method gets called. So a new Logger object is created every request.</p>
  
  <pre><code>Page_load(){
     MyApplication.Instance.Load();
  }
  </code></pre>
  
  <p>The fact is that i am getting sometimes the NOT_SET_EXCEPTION.
  It might be for some concurrent accesing to the Logger variable in MyApplication cause it gets reseted by a reason i cannot understand.</p>
  
  <ul>
  <li>What do you think? </li>
  <li><p>How can i make safe threaded the Logger variable?</p></li>
  <li><p>Is a bad idea to use builder patter in this case?</p></li>
  </ul>
  
  <p>It would be desirable to lock the creational procces until it ends, because if not logger variable gets modified from different threads..
  Please help</p>