该进程无法访问该文件'C:\\的Inetpub \\ wwwroot的\\ MyApp的\\ 5-23-2011.log',因为它正由另一个进程使用 [英] The process cannot access the file 'C:\inetpub\wwwroot\MyApp\5-23-2011.log' because it is being used by another process

查看:191
本文介绍了该进程无法访问该文件'C:\\的Inetpub \\ wwwroot的\\ MyApp的\\ 5-23-2011.log',因为它正由另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这是一个ASP.NET应用程序内部使用一个单记录。有时我该进程无法访问该行的文件错误:

I have a singleton logger which is used inside an ASP.NET application. Sometimes I get The process cannot access the file error on this line:

StreamWriter sw = new StreamWriter("Path to log file", true);

我通过 的Process Explorer检查文件句柄 的w3wp.exe 拥有把手,它似乎来自同一进程不同线程造成的问题。

I checked file handle by Process Explorer and w3wp.exe owns the handle so it seems different threads from the same process caused the problem.

我使用了一个锁定围绕上述code,但我仍然得到错误。
我怎样才能确保所有的线程可以安全地使用相同的流?

I have used a lock around the above code, but still I get the error. How can I make sure all threads can use the same stream safely?

推荐答案

不要打开日志文件不止一次;在应用程序启动时,只要打开它,在退出关闭(经常刷新它。)打开和关闭一次以上仅仅是低效的。

Don't open the log file more than once; just open it when the application starts, close it at exit (and flush it often.) Opening and closing more than once is just inefficient.

这篇关于该进程无法访问该文件'C:\\的Inetpub \\ wwwroot的\\ MyApp的\\ 5-23-2011.log',因为它正由另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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