本文介绍了竞争条件/ TextWriterTraceListener会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有话至极看起来对我来说,比赛的条件,同时从记录到文件多线程。

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

1)我有说是在我的应用程序的多个线程共享一个自定义记录类(ConfigurableTraceLogger)。它有大量的包装功能,所有调用的主要核心功能

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();
        }
    }
}

正如你可以看到我的课只存储不同TraceListner-derved类集合_听众。基本上有唯一的控制台和文本文件的听众。什么TRACEDATA确实需要的类别名称(如日志启动时),并找到合适的听众。所有的听众是由配置文件名定义

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" };
}

在我5至10分钟后,开始了我的申请,我得到异常在电话会议上

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

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

例外是在说:

Exception is saying:

检测,同时复制存储的I / O包不是默认线程安全的。在多线程应用程序,流必须在一个线程安全的方式来访问,如线可能​​的I / O竞争条件通过的TextReader的或TextWriter的的同步方法返回-safe包装。这也适用于类,如的StreamWriter和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);

异常

计数不能小于零。参数名:计数堆栈跟踪:在System.String.CopyTo(的Int32 sourceIndex,字符[]目标,的Int32 destinationIndex,的Int32计数)\ r \ n将在System.IO.StreamWriter.Write(字符串值)\ r \ n将在System.Diagnostics程序。 TextWriterTraceListener.Write(字符串消息)\ r \ n将在System.Diagnostics.TraceListener.WriteHeader(字符串源,TraceEventType事件类型,的Int32 ID)\ r \ n将在System.Diagnostics.TraceListener.TraceData(TraceEventCache eventCache,弦乐来源,TraceEventType EVENTTYPE ,的Int32 ID,对象数据)\ r \ n将在Jfc.Configuration.ConfigurableTraceLogger.TraceData(串类,TraceEventType类型,事件ID编号,字符串preFIX,字符串格式,对象[]参数)

Count cannot be less than zero.Parameter name: countStack 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)"

在我看来,我的类不是线程安全的,以及调用TRACEDATA。但ConfigurableTextWriterTraceListener据说线程毕竟安全。但是我检查IsThreadSafe欢迎使用属性为我TextWriterTraceListener会派生类在运行时而且假的。我试图找出问题所在。

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-timeand it false. I am trying to figure out where the problem is.

推荐答案

这意味着什么说 - 从多个线程访问时,您的TraceListener不是线程安全和休息。你需要让你的听众是线程安全的,或找到一种方法,以确保只有一个线程访问任何特定实例。

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.

,使他们线程安全的一种方法是使用一个同步的队列中,让所有的呼叫排队的数据项的队列中,而真实的TraceListener队列中取出它们,将它们写在一个单独的线程。

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

这篇关于竞争条件/ TextWriterTraceListener会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:18