Edit

Share via


Log.WriteEntry Method

Definition

Writes a message to the application's log listeners.

Overloads

WriteEntry(String)

Writes a message to the application's log listeners.

WriteEntry(String, TraceEventType)

Writes a message to the application's log listeners.

WriteEntry(String, TraceEventType, Int32)

Writes a message to the application's log listeners.

WriteEntry(String)

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

Writes a message to the application's log listeners.

public:
 void WriteEntry(System::String ^ message);
public void WriteEntry(string message);
member this.WriteEntry : string -> unit
Public Sub WriteEntry (message As String)

Parameters

message
String

Required. The message to log. If message is Nothing, an empty string is used.

Exceptions

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.WriteEntry method to log tracing information. For more information, see How to: Write Log Messages.

Private Sub GetOpenFormTitles()
    Dim formTitles As New Collection

    Try
        For Each f As Form In My.Application.OpenForms
            ' Use a thread-safe method to get all form titles.
            formTitles.Add(GetFormTitle(f))
        Next
    Catch ex As Exception
        formTitles.Add("Error: " & ex.Message)
    End Try

    Form1.ListBox1.DataSource = formTitles
End Sub

Private Delegate Function GetFormTitleDelegate(f As Form) As String
Private Function GetFormTitle(f As Form) As String
    ' Check if the form can be accessed from the current thread.
    If Not f.InvokeRequired Then
        ' Access the form directly.
        Return f.Text
    Else
        ' Marshal to the thread that owns the form. 
        Dim del As GetFormTitleDelegate = AddressOf GetFormTitle
        Dim param As Object() = {f}
        Dim result As System.IAsyncResult = f.BeginInvoke(del, param)
        ' Give the form's thread a chance process function.
        System.Threading.Thread.Sleep(10)
        ' Check the result.
        If result.IsCompleted Then
            ' Get the function's return value.
            Return "Different thread: " & f.EndInvoke(result).ToString
        Else
            Return "Unresponsive thread"
        End If
    End If
End Function

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

Remarks

The WriteEntry method writes a message 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 WriteEntry 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 id 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 WriteEntry method.

To See
Write event information to the application's log listeners How to: Write Log Messages
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

WriteEntry(String, TraceEventType)

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

Writes a message to the application's log listeners.

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

Parameters

message
String

Required. The message to log. If message is Nothing, an empty string is used.

severity
TraceEventType

The type of message. By default, TraceEventType.Information.

Exceptions

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.WriteEntry method to log tracing information. For more information, see How to: Write Log Messages.

Private Sub GetOpenFormTitles()
    Dim formTitles As New Collection

    Try
        For Each f As Form In My.Application.OpenForms
            ' Use a thread-safe method to get all form titles.
            formTitles.Add(GetFormTitle(f))
        Next
    Catch ex As Exception
        formTitles.Add("Error: " & ex.Message)
    End Try

    Form1.ListBox1.DataSource = formTitles
End Sub

Private Delegate Function GetFormTitleDelegate(f As Form) As String
Private Function GetFormTitle(f As Form) As String
    ' Check if the form can be accessed from the current thread.
    If Not f.InvokeRequired Then
        ' Access the form directly.
        Return f.Text
    Else
        ' Marshal to the thread that owns the form. 
        Dim del As GetFormTitleDelegate = AddressOf GetFormTitle
        Dim param As Object() = {f}
        Dim result As System.IAsyncResult = f.BeginInvoke(del, param)
        ' Give the form's thread a chance process function.
        System.Threading.Thread.Sleep(10)
        ' Check the result.
        If result.IsCompleted Then
            ' Get the function's return value.
            Return "Different thread: " & f.EndInvoke(result).ToString
        Else
            Return "Unresponsive thread"
        End If
    End If
End Function

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

Remarks

The WriteEntry method writes a message 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 WriteEntry 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 id 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 WriteEntry method.

To See
Write event information to the application's log listeners How to: Write Log Messages
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

WriteEntry(String, TraceEventType, Int32)

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

Writes a message to the application's log listeners.

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

Parameters

message
String

Required. The message to log. If message is Nothing, an empty string is used.

severity
TraceEventType

The type of message. By default, TraceEventType.Information.

id
Int32

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

Exceptions

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.WriteEntry method to log tracing information. For more information, see How to: Write Log Messages.

Private Sub GetOpenFormTitles()
    Dim formTitles As New Collection

    Try
        For Each f As Form In My.Application.OpenForms
            ' Use a thread-safe method to get all form titles.
            formTitles.Add(GetFormTitle(f))
        Next
    Catch ex As Exception
        formTitles.Add("Error: " & ex.Message)
    End Try

    Form1.ListBox1.DataSource = formTitles
End Sub

Private Delegate Function GetFormTitleDelegate(f As Form) As String
Private Function GetFormTitle(f As Form) As String
    ' Check if the form can be accessed from the current thread.
    If Not f.InvokeRequired Then
        ' Access the form directly.
        Return f.Text
    Else
        ' Marshal to the thread that owns the form. 
        Dim del As GetFormTitleDelegate = AddressOf GetFormTitle
        Dim param As Object() = {f}
        Dim result As System.IAsyncResult = f.BeginInvoke(del, param)
        ' Give the form's thread a chance process function.
        System.Threading.Thread.Sleep(10)
        ' Check the result.
        If result.IsCompleted Then
            ' Get the function's return value.
            Return "Different thread: " & f.EndInvoke(result).ToString
        Else
            Return "Unresponsive thread"
        End If
    End If
End Function

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

Remarks

The WriteEntry method writes a message 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 WriteEntry 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 id 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 WriteEntry method.

To See
Write event information to the application's log listeners How to: Write Log Messages
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