Mult-Threading and TraceListeners

  • Thread starter Thread starter Stewart Berman
  • Start date Start date
S

Stewart Berman

I have a simple test application that uses a BackgroundWorker class.

In looking at the trace.listeners collection I noticed that by default there is only one and it is
not thread safe.

Does that mean there could be a problem if I have Trace.WriteLine statements in both the GUI
foreground task and the BackgroundWorker task?

If I define a trace listener that writes to a file and add it to the tracelisteners collection
during the load event (main GUI thread) what happens when I do a Trace.WriteLine in the background
worker thread?

Is there an example of tracing in a multi-threaded environment?
 
Hi Stewart,

The Trace class itself and the DefaultTraceListener class are both thread
safe, according to the document (the Thread Safty section):
http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.defaulttraceliste
ner.aspx

And if you added any non-ThreadSafe listeners to the Trace.Listeners, the
Trace will always use a global lock to ensure thread safety.
The Trace class uses the TraceListener.IsThreadSafe property to determine
if the listener is thread safe.

For my understanding of what you said "In looking at the trace.listeners
collection I noticed that by default there is only one and it is not thread
safe.", yes, the TraceListenerCollection class itself is not guaranteed to
be thread safe, however, that *only* means you don't add/remove listeners
from more than one thread without any protection.

To conclude, for any calls to Trace class itself to log anything, you don't
need to worry about thread safety, the Trace class takes care of it for you
so you can just focus on your task.

If you want to force the Trace class to use a global lock, you can set the
Trace.UseGlobalLock property or write the setting in app.config file:
http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.usegloballo
ck.aspx

Hope this makes it clear.

If you have any further questions, please don't hesitate to let me know.

Thanks,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
For my understanding of what you said "In looking at the trace.listeners
collection I noticed that by default there is only one and it is not thread
safe.", yes, the TraceListenerCollection class itself is not guaranteed to

I was looking at the only member of the TraceListenerCollection not the collection itself:
Trace.Listeners(0).IsThreadSafe was false. Does that mean it uses a global lock?
 
Does that mean it uses a global lock?

According to the document, yes.

According to the actual implementing code, also yes. It will first check
the UseGlobalLock property, if that's true, a lock will always be used; if
that's false, then it will check each listener's IsThreadSafe property, if
not thread safe, it will lock on the listener.

Regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top