Logging using System.Diagnostics.Trace is creating multiple files when writing
<p><img src="http://i.stack.imgur.com/pG3nv.png" alt="enter image description here"><img src="http://i.stack.imgur.com/xjTmR.png" alt="enter image description here">I am using the Write Method which accept message to write and a type category value which is used in the switch case statement to make filepath depending on the type passed in the method.</p>

<p>When I run the application using wcftestclient.exe for the first time, the app creates two files rawxmllog.txt and errorlog.txt(to log email send failure as smtp is not configured on my local system), results are satisfactory. But if I again invoke the service using wcftestclient.exe, application is creating multiple rawsmllog.txt and it renames to 7b000600-8c9f-49d2-9392-1270f5eb4109RequestRawXml.txt(Prefix changes everytime). But it is not creating multiple files for errorlog.txt. Without stopping the application, system allows me to delete errorlog.txt but not rawxmllog.txt and says that </p>

<blockquote>
  <p>The action can't be completed because the file is open in WebDev.WebServer40.exe.</p>
</blockquote>

<p>I am not able to figure out that the same method is written for both the files but it is only creating problem for rawxmllog.txt. The media is still in use, though I am closing it properly.</p>

<pre><code>public static void Write(string logMessage, string loggingtype)
        {
            string fileName = null;
            string filePath = null;
            switch (loggingtype)
            {
                case "rawxmllog":
                            //Logic to store the input XML in to flat files. Write logic as instructed -- i.e on file for all or one file for each xml or one file per day.
                            fileName = ConfigurationManager.AppSettings["RawXmlFileName"] + ConfigurationManager.AppSettings["RawXmlFileExtenstion"];
                            filePath = ConfigurationManager.AppSettings["RawXmlFilePath"] + fileName;
                            break;
                case "errorlog":
                            fileName = ConfigurationManager.AppSettings["ErrorLoggingFileName"] + ConfigurationManager.AppSettings["ErrorLoggingFileExtension"];
                            filePath = ConfigurationManager.AppSettings["ErrorLoggingFilePath"] + fileName;
                            break;
                case "warninglog":
                            fileName = ConfigurationManager.AppSettings["WarningLoggingFileName"] + ConfigurationManager.AppSettings["WarningLoggingFileExtension"];
                            filePath = ConfigurationManager.AppSettings["WarningLoggingFilePath"] + fileName;
                            break;

            }
            try
            {
                using (TextWriterTraceListener textWriterTraceListener = new TextWriterTraceListener(filePath))
                {

                    Trace.Listeners.Clear();
                    Trace.Listeners.Add(textWriterTraceListener);
                    Trace.Write(Environment.NewLine + logMessage + Environment.NewLine);
                    Trace.Close();
                    Trace.Flush();
                    textWriterTraceListener.Close();
                    textWriterTraceListener.Dispose();


                }
            }
            catch(Exception e) {
                string msg = e.Message;
            }
        }   `
</code></pre>
