2011-02-01 73 views
0

我正在使用.net tracelistener来启用应用程序中的日志记录功能。我的应用程序是多线程.Net服务。在重负载下,.Net跟踪会抛出以下错误。 有没有人有任何想法,为什么这是幸福和任何解决方案呢?.net tracelistener问题

System.UnhandledExceptionEventArgs 
System.IO.IOException 
mscorlib 
The process cannot access the file 'C:\035_Prg.log' because it is being used by another process. 

Server stack trace: 
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) 
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) 
    at System.IO.StreamWriter.CreateFile(String path, Boolean append) 
    at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) 
    at System.IO.StreamWriter..ctor(String path, Boolean append) 
    at System.Diagnostics.TraceListener.WriteHeader(String source, TraceEventType eventType, Int32 id) 
    at System.Diagnostics.TraceListener.TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, String format, Object[] args) 

    at System.Diagnostics.TraceSource.TraceEvent(TraceEventType eventType, Int32 id, String format, Object[] args) 
    at System.Diagnostics.TraceSource.TraceInformation(String message) 
+0

您是否尝试停止您的服务或IIS托管您的服务,或在任务管理器中终止asp_net进程? – Kuncevic 2011-02-01 11:15:38

回答

1

TraceListeners中有各种aspects of thread-safety应该有助于防止这种情况。它们允许全局锁定您正在编写的监听器集合,单个监听器的级别,或者在您的TraceListener announces itself as thread safe的情况下完全不锁定框架。框架没有做的是锁定文件本身,所以一定要检查明显的例如您的机器上的其他进程或应用程序中可能锁定文件的其他代码。

您可能还想考虑更改跟踪的粒度是否有帮助。我通常为每个模块和/或我的应用程序的活动定义单独的静态TraceSources。这样做,并关闭使用GlobalLock作为Rory在his article建议可能会减少争用。