开发者

How to inject inside catch block to send exception email/sms/event log in C#

开发者 https://www.devze.com 2023-02-04 06:40 出处:网络
I want to know every exceptional situation while it is happened. I think I need to write code in every catch block

I want to know every exceptional situation while it is happened. I think I need to write code in every catch block

  1. send email
  2. send sms
  3. write it to log file
  4. 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开发者_如何学C 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:

[Serializable]
public class FooException : Exception
{
    // Logger configured for email, file, etc.
    static ILog _log = LogFactory.Create();

    public FooException(string message, Exception innerException)
        : base(message, innerException)
    {
        _log.Error(message, innerException);
    }

    // ...
}

public class FooDataAccess<T> : IFooRepository
{
    public T GetFoo()
    {
        // Consider creating general helper methods with 
        // Action, Func, parameters so that you only have
        // to code the try ... catch block once.
        try
        {              
            // all exceptions caught
        }
        catch (Exception e)
        {
            throw new FooException(Exceptions.GetFooException, e);
        }
    }     
}


You can use aspect oriented programming with PostSharp.

However, having a single error handler at the top of you program is a better choice in my opinion, if all you are doing is gathering information and logging. You will get the stack trace that way.


You can use Spring.net, Unity, or MEF which are all open source and have good documentation and samples over web. but if you have just this issue I'll personally prefer do it by myself or use closures to handle this.

For exception handling you can use Enterprise Library and also see the Quick start, with this you should just have some config and your exception handler, learning simple configuration and using it (if you have handler) takes less than 10 minutes (or at most one hour).


While the idea of getting every single exception seems interesting, it may be overkill.

In my experience, I've found that having a logging framework (such as log4net) which is run-time configurable handles this scenario quite nicely.

There are a number of benefits. The biggest benefit I've found is that if you use a logger-per-class, then you can tune when you want to receive exception emails on the class level.

This means that for those exceptions which are considered known and not critical, you can simply alter the configuration to turn off (say) email reporting for that exception, but leave on logging to a textfile on disk.

Because all of these options are set up in your logging configuration, the operations team responsible for monitoring it can alter the configuration without needing to alter your application code.

With some logging frameworks you can even do this reconfiguration while the application is still running.

The only down side, is that you need to be diligent in sprinkling logging information wherever you have a catch block.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号