Error tracking a windows service

  • Thread starter Thread starter OpticTygre
  • Start date Start date
O

OpticTygre

What is the best way that anyone has found to capture errors inside a
windows service? It's pointless to use compilation constants, as you can't
"debug" a service very easily, and you can't pass in a command line
arguement (can you?) to tell it to do something special like capture errors
to a file while it's running. Has anyone figured out any good ways to do
this type of run-time error capturing?

Basically, if I were building a standard windows app, I'd just program it so
when certain command line arguements are passed in, the compiled code would
know to output information to a file. This saves me time from having to be
on-site and step through the code line by line. Can you do something like
that with a windows service?
 
Take a look at using TraceListeners and the application configuration file.
 
Ray Cassick (Home) said:
Take a look at using TraceListeners and the application configuration file.

I just started with .net, but I've been using the application event log for
a service I've been working on. Is there a reason not to?
 
OpticTygre,
I currently am using a Try/Catch around my main timer routine, within the
Catch block I log any exceptions (errors) to the Event Log
(ServiceBase.EventLog).

As Ray suggests you could also use TraceListeners.

I am considering "upgrading" my logging to either Log4Net or the Microsoft
Enterprise Library Logging Block:

http://logging.apache.org/log4net/

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/entlib.asp


You can pass parameters to a service, the following example demonstrates
how.

http://msdn.microsoft.com/msdnmag/issues/01/12/NETServ/default.aspx

Specifically Walkthrough item #7 & Figure 5, the args array to the
ServiceBase.OnStart method is the "parameters" to the service. You set these
parameters in the properties of the Service in Computer Management under the
Windows Control Panel.

Alternatively I simply put the settings in the app.config for the service.
Depending on the needs of the service, I would consider passing the name of
the "environment" to as a Parameter, then used this "environment" parameter
as an indexer into a custom Configuration Section in my app.config... By
"environment" I mean: unit test, system test, QA, Production...

Hope this helps
Jay


| What is the best way that anyone has found to capture errors inside a
| windows service? It's pointless to use compilation constants, as you
can't
| "debug" a service very easily, and you can't pass in a command line
| arguement (can you?) to tell it to do something special like capture
errors
| to a file while it's running. Has anyone figured out any good ways to do
| this type of run-time error capturing?
|
| Basically, if I were building a standard windows app, I'd just program it
so
| when certain command line arguements are passed in, the compiled code
would
| know to output information to a file. This saves me time from having to
be
| on-site and step through the code line by line. Can you do something like
| that with a windows service?
|
|
 
The event log is fine for things that you can easily document (stopping,
starting, exceptions during critical areas, etc...) but for debug style
traces nothing beats a good old fashioned, well formatted text file.

I have seen sooo many service type apps that try to use the vent log for
debug type tracing. Makes it a real pain in the butt to actual trace code
that way, and besides, you can really fill up someone's log file if you're
not careful.
 
Ray Cassick (Home) said:
I have seen sooo many service type apps that try to use the vent log for
debug type tracing. Makes it a real pain in the butt to actual trace code
that way, and besides, you can really fill up someone's log file if you're
not careful.

Programming is my second hat (that's why I'm not very good at it). My first
hat is being an engineer. As you probably know, we know everything. I would
never fill up an event log... :p Thanks for the info.
 

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

Back
Top