Subscribe

RSS Feed (xml)

Logging Error Details

Create an object of type


System.Diagnostics.EventLog.Then begin the try/catch


block and enter the code you suspect is causing the error. Create a catch block to run if


an error occurs and assign the exception to the ex variable. Inside the block, create a


new EventLog instance. Set the log to send the error to, and enter your application name


as the source. Finally, write the exception to the event log.


In <script runat="server" /> block or codebehind:


Sub Page_Load(sender As Object, e As EventArgs)


Dim a as Integer=0


Dim myEventLog as System.Diagnostics.EventLog


Try


a=1/a


Catch ex as Exception


myEventLog=new System.Diagnostics.EventLog()


myEventLog.Log="Application"


myEventLog.Source="Recipe0902"


myEventLog.WriteEntry(ex.ToString())



End Try




End Sub


You also can handle errors on an application error by modifying the Application_Error


event handler in the global.asax file. Use Server.GetLastError() to get a reference to


the exception that was thrown.


In the global.asax file:


Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)


Dim myEventLog As System.Diagnostics.EventLog


Dim ex As Exception = Server.GetLastError()


myEventLog = New System.Diagnostics.EventLog()


myEventLog.Log = "Application"


myEventLog.Source = "Recipe0902"


myEventLog.WriteEntry(ex.ToString())


End Sub


Comments


Catching and keeping a log of errors that occur can greatly decrease the amount of time


you spend supporting an application. Note:The ASP.NET account does not have access


to write to the event log by default.To run this example, you need to do one of the


following:


1. Modify the processModel element in machine.config to set ASP.NET to run as


the System account.


2.Add the ASP.NET account to the local Administrators group.


3. Use regedt32 to grant the ASP.NET account write access to the


HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog key.

No comments:

Archives

Variety in the Web World