Programatic creation of FlatFileTrace creates multiple files

Topics: Logging Application Block
Aug 11, 2009 at 6:52 PM

All...

Have a need to create our flat or rolling fileTraceListeners at runtime due to environment issues. I've created the following code which does produce a file, however, every time the btnLogIt_Click event fires, it wants to create a new file. Understandable, however what the desired affect would be, is to have it simply reuse the file and append the text. 

Anyone have any tips on this?

Thanks in advance..

Rick

 

protected void btnLogIt_Click(object sender, EventArgs e)
        {
            LogWriter                       logWriter;
            LogSource                       logSource;
            LogSource                       nonExistantLog;
            TextFormatter                   textFormatter;
            RollingFlatFileTraceListener flatFileTraceListener;
            FormattedEventLogTraceListener  eventLogListener;
            IDictionary<string, LogSource>  traceSources;


            try
            {
                textFormatter = new TextFormatter("Timestamp: {timestamp}{newline}" + "Message: {message}{newline}" + "Category: {category}{newline}" + "Machine: {machine}{newline}");
                
                 flatFileTraceListener = new RollingFlatFileTraceListener("myLogFile.log", "====HEADER====", "====FOOTER====", textFormatter, 500, "dd/mm/yyyy", RollFileExistsBehavior.Increment, RollInterval.Week);
                
                eventLogListener = new FormattedEventLogTraceListener("Enterprise Library Logging", textFormatter);

                logSource = new LogSource("MainLogSource", SourceLevels.All);
                nonExistantLog = new LogSource("Empty");
                traceSources = new Dictionary<string, LogSource>();

                logSource.Listeners.Add(flatFileTraceListener);
                logSource.Listeners.Add(eventLogListener);

                traceSources.Add("Error", logSource);
                traceSources.Add("Debug", logSource);

                logWriter = new LogWriter(new ILogFilter[0],
                                            traceSources,
                                            nonExistantLog,
                                            nonExistantLog,
                                            logSource,
                                            "Error",
                                            false,
                                            true);

                LogEntry entry = new LogEntry();
                entry.Message = txtMessage.Text;
                entry.Categories.Add("Debug");
                logWriter.Write(entry);
            }
            catch (Exception ex)
            {
                txtMessage.Text = ex.Message;
            }
        }
    }

Aug 12, 2009 at 6:03 AM
Edited Aug 12, 2009 at 6:11 AM

Hi,

I would assume that the other created file is the ones that are prefixed with guid? is that right? That specific behavior is defined by the TextWriterTraceListener class (http://msdn.microsoft.com/en-us/library/system.diagnostics.textwritertracelistener.aspx) where the Rolling Flat File inherits from. In the link I provided, you can find the note that states "If an attempt is made to write to a file that is in use or unavailable, the file name is automatically prefixed by a GUID.". Imagine a scenario that you have 2 file based trace listener which points to the same file. If one of the trace listener logs to the file, it will keep a lock to that file. So if the other trace listener tries to log to the same file that has been locked by the first trace listener, this will result to the creating of file prefixed with guid. Anyways, The solution to your problem is to explicity dispose the logWriter after logging to the lock to the file wont be kept. add the highlighted line in your code.

 

protected void btnLogIt_Click(object sender, EventArgs e)
        {
            LogWriter                       logWriter;
            LogSource                       logSource;
            LogSource                       nonExistantLog;
            TextFormatter                   textFormatter;
            RollingFlatFileTraceListener flatFileTraceListener;
            FormattedEventLogTraceListener  eventLogListener;
            IDictionary<string, LogSource>  traceSources;


            try
            {
                textFormatter = new TextFormatter("Timestamp: {timestamp}{newline}" + "Message: {message}{newline}" + "Category: {category}{newline}" + "Machine: {machine}{newline}");
                
                 flatFileTraceListener = new RollingFlatFileTraceListener("myLogFile.log", "====HEADER====", "====FOOTER====", textFormatter, 500, "dd/mm/yyyy", RollFileExistsBehavior.Increment, RollInterval.Week);
                
                eventLogListener = new FormattedEventLogTraceListener("Enterprise Library Logging", textFormatter);

                logSource = new LogSource("MainLogSource", SourceLevels.All);
                nonExistantLog = new LogSource("Empty");
                traceSources = new Dictionary<string, LogSource>();

                logSource.Listeners.Add(flatFileTraceListener);
                logSource.Listeners.Add(eventLogListener);

                traceSources.Add("Error", logSource);
                traceSources.Add("Debug", logSource);

                logWriter = new LogWriter(new ILogFilter[0],
                                            traceSources,
                                            nonExistantLog,
                                            nonExistantLog,
                                            logSource,
                                            "Error",
                                            false,
                                            true);

                LogEntry entry = new LogEntry();
                entry.Message = txtMessage.Text;
                entry.Categories.Add("Debug");
                logWriter.Write(entry);
	//dispose the logwriter explicitly
	logWriter.Dispose();
            }
            catch (Exception ex)
            {
                txtMessage.Text = ex.Message;
            }
        }
    }

 Please see if this solution works for you.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Aug 12, 2009 at 3:50 PM

THANK YOU... PERFECT....

It now works like I was hoping and expecting.  :)