Singleton compact Logger for ASP.NET applications

2019-06-09 09:28发布

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条回答
戒情不戒烟
2楼-- · 2019-06-09 09:50

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.

查看更多
登录 后发表回答