Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 437785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:36:47+00:00 2026-05-12T20:36:47+00:00

I have something wich looks to me like race condition while logging to file

  • 0

I have something wich looks to me like race condition while logging to file from
multiple thread.

1) I’ve got a custom logger class (ConfigurableTraceLogger) that is shared by multiple threads in my application.
It’s has lots of wrapper functions which all call to main core function

protected void TraceData(String category, TraceEventType type, EventId id, string prefix, string format)
{
    foreach (TraceListener item in _listeners)
    {
        IConfigurableTraceListener cl = item as IConfigurableTraceListener;

        if (cl != null && cl.Category == category.ToLower())
        {

            if (DisplayMethodName)
                item.TraceData(new TraceEventCache(), _instanceName, type, (int)id, prefix + format);
            else
                item.TraceData(new TraceEventCache(), _instanceName, type, (int)id, format);

            item.Flush();
        }
    }
}

As you can see my class simply stores different TraceListner-derved class in a collection
_listeners. Basically there are only console and text file listeners. What TraceData does it takes the category name (i.e. logging start-up) and finds the right listener. All listeners are defined by config file name

Now I also have my custom listeners in the collection

public class ConfigurableTextWriterTraceListener : TextWriterTraceListener, IConfigurableTraceListener

That custom class overrides nothing except for one property.

protected override string[] GetSupportedAttributes()
{
    return new string[] { "category" };
}

When I start my application after 5 to 10 minutes I get exception
on the call

           item.TraceData(new TraceEventCache(), _instanceName, type, (int)id, prefix + format);

Exception is saying:

“Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader’s or TextWriter’s Synchronized methods. This also applies to classes like StreamWriter and StreamReader.”

After that I keep getting second exception lots of times on the same call to

item.TraceData(new TraceEventCache(), _instanceName, type, (int)id, prefix + format);

Exception

Count cannot be less than zero.
Parameter name: count
Stack trace: ” at System.String.CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIndex, Int32 count)\r\n at System.IO.StreamWriter.Write(String value)\r\n at System.Diagnostics.TextWriterTraceListener.Write(String message)\r\n at System.Diagnostics.TraceListener.WriteHeader(String source, TraceEventType eventType, Int32 id)\r\n at System.Diagnostics.TraceListener.TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, Object data)\r\n at Jfc.Configuration.ConfigurableTraceLogger.TraceData(String category, TraceEventType type, EventId id, String prefix, String format, Object[] args)”

It seems to me that my class is not thread safe as well the call to TraceData. But ConfigurableTextWriterTraceListener is said to thread safe after all. However I checked IsThreadSafe propety for my TextWriterTraceListener derived class at run-time
and it false. I am trying to figure out where the problem is.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:36:47+00:00Added an answer on May 12, 2026 at 8:36 pm

    It means what says – your TraceListener is not thread safe and breaks when accessed from several threads. You need to make your listeners thread safe or find a way to ensure that only one thread accesses any particular instance.

    One way to make them thread safe is to use a synchronized queue and make all your calls enqueue data items to the queue while the ‘real’ traceListener dequeues them and writes them out in a separate thread.

    You also have to be careful with your dictionary of listeners – updating the dictionary is not thread safe, but if you never access it before the last update is applied, you can leave it as is

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file wich has about 12 millon lines, each line looks like
I have something like this in my project: add_custom_command(OUTPUT somefile) add_custom_target(tgt ALL DEPENDS somefile)
I have something like this: <ListBox ItemsSource={Binding List}> <ListBox.ItemContainerStyle> <Style TargetType=ListBoxItem> <Setter Property=HorizontalContentAlignment Value=Stretch></Setter>
I have something like in my JSP page : <% if(id!=null) { %> <input
I have something like this in HTML: <select name=selection> <option id=1>First Option</option> ... </select>
I have something like this: barProgress.BeginAnimation(RangeBase.ValueProperty, new DoubleAnimation( barProgress.Value, dNextProgressValue, new Duration(TimeSpan.FromSeconds(dDuration))); Now, how
I have something like this: <node TEXT= txt A /> <node TEXT= txt X
Im trying to have a blueprint layout wich is 950px so I do something
well I have a wordpress loop in wich I want to query posts from
I have a simple servlet wich generates a piechart. It gets the data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.