Below is an example on how to write to the SharePoint log files (located in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS folder).

catch(Exception e) 
{
	Microsoft.Office.Server.Diagnostics.PortalLog
    	.LogString("Exception: {0} - {1}", e.Message, e.StackTrace); 
 }

You’ll likely want to prefix your log entries with a date and time stamp.