Log files still in use after loggin

Topics: Logging Application Block
Dec 23, 2010 at 3:11 PM

Hi,

this code:

LogWriter writer = null;

            ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();
                
            builder
                .ConfigureLogging()
                .LogToCategoryNamed("UnitTest").WithOptions.ToSourceLevels(SourceLevels.All)
                .SendTo
                    .FlatFile("FlatFile").ToFile(@"d:\Temp\test1.txt").Filter(SourceLevels.Information)
                .SendTo
                    .XmlFile("XmlFile").ToFile(@"d:\Temp\test1.xml").Filter(SourceLevels.Error);
            
            //Create new source
            DictionaryConfigurationSource configurationSource = new DictionaryConfigurationSource();

            //Replace the old configuration with the new one
            builder.UpdateConfigurationWithReplace(configurationSource);

            //Update the current container
            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configurationSource);

            //Update the writer object with the current instance of the LogWriter
            writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

            writer.Write("Hallo""UnitTest", 0, 0, TraceEventType.Warning);

            File.OpenText(@"d:\Temp\test1.xml").ReadToEnd();

throws an exception System.IO.IOException: The process cannot access the file 'd:\Temp\test1.xml' because it is being used by another process.

How can this be avoided?

Best regards,
Peter

 

Dec 26, 2010 at 12:31 AM
Edited Dec 26, 2010 at 12:33 AM

You can open the file using the code below:

using(FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
        using(StreamReader reader = new StreamReader(fs))
        {
               string log =  reader.ReadToEnd();
        }
}

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com