用于 ASP.NET 应用程序的单例紧凑型记录器
我编写了一个 Singeton
紧凑型记录器,对于 ASP.NET 应用程序来说非常方便。只需引用它,然后 Logger.Log.Info("Hello world!");
。它还自动记录未处理的异常。
有时,我在尝试创建日志文件 Stream
时遇到错误。
例外的是:
该进程无法访问文件“C:\inetpub\wwwroot\MyApp\Logs\5-22-2011.log”,因为它正在被另一个进程使用。
我检查了 Process Explorer,只有 w3wp.exe 可以处理该文件日志档案。似乎不同的线程造成了这个问题。这种情况大约每 24 小时发生一次!
I have coded a Singeton
compact logger which is very handy for ASP.NET
applications. Just refrencing it and then Logger.Log.Info("Hello world!");
. It also logs unhandled exceptions automatically.
Sometimes I get error where I try to create the log file Stream
.
The exception is:
The process cannot access the file 'C:\inetpub\wwwroot\MyApp\Logs\5-22-2011.log' because it is being used by another process..
I checked with Process Explorer and only w3wp.exe has handle over the log file. It seems different threads made the problem. This happens about every 24h!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这种情况定期发生,您可能需要检查 IIS 设置并查看应用程序池是否以 24 小时为间隔进行回收。 (我认为从记忆中默认是 29h)
如果是这种情况,您的问题可能是由于 IIS“启动”一个新的应用程序池而旧的应用程序池仍在处理请求(重叠回收 - 这是默认行为)引起的,即,在旧记录器调用析构函数之前,新记录器尝试在新进程中实例化。
编辑:忘记提及(当我写答案时已经晚了),缓解这种情况的一种方法是更改日志文件名以包含完整的日期和时间。
If it's happening periodically, you might want to check the IIS settings and see if the application pool is being recycled at 24h intervals. (I think from memory the default is 29h)
If this is the case, your problem could be caused by IIS 'spinning up' a new app pool while the old one is still handling requests (overlapped recycling - which is the default behaviour), i.e. a new Logger is attempting to instantiate in a new process before the old one has had its destructor called.
Edit: Forgot to mention (it was late when I wrote the answer), that one way to mitigate this would be to change the log filename to include the full date AND time.