Edit

Share via


Log.WriteException Method

Definition

Writes exception information to the application's log listeners.

Overloads

WriteException(Exception)

Writes exception information to the application's log listeners.

WriteException(Exception, TraceEventType, String)

Writes exception information to the application's log listeners.

WriteException(Exception, TraceEventType, String, Int32)

Writes exception information to the application's log listeners.

WriteException(Exception)

Source:
Log.vb
Source:
Log.vb
Source:
Log.vb

Writes exception information to the application's log listeners.

public:
 void WriteException(Exception ^ ex);
public void WriteException (Exception ex);
member this.WriteException : Exception -> unit
Public Sub WriteException (ex As Exception)

Parameters

ex
Exception

Required. Exception to log.

Exceptions

ex is Nothing.

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

Examples

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.

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.

Remarks

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. You can change default log listeners. For more information, see Working with Application Logs.

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
Determine where Log writes information. Walkthrough: Determining Where My.Application.Log Writes Information

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

See also

Applies to

WriteException(Exception, TraceEventType, String)

Source:
Log.vb
Source:
Log.vb
Source:
Log.vb

Writes exception information to the application's log listeners.

public:
 void WriteException(Exception ^ ex, System::Diagnostics::TraceEventType severity, System::String ^ additionalInfo);
public void WriteException (Exception ex, System.Diagnostics.TraceEventType severity, string additionalInfo);
member this.WriteException : Exception * System.Diagnostics.TraceEventType * string -> unit
Public Sub WriteException (ex As Exception, severity As TraceEventType, additionalInfo As String)

Parameters

ex
Exception

Required. Exception to log.

severity
TraceEventType

The type of message. By default, Error.

additionalInfo
String

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

Exceptions

ex is Nothing.

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

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

Examples

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.

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.

Remarks

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. You can change default log listeners. For more information, see Working with Application Logs.

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
Determine where Log writes information. Walkthrough: Determining Where My.Application.Log Writes Information

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

See also

Applies to

WriteException(Exception, TraceEventType, String, Int32)

Source:
Log.vb
Source:
Log.vb
Source:
Log.vb

Writes exception information to the application's log listeners.

public:
 void WriteException(Exception ^ ex, System::Diagnostics::TraceEventType severity, System::String ^ additionalInfo, int id);
public void WriteException (Exception ex, System.Diagnostics.TraceEventType severity, string additionalInfo, int id);
member this.WriteException : Exception * System.Diagnostics.TraceEventType * string * int -> unit
Public Sub WriteException (ex As Exception, severity As TraceEventType, additionalInfo As String, id As Integer)

Parameters

ex
Exception

Required. Exception to log.

severity
TraceEventType

The type of message. By default, Error.

additionalInfo
String

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

id
Int32

Message identifier, typically used for correlation. By default, related to entryType as described in the table in the Remarks section.

Exceptions

ex is Nothing.

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

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

Examples

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.

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.

Remarks

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. You can change default log listeners. For more information, see Working with Application Logs.

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
Determine where Log writes information. Walkthrough: Determining Where My.Application.Log Writes Information

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

See also

Applies to