<p>I would like to have some kind of catch-all exceptions mechanism in the root of my code, so when an app terminates unexpectedly I can still provide some useful logging.</p>

<p>Something along the lines of</p>

<pre><code>static void Main () {
    if (Debugger.IsAttached)
        RunApp();
    else {
        try {
            RunApp();
        }
        catch (Exception e) {
            LogException(e);
            throw;
        }
    }
 }
</code></pre>

<p>While this all works fine, my problem is when I want to attach the debugger after the exception has been raised.</p>

<p>Since the exception escapes to the runtime, windows will prompt to attach visual studio, except, since it has been rethrown, all locals and parameters further up the stack have been lost.</p>

<p>Is there anyway to log these exceptions, while still providing a way to attach the debugger and retain all useful information?</p>

Shouldn't simply do a
```
```
Exception e1 = e;
LogException(e);
throw(e1);
```
```
inside the catch do the trick (at least you can examine the outer exception)?