473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem with EventLog

I have created a windows service that logs errors to the EventLog. I want
the logs to go to an EventLog I have created. However, I am finding that
the event is not logging to the created EventLog instead it is logging to
Application log. What could be the problem?

================================================== ======================
SYSTEM DETAILS:
================================================== ======================

Operating System: Windows 2000 Professional SP4
..NET Version: 1.1.4322

================================================== ======================
CODE:
================================================== ======================

Public Class MyClass

Private _EventSourceName As String = "MySource"
Private _EventLogName As String = "MyLog"

'************************************************* *********************
'*** DESCRIPTION: Adds entries to the Application Log.
'************************************************* *********************
Private Sub Add( _
ByVal DisplayMessage As String, _
ByVal objEventLogEntryType As EventLogEntryType, _
ByVal EventID As Integer, _
ByVal Category As Short _
)

'*** Create new event log ***
Dim oEventLog As New EventLog

'*** If the the Event Log does not exist, then create one.
If Not oEventLog.SourceExists(_EventSourceName) Then
'*** Create Log ***
oEventLog.CreateEventSource(_EventSourceName, _EventLogName)
End If

'*** Set the event log source ***
oEventLog.Log = _EventLogName
oEventLog.Source = _EventSourceName

'*** Write to the event log ***
oEventLog.WriteEntry(_EventSourceName, DisplayMessage,
objEventLogEntryType, EventID, Category)

End Sub
'************************************************* *********************
'*** DESCRIPTION: Creates "Variable Information" entry. This is
'*** mainly used for testing purposes.
'************************************************* *********************
Public Sub Message( _
ByVal Value As String, _
Optional ByVal objEventLogEntryType As EventLogEntryType =
EventLogEntryType.Information, _
Optional ByVal EventID As Integer = 0, _
Optional ByVal Category As Short = 0 _
)

'*** Declare Variables ***
Dim strMessage As String

'*** Initialize Variables ***
strMessage = "Message" & vbCrLf & vbCrLf
strMessage &= Value & vbCrLf

'*** Add Application Log Entry ***
Add(strMessage, objEventLogEntryType, EventID, Category)

End Sub

End Class

'***************************************
'*** MY FUNCTION CALL ***
'***************************************
Dim oApplicationEntry As New MyClass
oApplicationEntry.Message("TEST", EventLogEntryType.Error, 9999)
oApplicationEntry = Nothing


--
Thanx in Advance,

atr2000
Jun 8 '06 #1
2 1531
Hi John,

The issue I see is with the statement
'*** If the the Event Log does not exist, then create one.
If Not oEventLog.SourceExists(_EventSourceName) Then
'*** Create Log ***
oEventLog.CreateEventSource(_EventSourceName, _EventLogName)
End If
The default project installer action creates the event source in the
"Application" log, So the "SourceExists" would always return true and the
"CreateEventSource" statement would never get executed. If this is indeed a
service then I'd recommend you use the Me.EventLog option
(ServiceBase.EventLog) to set the source & Log for logging messages.
--
Ravi Shankar
"John Smith" wrote:
I have created a windows service that logs errors to the EventLog. I want
the logs to go to an EventLog I have created. However, I am finding that
the event is not logging to the created EventLog instead it is logging to
Application log. What could be the problem?

================================================== ======================
SYSTEM DETAILS:
================================================== ======================

Operating System: Windows 2000 Professional SP4
..NET Version: 1.1.4322

================================================== ======================
CODE:
================================================== ======================

Public Class MyClass

Private _EventSourceName As String = "MySource"
Private _EventLogName As String = "MyLog"

'************************************************* *********************
'*** DESCRIPTION: Adds entries to the Application Log.
'************************************************* *********************
Private Sub Add( _
ByVal DisplayMessage As String, _
ByVal objEventLogEntryType As EventLogEntryType, _
ByVal EventID As Integer, _
ByVal Category As Short _
)

'*** Create new event log ***
Dim oEventLog As New EventLog

'*** If the the Event Log does not exist, then create one.
If Not oEventLog.SourceExists(_EventSourceName) Then
'*** Create Log ***
oEventLog.CreateEventSource(_EventSourceName, _EventLogName)
End If

'*** Set the event log source ***
oEventLog.Log = _EventLogName
oEventLog.Source = _EventSourceName

'*** Write to the event log ***
oEventLog.WriteEntry(_EventSourceName, DisplayMessage,
objEventLogEntryType, EventID, Category)

End Sub
'************************************************* *********************
'*** DESCRIPTION: Creates "Variable Information" entry. This is
'*** mainly used for testing purposes.
'************************************************* *********************
Public Sub Message( _
ByVal Value As String, _
Optional ByVal objEventLogEntryType As EventLogEntryType =
EventLogEntryType.Information, _
Optional ByVal EventID As Integer = 0, _
Optional ByVal Category As Short = 0 _
)

'*** Declare Variables ***
Dim strMessage As String

'*** Initialize Variables ***
strMessage = "Message" & vbCrLf & vbCrLf
strMessage &= Value & vbCrLf

'*** Add Application Log Entry ***
Add(strMessage, objEventLogEntryType, EventID, Category)

End Sub

End Class

'***************************************
'*** MY FUNCTION CALL ***
'***************************************
Dim oApplicationEntry As New MyClass
oApplicationEntry.Message("TEST", EventLogEntryType.Error, 9999)
oApplicationEntry = Nothing


--
Thanx in Advance,

atr2000

Jun 9 '06 #2
Ravi,

Thank you for the idea but unfortunately that did not help. However I did
figure it out. I changed the code to more like the following.

'*** Create new event log ***
Dim oEventLog As New EventLog

'*** Since I know that my log name is unique, I am not afraid to remove
the source registration from the Application log ***
If oEventLog.LogNameFromSourceName(_EventSourceName,
Environment.MachineName).ToLower <> _EventLogName.ToLower Then
'*** Delete Source ***
oEventLog.DeleteEventSource(_EventSourceName)
End If

'*** If the the Event Log does not exist, then create one. ***
If oEventLog.Exists(_EventLogName) = False Or
oEventLog.SourceExists(_EventSourceName) = False Then
'*** Create Log ***
oEventLog.CreateEventSource(_EventSourceName, _EventLogName)
End If

..... Etc....

--
Thanx,

atr2000
"Ravi Shankar" <sh*********@newsgroup.nospam> wrote in message
news:74**********************************@microsof t.com...
Hi John,

The issue I see is with the statement
'*** If the the Event Log does not exist, then create one.
If Not oEventLog.SourceExists(_EventSourceName) Then
'*** Create Log ***
oEventLog.CreateEventSource(_EventSourceName, _EventLogName)
End If
The default project installer action creates the event source in the
"Application" log, So the "SourceExists" would always return true and the
"CreateEventSource" statement would never get executed. If this is indeed

a service then I'd recommend you use the Me.EventLog option
(ServiceBase.EventLog) to set the source & Log for logging messages.
--
Ravi Shankar
"John Smith" wrote:
I have created a windows service that logs errors to the EventLog. I want the logs to go to an EventLog I have created. However, I am finding that the event is not logging to the created EventLog instead it is logging to Application log. What could be the problem?

================================================== ======================
SYSTEM DETAILS:
================================================== ======================

Operating System: Windows 2000 Professional SP4
..NET Version: 1.1.4322

================================================== ======================
CODE:
================================================== ======================

Public Class MyClass

Private _EventSourceName As String = "MySource"
Private _EventLogName As String = "MyLog"

'************************************************* ********************* '*** DESCRIPTION: Adds entries to the Application Log.
'************************************************* ********************* Private Sub Add( _
ByVal DisplayMessage As String, _
ByVal objEventLogEntryType As EventLogEntryType, _
ByVal EventID As Integer, _
ByVal Category As Short _
)

'*** Create new event log ***
Dim oEventLog As New EventLog

'*** If the the Event Log does not exist, then create one.
If Not oEventLog.SourceExists(_EventSourceName) Then
'*** Create Log ***
oEventLog.CreateEventSource(_EventSourceName, _EventLogName)
End If

'*** Set the event log source ***
oEventLog.Log = _EventLogName
oEventLog.Source = _EventSourceName

'*** Write to the event log ***
oEventLog.WriteEntry(_EventSourceName, DisplayMessage,
objEventLogEntryType, EventID, Category)

End Sub
'************************************************* ********************* '*** DESCRIPTION: Creates "Variable Information" entry. This is
'*** mainly used for testing purposes.
'************************************************* ********************* Public Sub Message( _
ByVal Value As String, _
Optional ByVal objEventLogEntryType As EventLogEntryType =
EventLogEntryType.Information, _
Optional ByVal EventID As Integer = 0, _
Optional ByVal Category As Short = 0 _
)

'*** Declare Variables ***
Dim strMessage As String

'*** Initialize Variables ***
strMessage = "Message" & vbCrLf & vbCrLf
strMessage &= Value & vbCrLf

'*** Add Application Log Entry ***
Add(strMessage, objEventLogEntryType, EventID, Category)

End Sub

End Class

'***************************************
'*** MY FUNCTION CALL ***
'***************************************
Dim oApplicationEntry As New MyClass
oApplicationEntry.Message("TEST", EventLogEntryType.Error, 9999)
oApplicationEntry = Nothing


--
Thanx in Advance,

atr2000

Jun 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jacob Colding | last post by:
Hello, I have created a a logging mechanism, that writes mesages to a custom event log. On each check it verifies whether the requested source is registered to the "IPS" log. If not, it changes...
0
by: Alex | last post by:
I am trying to write an Exit module for the Certification server in VB.NET and I cant get the module to be recognised by the Certification MMC snap in. Its function is to capture Serial Numbers of...
2
by: Ronald Moolenaar | last post by:
Hi, My colleague implemented a class overwriting TextWriter. What he tried to do was logging to the event logger and logging to console. As you can see in the code below, the part of 'logging to...
5
by: Jassim Rahma | last post by:
Hi, I am trying to create an eventlog DLL which uses vistaDB but I have a problem calling it.. can you please help... the DLL: using System;
5
by: Dan Brill | last post by:
Hi, I'm sure this issue has been covered before but after searching around I can't find anything which deals with quite the same set of circumstances or suggests an optimal solution. The...
0
by: Brian Henry | last post by:
Has security changed with accessing the event log through a local service in ..NET 2.0? in 1.1 i did this If Not Diagnostics.EventLog.SourceExists("BDBHOST") Then ' create log ...
0
by: Ben | last post by:
I modified the logmonitor sdk example so it would work over a network. It works great when the client and server are running on the same PC and have administrator privileges. So I have two...
0
by: Dave Raskin | last post by:
I am self hosting a Web Service in a Windows service. I am trying to start the service using the NT AUTHORITY\NetworkService account. I get a NullReferenceException on ServiceHost.Open() in the...
4
by: Dave Burns | last post by:
I am self hosting a Web Service in a Windows service. I am trying to start the service using the NT AUTHORITY\NetworkService account. I get a NullReferenceException on ServiceHost.Open() in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.