title: How to dynamically set log file using App.config and System.Diagnostics?
body: <p>I was looking for a solution to provide logging to my latest project when I came across an article ( <a href="http://www.daveoncsharp.com/2009/09/create-a-logger-using-the-trace-listener-in-csharp/" rel="nofollow">http://www.daveoncsharp.com/2009/09/create-a-logger-using-the-trace-listener-in-csharp/</a> ) that talked about using System.Diagnostics and App.config to log via the Trace method. I was able to successfully implement both the class and the App.config but I'd really like to be able to dynamically assign the value/location (inside initializeData) of the log file and I don't have a clue how to do it. If you have any suggestions, please feel free to post!</p>
  
  <p>App.config:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;system.diagnostics&gt;
      &lt;trace autoflush="true" indentsize="4"&gt;
        &lt;listeners&gt;
          &lt;add name="myListener" 
               type="System.Diagnostics.TextWriterTraceListener" 
               initializeData="fileSizeThreshold=512, fileSizeUnit=kilobytes, 
               fileAgeThreshold=1, fileAgeUnit=months, fileNameTemplate='{0}\MyApp-{1:MMM-yy}.log'"/&gt;
          &lt;remove name="Default" /&gt;
        &lt;/listeners&gt;
      &lt;/trace&gt;
    &lt;/system.diagnostics&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Logger Class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Text;
  
  
  namespace MyCurrentProject
  {
      class Logger
      {
          public void Error(string module, string message)
          {
              WriteEntry(message, "ERROR:", module);
          }
  
          public void Error(Exception ex, string module)
          {
              WriteEntry(ex.Message, "ERROR:", module);
          }
  
          public void Warning(string module, string message)
          {
              WriteEntry(message, "WARNING:", module);
          }
  
          public void Info(string module, string message)
          {
              WriteEntry(message, "INFO:", module);
          }
  
          private void WriteEntry(string message, string type, string module)
          {
              Trace.WriteLine(
                      string.Format("{0} {1} [{2}] {3}",
                                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                    type,
                                    module,
                                    message));
          }
      }
  }
  </code></pre>
  
  <p><strong>RE:</strong> Sorry for not being clear... just to be clear, I need the file path that the log file output is save to to be dynamically set. I'd like the path to save to a location in %AppData%. The problem I had with the config was that once I set the value for 'initializeData' I couldn't find a way to change or dynamically set/reset that value. Truthfully... at this point I just want a solution that works and allows me to manage the location of the log file.</p>