How to log all thrown exceptions?

泄露秘密 提交于 2019-11-27 04:46:32
yas4891

I guess the feature you are searching for is called FirstChanceException and can be accessed via the AppDomain.FirstChanceException Event

Essentially this event provides a hook to get information about any (managed) exception getting thrown in the AppDomain. You can not handle the Exception this way! It is only a sort of notification


Update: regarding your comment about a "swallowed exception" on another answer - and just a shot into the dark:
On x64 systems exceptions that get thrown in a windows' onLoad method can not be caught in your Main() method.
see this SO article for reference


Update 2: As for Threads I think that you would have to implement it yourself. This would involve some sort of polling and would harm performance, but I guess for debugging it is OK in most cases. This could be done using

var threads = Process.GetCurrentProcess().Threads;

You might go with aspects. If for example you take PostSharp, and write an aspect that creates a try-catch for every function, logging is done in the aspect. After logging just rethrow it.

Example code from their website to have a complete answer with demo code:

/// <summary>
/// Aspect that, when applied on a method, catches all its exceptions,
/// assign them a GUID, log them, and replace them by an <see cref="InternalException"/>.
/// </summary>
[Serializable]
public class ExceptionPolicyAttribute : OnExceptionAspect
{
    /// <summary>
    /// Method invoked upon failure of the method to which the current
    /// aspect is applied.
    /// </summary>
    /// <param name="args">Information about the method being executed.</param>
    public override void OnException(MethodExecutionArgs args)
    {
        Guid guid = Guid.NewGuid();

        Trace.TraceError("Exception {0} handled by ExceptionPolicyAttribute: {1}",
            guid, args.Exception.ToString());

        throw new InternalException(
            string.Format("An internal exception has occurred. Use the id {0} " +
                "for further reference to this issue.", guid));
    }
}

Edit:

You could use any logger like log4net, NLog or the enterprise library ( my preferred way of doing logging and some other stuff ). But that isn't really the task here. The task is - IMHO - to inject logging into the project with as less manual coding as possible.

try using Log4Net - its a great logger and used a lot in these scenarios http://sourceforge.net/projects/log4net/

For handled exceptions you'd most likely need to log them explicitly. Even if that's not the case semantically there's a huge difference to handled and unhandled exceptions.

Handled exceptions are no longer an exceptional situation. Some one writing the code said. I know how to handle this exception and proceed correctly afterwards.

For unhandled exceptions have a look at Elmah

You could attach a debugger, like WinDbg, to your process, and get it to break on any first chance CLR exceptions; this will include exceptions in the third party library. See here for an example of how to do this.

You can use your own logging system or use third party lib called log4net.

Alternatively to Log4Net proposed by some guys, you could also use NLog. I don't know the differences between the two solutions, I just know that I'm happy with NLog.

If your thing is a web thing then you can use https://elmah.github.io/ to automatically log errors (without even stopping your service!)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!