Sending trace to event log VB.NET

Private Sub cmdTraceToEventLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTraceToEventLog.Click
' This Sub demonstrates how to create and add a trace
' listener that sends messages to the application
' event log. This should be used sparingly due to the
' resources it uses to write to the event log.

' Create an EventLog instance and assign it a source.
Dim myLog As New EventLog()
myLog.Source = Me.Text

' Create a trace listener for the application event log.
Dim tlEventLog As New EventLogTraceListener(myLog)

Trace.Listeners.Clear()

' Add the event log trace listener to the collection.
Trace.Listeners.Add(tlEventLog)

' Write output to the event log.
Trace.WriteLine("This is a test of event log tracing")

End Sub

Write code in Module:
Option Strict On

Imports System
Imports System.IO

' This class was created to output trace messages to an HTML file using tags

Public Class HTMLTraceListener
Inherits System.Diagnostics.TextWriterTraceListener

' Use constructor from TextWriterTraceListener to write to a stream
Public Sub New(ByVal stream As Stream)
MyBase.New(stream)
End Sub

Public Sub WriteHeader(ByVal Title As String)
' New method to add a title to the top of the HTML document
Writer.WriteLine("")
Writer.WriteLine("" & Title & "")
Writer.WriteLine("")
Writer.WriteLine("

" & Title & "

")
Writer.WriteLine("


")

End Sub

Protected Overloads Overrides Sub WriteIndent()
' Override WriteIndent to handle indention in an HTML document
' using   for a space
Dim i As Integer
Dim j As Integer

If IndentLevel > 0 Then
For i = 1 To IndentLevel
For j = 1 To IndentSize
Writer.Write(" ")
Next j
Next i
End If

End Sub

Public Overloads Overrides Sub WriteLine(ByVal message As String)
' Override WriteLine Method to add tags to output message
Writer.Write("" & Now() & " - ")

If NeedIndent Then
WriteIndent()
End If

Writer.WriteLine(message & "

")
End Sub

End Class


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: