Export (0) Print
Expand All

Log.WriteException Method (Exception, TraceEventType, String)

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,
	TraceEventType severity,
	string additionalInfo
)

Parameters

ex
Type: System.Exception

Required. Exception to log.

severity
Type: System.Diagnostics.TraceEventType

The type of message. By default, Error.

additionalInfo
Type: System.String

String to append to the message. By default, this is an empty string.

ExceptionCondition
ArgumentNullException

ex is Nothing.

InvalidEnumArgumentException

The message type is not one of the TraceEventType enumeration values.

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.

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)

Availability by Project Type

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.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Show:
© 2015 Microsoft