I want to know every exceptional situation while it is happened. I think I need to write code in every catch block
- send email
- send sms
- write it to log file
-
write event log
etc.try{
// do something
}catch(Exception ex){
// send me ex…
}
Is there any easy way to inject catch blocks without writing any code?
PS: While I’m reading the answer’s links, I came across some articles and I want to add quotas to my question.
Exception Handling Best Practices in .NET
Generic Exceptions caught should be published
It really doesn’t matter what you use for logging – log4net, EIF, Event Log, TraceListeners, text files, etc. What’s really important is: if you caught a generic Exception, log it somewhere. But log it only once – often code is ridden with catch blocks that log exceptions and you end up with a huge log, with too much repeated information to be useful.
You could create your own exception class which logs the exception message and/or stack trace. We do this using log4net and it works, but does require consistent implementation. You only really want to do this at a layer or tier boundary, since for most situations if you don’t know how to handle an exception it should just bubble up the call stack.
Something like: