How to handle error handling in Windows.NET Services

This is similar to a question that was probably asked earlier, but I could not find it, so I ask (maybe again).

I am writing a Windows service to track and process files a bit. All this does what I want, but I know that there are several places that my code can cause errors when reading / writing to the file system or the event log. I am going to use some attempts / catches where I expect that there will be errors, but I wanted to do some global error handling to pick up all these unforeseen problems. I'm not sure how to do this though.

My first thought was to look for some kind of global error handler, similar to what I find in ASP.NET projects, but didn't see anything on these lines.

Another thought was that I could just get all my top-level function calls and put try / catch around them. As I see it, this would mean trying / capturing all the code of my event handler and my methods of starting and stopping the service. This doesn't seem like a good way to do this, so I think I'm probably wrong. Can anyone suggest what should I do in terms of error handling?

I assume that in the service I do not want it to simply throw errors because it will potentially stop the service, or should I actually allow the service to fail and automatically restart it through the recovery mechanism that I already mentioned?

I highly doubt it, so please share your wisdom with me ... :)

+3
3

try/catch , . , .

, try/catch, try/catch , (, ), , , .

, , , , .

+6

Windows, .

dll. , DLL, try/catch. , .

, , dll try/catch , .

0

. ( )... , , try catch, .

EventLog . log4net + EventLogAppender .

It may still happen that the service is shutting down due to hardware problems such as full disks or memory problems. If you want it to restart automatically, you can install recovery in the NT service settings dialog box or in the installer.

0
source

Source: https://habr.com/ru/post/1754247/


All Articles