View Light

How to write to the event log from code

I'd never tried to do this until today, but it's actually very easy to do. In the example below, I am executing a command on the database inside of a Try/Catch block. In the case that the command fails, the catch block writes details of the exception to the Application Event Log.

Note that you can plug any string into the first parameter, I just have it set to provide the product name of the application (Application.ProductName) which is nicer than a static string for when you embed this code into a reusable class.

Catch ex As Exception
  strStatus = "Error #" & Str(Err.Number) & " was generated by " & Err.Source & vbCrLf & _
  Err.Description & ". The error occurred in function makeConn."
  EventLog.WriteEntry(Application.ProductName, strStatus, EventLogEntryType.Error)
End Try
Comfortably Anonymous
3/21/2005 9:42:04 AM
This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit to those who have expressed a prior interest in receiving the included information for research and educational purposes. For more information go to: . If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner.