Friday, May 15, 2009

Write to System Log in Vb.net

To write an entry to System Log, you could do it 2 ways:
  1. Use existing source "Application" in the Event Viewer
  2. Create new source just for your application
BTW: To write logs, all functions are defined under System.Diagnositcs.EventLog namespace. Use: Imports System.Diagnostics.EventLog
To write to existing source, use:
EventLog.WriteEntry("TestApp", "This is Test") -- This will write log under Application with source name as TestApp.
To write to new source, use:
Dim objEventLog As New System.Diagnostics.EventLog()
Dim AppName As String = "Test App"
Dim LogName As String = "My Log"

'Register the App as an Event Source
If Not System.Diagnostics.EventLog.SourceExists(AppName) Then
       System.Diagnostics.EventLog.CreateEventSource(AppName, LogName)
End If
objEventLog.Source = LogName
objEventLog.WriteEntry("This is Test")

HTH.

Sunday, May 10, 2009

SqlServer 2000 Error Logging

To log error in sql log / windows log, just exec the following SP:

….sql code goes here which will cause error.
--To check for error, we use @@error global variable predefined in sql.
set @error = @@error
if @error <> 0
exec sp_altermessage @error ,'with_log', 'true'

To view the error, you can either view either in
  1. windows event viewer
  2. SQL server Mgmt studio , path is : --> Management --> Sql Server Logs
  3. exec master..xp_readerrorlog
To learn more visit
http://www.sqlservercentral.com/articles/Error+Description/65013/

Regards