views:

66

answers:

6

I am trying to understand why a deployed application is not working properly. I have pinned down the problem to a particular update routine. Sadly, the routine is embedded into a do nothing try-catch.

bool requireUpdate = false;
try
{
    requireUpdate = !client.IsUpToDate();
}
catch{ }

I need a way to get the exception without having to recompile and do a deploy.

Is there is a way to modify the app.config file so it can trace all handled exceptions to a log file, regardless of how they have been handled?

A: 

This solution may be what you are looking for.
Basically the instruction is to use this block of code:

System.Diagnostics.EventLog.WriteEntry("MyEventSource", exception.StackTrace,
System.Diagnostics.EventLogEntryType.Warning);

Additionally the app.config from that same link seems to be like so: <system.diagnostics> <switches>

    <add name="MySwitch" value="Verbose" />
</switches>
<trace autoflush="true">
    <listeners>
        <add name="EventLogger"
             type="System.Diagnostics.EventLogTraceListener"
             initializeData="NameOfYourApplication" />
    </listeners>
</trace>

Woot4Moo
err still messing up my syntax somehow
Woot4Moo
@Woot4Moo, this shows how to use tracing but I don't see how this could be helpful in this situation.
Darin Dimitrov
WITHOUT RECOMPILING!!! read it carefully....
Aliostad
Without recompiling... sweet mother of jeebus
Woot4Moo
+1  A: 

Unfortunately there is nothing that can be done about handled exceptions, as they are handled (even if very badly, as in your case).

The best way to get them will be to rewrite/recompile/deploy.

Oded
+1 for rewriting the code. Today it's this function, tomorrow another. Fix the bad code, that's the only solution.
Darin Dimitrov
There is something that can be done... CLR profiling
Aliostad
@Aliostad - you obviously never used profilers before. Chances are that the application will have to be stopped before/after each profiling session and it could very well be that it is not an option on a production environment anyway (installing the profiler and running it may have an adverse impact on the system).
Oded
A: 

UPDATED

CLR profiling can help ya!

Have a look at this link: http://msdn.microsoft.com/en-us/magazine/cc301839.aspx

You can basically track your exceptions:

"he profiling API offers an extensive set of callbacks that depict in detail the search, unwind, and finally phases of the exception handling cycle. An interesting profiling scenario would monitor managed exceptions and provide additional information such as the Win32 thread that threw the exception, the function that was on the top of the stack, the arguments of that function, and the locals that were in scope at the point where the exception occurred."

Aliostad
Its not crashing; its doing nothing.
Pierre-Alain Vigeant
@Aliostad, what crashes are you talking about? It's swallowing which is worse than a crash:-)
Darin Dimitrov
Note the update. @Darin @Pierre
Aliostad
@Aliostad, yes using the profiler API is definitely one way of doing it but I think that the time you would waste into it would be better used to rewrite this crap of a code. Also using the CLR API requires some administrative privileges and will probably crash the application after finished using it which is not a very good idea to do in production.
Darin Dimitrov
A: 

Tricks I've used for this in Java:

Go through all of the catch blocks in the application, looking for empty ones. Add a debug or trace message to the block saying something like, "Catching expected exception", and the exception. You should have this anyway, and set a policy that silently swallowing exceptions is absolutely not acceptable.

If that fails to flush out the exception, recompile the base Exception, add a block to the no-arg constructor to log a stack trace if the application is in debug (or trace) mode. Obviously this is easier to do if your application/library has its own Exception heirarchy, but there are ways to inject this behavior (here the way to do this in C# would be distinct from how I'd accomplish this)

Jason
He wants a solution that does not require recompiles.
Oded
A: 

There's another option, but it may or may not be pretty: Moles.

Technically used for unit testing, Moles allows you to replace any .NET method with a delegate. You might be able to use it to replace that method with your delegate that does what you want instead.

You might be able to replace the original method with a delegate that calls client.IsUpToDate() so you can snag the exception instead?

Moles is part of Pex: http://research.microsoft.com/en-us/projects/pex/default.aspx

John Gardner
+1  A: 

Use low level debuggers, such as WinDbg you will be able to know first chance exceptions.

Lex Li