Debug.WriteLine锁 [英] Debug.WriteLine locks

查看:221
本文介绍了Debug.WriteLine锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序经常与死锁停止。当我做一个突破一切,看看我看到三个线程都停留在我们的记录功能的线程:

 公共类记录
{
公共静态无效WriteClientLog(LogLevel的日志级别字符串消息)
{
#如果DEBUG
System.Diagnostics.Debug.WriteLine(的String.Format( {0} {1},DateTime.Now.ToString(HH:MM:SS),消息)); // LOCK
#ENDIF
//...Log4net记录
}
}

如果我让程序继续线程都还停留在那一行。



我看不出这个可以锁定。 Debug类,串类功放和; DateTime类似乎是线程安全的。



错误消失,当我删除#如果调试系统... #ENDIF 代码,但我很好奇,为什么这种行为发生。



主题之一:

 公共无效CleanCache()
{
Logging.WriteClientLog(LogLevel.Debug,开始清理缓存。); //卡住
}

主题二:

 私人无效AliveThread()
{
Logging.WriteClientLog(LogLevel.Debug,检查连接); //卡住
}


解决方案

的Debug.WriteLine 写入日志消息连接到的听众集合。



你的一个跟踪听众必须在内部有一个锁,导致死锁。检查你的听众的代码,因为它是最有可能的罪魁祸首。


My program frequently stops with a deadlock. When I do a break-all and look at the threads I see that three threads are stuck in our logging function:

public class Logging
{
    public static void WriteClientLog(LogLevel logLevel, string message)
    {
      #if DEBUG
      System.Diagnostics.Debug.WriteLine(String.Format("{0} {1}", DateTime.Now.ToString("HH:mm:ss"), message)); //LOCK
      #endif
      //...Log4net logging
    }
}

If I let the program continue the threads are still stuck on that line.

I can't see where this can lock. The debug class, string class & datetime class seem to be thread safe.

The error goes away when I remove the #if DEBUG System... #endif code but I'm curious why this behavior happens.

Thread one:

public void CleanCache()
{
    Logging.WriteClientLog(LogLevel.Debug, "Start clean cache.");//Stuck
}

Thread two:

private void AliveThread()
{
    Logging.WriteClientLog(LogLevel.Debug, "Check connection");//Stuck
}

解决方案

Debug.WriteLine writes logging messages to the attached Trace Listeners attached to the Listeners collection.

One of your trace listeners must have a lock internally which is causing a deadlock. Check your listener code, as it's most likely the culprit.

这篇关于Debug.WriteLine锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆