.NET Trace File

The behavior of the default trace listner text file allows read access to the file while the listner is active and writting to the file.  For a custom trace listner created with TexWriterTraceListner how do we allow read access to the file without removing it from the Trace Listners collection?
sclamersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DaveJellisonCommented:
You can completely subvert the behavior of the TraceListener. A proper implementation would hold the FileStream as a private variable and implement the IDisposable pattern to release the stream resource. This quick example should get you started down the right path. You'll have to overload each method you're using via Trace.WriteXYZ(). If this isn't going into a shared class library, just hide the inherited members of the base class that you're not currently using so that you don't hurt yourself in the future by call a Trace.Write method that you haven't implemented. Or, of course, implement every base member for comprehensiveness would be the best approach.



using System.Diagnostics;
using System.IO;

namespace ConsoleTestBed
{
	public class FileSharingTraceListner : TextWriterTraceListener
	{
		private readonly string _path;

		public FileSharingTraceListner(string path)
		{
			_path = path;
		}

		public override void WriteLine(object o)
		{
			using (var stream = new FileStream(_path, FileMode.Create, FileAccess.Write, FileShare.Read))
			using (var writer = new StreamWriter(stream))
			{
				writer.WriteLine(o);
			}
		}
	}
}


// USAGE EXAMPLE

using System.Diagnostics;

namespace ConsoleTestBed
{
	public class Program
	{
		public static void Main()
		{
			var listener = new FileSharingTraceListner(@"C:\output.trace");
			Trace.Listeners.Add(listener);

			Trace.WriteLine("You can still read this file while this is being written");
		}
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.