Close Specified Trace Listener

Topics: Logging Application Block
Feb 11, 2009 at 7:26 PM
I have a situation where my application uses multiple trace listeneners (Console, Rolling File) in a batch format. At the end of the batch process I need to email the Rolling File that was created by the Logging Application Block but I cannot do that without Disposing of the Logger.Writer because it's still holding on to that trace listener. How can I remove a single trace listener that is controlled by the Logging Application Block without having to Dispose of the Logger?
Feb 12, 2009 at 3:48 AM
I believe you could dispose a tracelistener using the Logger class like this:

Logger.Writer.TraceSources["General"].Listeners[0].Dispose();

but requires you to know the index of the tracelistener you wish to dispose.   Why not just copy that file to a new one and use that instead?


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com



 

Feb 12, 2009 at 3:41 PM
I had thought about doing that however the Logger has an exclusive lock on the file so I cannot open it to read the information from it into another file. I did find another way to get around this problem but it's pretty much a hack job until I can find another way around it.
Feb 13, 2009 at 7:39 AM
Hi,

I have tried copying the created file by the Logging App Block to another destination and seem to work. Here is the sample code:

            LogEntry entry = new LogEntry();

            entry.Message = "test";

            entry.Categories.Add("Custom");

            Logger.Write(entry);

 

            FileInfo flatFile = new FileInfo("trace.log");

            FileInfo rollingFile = new FileInfo("rolling.log");

            FileInfo[] files = new FileInfo[] { flatFile, rollingFile };

 

            foreach (FileInfo file in files)

            {

                if (file.Exists)

                {

                    file.CopyTo(@"C:\" + file.Name, true);

                }

            }

 

            Logger.Write("After Copy", "Custom");


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

Feb 13, 2009 at 3:41 PM
You are correct that method should work, I thought you ment to open the file and then copy the contents. I interpreted the suggestion wrong. Thanks