Question

I am handling errors using IErrorHandler in WCF REST. I need to log class name, method name, line number, error code etc. and i find out the soultions to all this.

But I am thinking any possibility or options to get 1ErrorLevel1 like windows event viewer. Eg: Warning, Information, Error.

Actually if I do like this my applciation exceptions can be put as Information. This can be work simple create an custom Exception class.

  1. But my question is for other exceptions (other than my application exceptions) any option to get this level based on the error. I don't need to set this level for other types of erros.

  2. Actually i am logging the exception in HandleError event. Now i am just throw my exceptions as applicationexception.

Just see my Service method code below:

        if(!long.TryParse("534543534", out value))
        {
            throw new ApplicationException("Input is Not Valid!!!");
        }

So in my error handling i got this as application exception. Here i dont knwo this is because of FormatException, Overflow Exception, ArgumentNullException. How i handle this. I can add the original exception as innerexception but need to wrap try..catch in each service method to get the exception. Is this good practice when i have the IErrorHandler methods. Please suggest good approach for this

Was it helpful?

Solution

In .NET, an exception is a severe error that interrupts the process of your application. The framework does (for the most part) not provide an error level as all exceptions are basically errors. If an exception is thrown, this always means that there is an error that the application cannot continue with.

If you want to discern between the exception types and write some as informational messages or warnings to your log, you'd have to inspect the exceptions in your error handler, e.g. by code similar to this:

public class MyErrorHandler : IErrorHandler
{
    // ...
    private static readonly Dictionary<Type, TraceLevel> _exceptionTraceLevelMappings;

    static MyErrorHandler()
    {
        _exceptionTraceLevelMappings = new Dictionary<Type, TraceLevel>();
        _exceptionTraceLevelMappings.Add(typeof(ApplicationException), TraceLevel.Information);
        _exceptionTraceLevelMappings.Add(typeof(ArgumentException), TraceLevel.Warning);
    }

    private static TraceLevel GetLevelByExceptionType(Type exType)
    {
        // You might want to add a more sophisticated approach here (e.g. for base classes)
        if (_exceptionTraceLevelMappings.ContainsKey(exType))
            return _exceptionTraceLevelMappings[exType];
        return TraceLevel.Error;
    }

    // ...
}

Based upon the comments, you want to discern between errors that are raised to coding mistakes and input data validation errors. In this case, you'd need to implement a CustomException type and use TryParse to validate input data:

public class MyValidationException : Exception
{
    public MyValidationException(string message)
        : base(message)
    {
    }

    // A custom exceptions needs several constructors, so add them also
}

In your service code, you'd use TryParse as in your sample:

if(!long.TryParse("534543534", out value))
{
    throw new MyValidationException("Input is Not Valid!!!");
}

In the mapping dictionary, you can register your exception type and assign TraceLevel.Information:

// ...
_exceptionTraceLevelMappings.Add(typeof(MyValidationException), TraceLevel.Information);
// ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top