28

How to log any exceptions that were thrown and catched? Something like Visual Studio's IntelliTrace do. Or is there a way to integrate InteliTrace into debug version of application and then view its logs?

Update: I'll clarify that a bit. I want standard .txt (or any custom) logs, the format doesn't matter. The main point is I want to log all exceptions that occurred in all third-party libraries without adding code to them.

Poma
  • 8,174
  • 18
  • 82
  • 144

8 Answers8

33

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;
Community
  • 1
  • 1
yas4891
  • 4,774
  • 3
  • 34
  • 55
  • That's it! Is there some similar way to discover when (and where) new threads are created? – Poma Aug 16 '11 at 09:56
  • I updated my answer. I don't think that there is a built-in event for this – yas4891 Aug 16 '11 at 10:05
  • @yas4891 Do you know of a way to filter the logged exceptions to only exceptions within my codebase? The FirstChanceExceptionHandler is logging exceptions thrown in 3rd party dlls – Justin Mar 26 '18 at 23:29
5

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.

Sascha
  • 10,231
  • 4
  • 41
  • 65
1

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

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
1

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

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • We having an obscure bug that has something to do with swallowed exception in third party library. – Poma Aug 16 '11 at 09:32
  • @Poma Does this "swallowing" happen in some "onLoad" method in WPF or WinForms? – yas4891 Aug 16 '11 at 09:56
  • No, it's about networking. Some function seems to send some trash to socket and swallow occurred error. – Poma Aug 16 '11 at 10:01
1

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.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
0

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

Loofer
  • 6,841
  • 9
  • 61
  • 102
0

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

Zenwalker
  • 1,883
  • 1
  • 14
  • 27
  • But i guess the OP refferring to wanting logs in event viewer (in windows control panel). I dont know, i am assuming. – Zenwalker Aug 16 '11 at 09:27
  • 1
    Poma, You cant log for 3rd party libs unless they throw exceptions to you (the caller), just a reminder mate. – Zenwalker Aug 16 '11 at 09:35
  • Yeah log4net is the best thing you could take... dont know any better logging Framework... – Smokefoot Aug 16 '11 at 09:36
  • There is another 1 basedo n log4net in codeplex. i forgot the name. Dont know how far its better than log4net. – Zenwalker Aug 16 '11 at 09:37
  • 1
    @zenwalker: this is only partially true. .NET provides a means to log **all** (managed) exceptions. See my answer for more information – yas4891 Aug 16 '11 at 09:47
  • @yas4891 Well that has a limitation that the 3rd party lib has to be a manged code. Any ways thanks for the heads up. – Zenwalker Aug 16 '11 at 09:50
  • @zenwalker: you are right. That might be a bit misguiding. I'll update my comment and answer. – yas4891 Aug 16 '11 at 09:52
0

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.

Otiel
  • 18,404
  • 16
  • 78
  • 126