Use a static form of the EventLog class?

  • Thread starter Thread starter Ron Hinds
  • Start date Start date
R

Ron Hinds

I'm developing a Windows Service in VB.Net. In the Visual Studio
Programmer's Guide for the .NET Framework - How to: Log Information About
Services, the following statement is made:

By default, all Windows Service projects have the ability to interact with
the Application event log and write information and exceptions to it. You
use the AutoLog property to indicate whether you want this functionality in
your application. By default, logging is turned on for any service you
create with the Windows Service project template. You can use a static form
of the EventLog class to write service information to a log without having
to create an instance of an EventLog component or manually register a
source.

So, in my code, I use the following:

EventLog.WriteEntry("Socket connected!")

But the IDE indicates there is a problem, and this is the error message:

Error 1 Reference to a non-shared member requires an object reference.

EventLog.Write Entry is Shared, but it requires an instance of an
EventInstance, and the methods require using a resource file. This app is
for use only in our company and I really don't want to go to the trouble of
creating a resource file, etc. (trying to keep it simple ;-).

Can anyone point me to a sample of a Windows Service that uses the default
AutoLog showing how to do the logging? Thanks!
 
Ron Hinds said:
I'm developing a Windows Service in VB.Net. In the Visual Studio
Programmer's Guide for the .NET Framework - How to: Log Information About
Services, the following statement is made:

By default, all Windows Service projects have the ability to interact with
the Application event log and write information and exceptions to it. You
use the AutoLog property to indicate whether you want this functionality
in your application. By default, logging is turned on for any service you
create with the Windows Service project template. You can use a static
form of the EventLog class to write service information to a log without
having to create an instance of an EventLog component or manually register
a source.

So, in my code, I use the following:

EventLog.WriteEntry("Socket connected!")

But the IDE indicates there is a problem, and this is the error message:

Error 1 Reference to a non-shared member requires an object reference.

EventLog.Write Entry is Shared, but it requires an instance of an
EventInstance, and the methods require using a resource file. This app is
for use only in our company and I really don't want to go to the trouble
of creating a resource file, etc. (trying to keep it simple ;-).

Can anyone point me to a sample of a Windows Service that uses the default
AutoLog showing how to do the logging? Thanks!

Never mind. I figured it out. For anyone else who might be in need of the
answer - it is only the first overload of WriteEntry that isn't Shared - so
this code works:

EventLog.WriteEntry("MyService", "Socket connected!")
 
Never mind. I figured it out. For anyone else who might be in need of the
answer - it is only the first overload of WriteEntry that isn't Shared - so
this code works:

EventLog.WriteEntry("MyService", "Socket connected!")

Permit me to say ...

"Well done" for figuring it out and, perhaps more importantly around
here, thank you for sharing the solution!

It is /so/ annoying when you "Google" a problem (is that a verb yet)
only to find some banal "I fixed it" note (and nothing else) at the end
of the thread.

Regards,
Phill W.
 
Back
Top