Problem logging to database

In my c# application, I use a database trace listener which writes to a remote sql database. And it works fine.
Problem:
If I lost the connection to my remote database, my program hangs trying to write to the database.
Is there a way I can avoid it. So that I log to a file rather trying to write to database.

Thanks for the reply. The application does not hang completly, but it takes unusually long time and the UI does not respond. This issue is so sensitive because my application is a client server multithreaded one.
I will look into the links provided by you. And will get back to you asap.