WriteException Method (My.Application.Log and My.Log)
Writes exception information to the application's log listeners.
' Usage
My.Application.Log.WriteException(ex)
My.Application.Log.WriteException(ex ,severity ,additionalInfo)
My.Application.Log.WriteException(ex ,severity ,additionalInfo ,id)
' Web usage
My.Log.WriteException(ex)
My.Log.WriteException(ex ,severity ,additionalInfo)
My.Log.WriteException(ex ,severity ,additionalInfo ,id)
' Declaration
Public Sub WriteException( _
ByVal ex As System.Exception _
)
' -or-
Public Sub WriteException( _
ByVal ex As System.Exception, _
ByVal severity As System.Diagnostics.TraceEventType, _
ByVal additionalInfo As String _
)
' -or-
Public Sub WriteException( _
ByVal ex As System.Exception, _
ByVal severity As System.Diagnostics.TraceEventType, _
ByVal additionalInfo As String, _
ByVal id As Integer _
)
Parameters
ex
Required. Exception to log.severity
The type of message. By default, Error.additionalInfo
String to append to the message. By default, this is an empty string.id
Message identifier, typically used for correlation. By default, related to entryType as described in the table in the Remarks section.
Exceptions
The following conditions will cause an exception:
The exception or message type is Nothing (ArgumentNullException).
The message type is not one of the TraceEventType enumeration values (InvalidEnumArgumentException).
Code with partial trust calls the method, but writes to an event log listener that requires full trust (SecurityException).
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 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 |
---|---|
0 |
|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
Tasks
The following table lists examples of tasks involving the WriteException method.
To |
See |
---|---|
Write exception information to the application's event log listeners. |
|
Determine where Log writes information. |
Walkthrough: Determining Where My.Application.Log Writes Information |
Example
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.
Requirements
Namespace:Microsoft.VisualBasic.Logging
Class:Log
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
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 |
Permissions
The following permission may be necessary:
Permission |
Description |
---|---|
Controls the ability to access files and folders. Associated enumeration: Unrestricted. |
For more information, see Code Access Security and Requesting Permissions.
See Also
Tasks
How to: Log Exceptions in Visual Basic
Walkthrough: Determining Where My.Application.Log Writes Information
Concepts
Working with Application Logs in Visual Basic