title: File locks using ETW and Semantic Logging Application Block
body: <p>We have created a very simple implementation of the ETW EventSource that is consumed by the Semantic Logging Application Block (via RollingFlatFileLogs).</p>
  
  <p>The basic setup is as follows:</p>
  
  <pre><code>[EventSource(Name = "My Applicaiton")]
  public partial class MyEventSource : EventSource
  {
      /// &lt;summary&gt;
      /// The log
      /// &lt;/summary&gt;
      public static MyEventSource Log = new MyEventSource();
  </code></pre>
  
  <p>...</p>
  
  <p>This then has the basic keywords, tasks, and opcodes. </p>
  
  <p>The files are written to by events that look like:</p>
  
  <pre><code>/// &lt;summary&gt;
      /// Application starting.
      /// &lt;/summary&gt;
      [Event(100, Level = EventLevel.Verbose, Keywords = Keywords.Application, Task = Tasks.AppStart, Opcode = Opcodes.Start, Message = "Application Starting")]
      public void ApplicationStarting()
      {
          if (this.IsEnabled(EventLevel.Verbose, Keywords.Application))
          {
              this.WriteEvent(100);
          }
      }
  </code></pre>
  
  <p>We subscribe using this method:</p>
  
  <pre><code>var listener = Microsoft.Practices.EnterpriseLibrary.SemanticLogging.RollingFlatFileLog.CreateListener("C:\\ServiceRegistry\\services.log", 5120, null, Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks.RollFileExistsBehavior.Increment, Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks.RollInterval.None, null, 10, true);
  
          listener.EnableEvents(MyEventSource.Log, System.Diagnostics.Tracing.EventLevel.LogAlways);
  
  
          // Log some things
          MyEventSource.Log.ApplicationStarting();
  </code></pre>
  
  <p>However, the issue that we have seen is that some developers are seeing locks the log file that stops execution. A message to the tune of "the file is in use and cannot be written to" is shown. </p>
  
  <p>Why is this happening? Why does it end the process even though logging is running asynchronously? </p>
  
  <p>How can we prevent this from happening in the future? Is there a way to harden our logging implementation so that it can roll the file if there is an error such as this one?</p>
  
  <p>Thanks!</p>
  
  <p>Edit: I have captured a stack trace of the issue:</p>
  
  <pre><code> The process cannot access the file 'C:\ServiceRegistry\services.log' because it is               being used by another process.
  
      at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
      at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
      at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
      at System.IO.FileInfo.Open(FileMode mode, FileAccess access, FileShare share)
      at Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks.RollingFlatFileSink..ctor(String fileName, Int32 rollSizeKB, String timestampPattern, RollFileExistsBehavior rollFileExistsBehavior, RollInterval rollInterval, Int32 maxArchivedFiles, Boolean isAsync)
      at Microsoft.Practices.EnterpriseLibrary.SemanticLogging.RollingFlatFileLog.CreateListener(String fileName, Int32 rollSizeKB, String timestampPattern, RollFileExistsBehavior rollFileExistsBehavior, RollInterval rollInterval, IEventTextFormatter formatter, Int32 maxArchivedFiles, Boolean isAsync)
  </code></pre>