Log.WriteException Method (Exception)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Writes exception information to the application's log listeners.

Namespace:   Microsoft.VisualBasic.Logging
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public void WriteException(
	Exception ex
)

Parameters

ex
Type: System.Exception

Required. Exception to log.

Exception Condition
ArgumentNullException

ex is Nothing.

SecurityException

Code with partial trust calls the method, but writes to an event log listener that requires full trust.

The WriteException method writes the information contained in an exception to the application's event log listeners.

In client applications, the Log object is available through the My.Application.Log object. In Web applications, the Log object is available through the My.Log object.

To learn which log listeners receive the WriteException method's messages, see Walkthrough: Determining Where My.Application.Log Writes Information (Visual Basic). You can change default log listeners. For more information, see Working with Application Logs in Visual Basic.

For overloads that do not take the id argument, the identifier written to the log is defined by the following table.

severity

Default id

Information

0

Warning

1

Error

2

Critical

3

Start

4

Stop

5

Suspend

6

Resume

7

Verbose

8

Transfer

9

The following table lists examples of tasks involving the WriteException method.

To

See

Write exception information to the application's event log listeners.

How to: Log Exceptions in Visual Basic

Determine where Log writes information.

Walkthrough: Determining Where My.Application.Log Writes Information (Visual Basic)

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

No

Windows Service

Yes

Web Site

Yes

This example shows how to use the My.Application.Log.WriteException method to log exceptions. Uncomment the Dim and MsgBox lines to cause a NullReferenceException exception. For more information, see How to: Log Exceptions in Visual Basic.

Public Sub ExceptionLogTest(ByVal fileName As String)
    Try
        ' Code that might generate an exception goes here.
        ' For example:
        '    Dim x As Object
        '    MsgBox(x.ToString)
    Catch ex As Exception
        My.Application.Log.WriteException(ex, 
            TraceEventType.Error, 
            "Exception in ExceptionLogTest " & 
            "with argument " & fileName & ".")
    End Try
End Sub

This code example can run only within a client application. Change My.Application.Log.WriteException to My.Log.WriteException for Web applications.

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

.NET Framework
Available since 2.0
Return to top
Show: