views:

267

answers:

4

Hi All

C# Development: I am calling a webservice/other methods in a try catch block with different parameters some of which may be custom objects. Please comment on the best way to log the parameters in a text file in case of an exception. Also is it possible to skip out some fields from an object (say for example if an object contains an image file which i dont want to log to the text file)

thanks for your time

A: 

The short answer is that you can't, not without going through hoops.

When you get an Exception, it contains stack trace info, but that information doesn't contain anything regarding the parameters that were passed to the method at the time.

To get around this, you have to store the parameters in your exception handler code so you can have access to them when the exception is handled.

For example, you would have an object which you would pass the parameter instances to (along with their names) and then call a method on that object in your catch block.

casperOne
A: 

You could give your objects a to_string method that displayed them however you want. If you want to be clever, you could even have them give their full details the first time, and subsequently refer back to the full version in later uses (lest your log grow unreadably cluttered).

If they don't have a good name to use as a reference, you could just assign them something (say "AxilWidget #7" or "PendingWhackQueue #14").

This can make the log much easier to read in many cases, and is vital if you're going to be recursively serializing object structure with loops. You'll have to take special care if the objects can change significantly between reporting though--say always include a summary containing those members, even in a back reference.

MarkusQ
A: 

I suggest you look into PostSharp. You can use attributes to declare that you want certain code to be executed "around" the normal method. That code can use the arguments to the method, and could make sure it only logged if the original code threw an exception, etc.

This kind of cross-cutting concern is exactly what AOP is designed for. I don't know how well PostSharp works with web services, but I can't imagine you're the first person to want this.

You'll be able to add properties to your attribute to specify any arguments to be skipped etc.

Jon Skeet
A: 

thank you all for your help