Singleton compact Logger for ASP.NET applications

让人想犯罪 __ 提交于 2019-12-08 12:22:32

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/6092543/singleton-compact-logger-for-asp-net-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!