Bruno Ferreira
Bruno Ferreira

Reputation: 484

Exception that handles and logs any kind of Exception

I don't know if this idea is possible to implement. If not, i would appreciate some alternative.

I need to create an custom exception class that can catch any time of error, as do the System.Exception class. But on catching an exception, it would be able to log the error.

This code is my implementation, but it does not catch any type of exceptions.

public class CustomException : Exception
{
    public CustomException()
    {
          //Log error
    }

    public CustomException(string message)
    : base(message)
    {
           //Log error
    }

    public CustomException(string message, Exception inner)
    : base(message, inner)
    {
           //Log error
    }
}

Thanks in advance.

Upvotes: 0

Views: 52

Answers (2)

Eric Burdo
Eric Burdo

Reputation: 813

Have you considered something like ELMAH It will log any unhandled exceptions to a system of your choice (text file, event log, SQL table, etc).

Upvotes: 1

Norbert Huurnink
Norbert Huurnink

Reputation: 1316

An exception object won't do the "catching" part. You need to have something else to do that, there are several ways to do this, depending on what type of application you have.

One is explained here:

https://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling

Or: https://msdn.microsoft.com/en-us/library/24395wz3.aspx

OWIN: https://blog.kloud.com.au/2016/03/23/aspnet-core-tips-and-tricks-global-exception-handling/

Console app: .NET Global exception handler in console application

Upvotes: 1

Related Questions