Use a static form of the EventLog class?

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!
 
R

Ron Hinds

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!")
 
P

Phill W.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top