Tracing/Logging to a text file question

N

Natalia DeBow

Hi,

I am using .NET provided Trace class for tracing purposes in my code:
[code snippet]
serverLogFile = new FileStream("C:\\Temp\\log.txt", FileMode.Append,
FileAccess.Write, FileShare.ReadWrite);

TextWriterTraceListener serverTraceListener = new
TextWriterTraceListener(serverLogFile);

if (!System.Diagnostics.Trace.Listeners.Contains(serverTraceListener))

{

System.Diagnostics.Trace.Listeners.Add(serverTraceListener);

}

........

and in another method I have the follwoing code:

........

try

{

System.Diagnostics.Trace.WriteLine(Format(moduleName, className,
functionName, errorMessage));

System.Diagnostics.Trace.Flush();

}

catch(IOException ioe)

{

EventLogEntryType type = EventLogEntryType.Warning;

EventLog("Exception logging information into a log file" + ioe.Message,
type);

}

catch (Exception e)

{

EventLogEntryType type = EventLogEntryType.Warning;

EventLog("Exception logging information into a log file" +
e.StackTrace.ToString(), type);

}

I have a question with writing to a TextWriter Trace Listener. If I have
multiple simultaneous writes to the same trace listener, would it preserve
data integrity? I'm having trouble simulating this scenario and I am
wondering if I need to lock the FileStream before I do Trace.WriteLine().
How is Trace.Write()/WriteLine() is actually implemented? Does it block
while the main thread is writing to the file or does it do it
asynchronously?



Thanks so much in advance,

Natalia
 
R

Ravichandran J.V.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top