Event Log Trace Listener

danyeungw

Well-known member
Joined
Aug 30, 2005
Messages
73
Programming Experience
10+
The following code ran but I didn't find the Event Log named EventLog. Is EventLog supposed to be created with the Dim statement and the text "Write something to the EventLog" supposed to be written to EventLog? I am using VB.NET 2.0 here.

VB.NET:
Dim objListener As New EventLogTraceListener("EventLog")
Trace.Listeners.Add(objListener)
Trace.WriteLine("Write something to the EventLog")

Thanks.
DanYeung
 
Last edited by a moderator:
No, the string overload constructor specifies an existing log. Use Eventlog class to create a new log and use an instance of the configured Eventlog class as input parameter to the EventLogTraceListener. See the code added, I copied it from the Eventlog class documentation:
VB.NET:
If Not EventLog.SourceExists("MySource") Then
  EventLog.CreateEventSource("MySource", "MyNewLog")
End If
Dim myLog As New EventLog()
myLog.Source = "MySource"
 
Dim objListener As New EventLogTraceListener(myLog)
Trace.Listeners.Add(objListener)
Trace.WriteLine("Write something to the EventLog")
 
"MyNewLog" is the event log file name, "MySource" is the logging source. Try it out, read the documentation, hover with mouse over code to see descriptions of what everything is - not necessarily in that order, but the information is literally at your fingertips. Did you know you can select a word in code (mouse doubleclick it) then click F1 button to bring up relevant help? By the way, here is the EventLog class documentation online, the page from where I mentioned I copied the code, you should read it to know what logs and sources are appropriate for you in different situations: http://msdn2.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx There are further details about Source in the Property page.
 
Thanks for you patients. I find it is a little more difficult to understand Microsoft's documents than other sites'. The code ran and the event log was created, but I couldn't locate it. I did a search in the entire c:\ drive. I haven't found one site that tells where the event log is located.

DanYeung
 
If you're (like 75% others) on XP, it's in Control Panel > Administrative Tools > Event Viewer.
There is also a Administrative Tools folder with the same content somewhere in Start Menu Programs.
I think it's pretty much same with Win2000.
 
I used XP Professional. I didn't find the event log I created with the code. I wanted to check if the content is correct. Thanks.

DanYeung
 
Back
Top