Cómo pasar Exception.Data

I have a project where in my error reporting I want to send a complete error report via email. I'm catching the exception and passing all the information to another method that is responsible for sending an email to support.

I'm adding important custom values to the Exception.Data object. However, I'm unable to figure out what type I need to use in my method for the entire Data object to be passed.

Aquí está mi código:

catch (Exception exc)
    exc.Data.Add("ID", id);
    emailConnector.sendErrorEmail(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), exc.Message, exc.Data, exc.StackTrace);

I'm passing exc.Data to the sendErrorEmail method so in the parameter declarations I have:

public void sendErrorEmail(string origin, string error_message, IDictionary<object, object> data, string stack)

But I can't seem to get the type declaration correct for Exception.Data. I prefer to keep the variables and structure the same (no new variables) because I currently have dozens of methods calling sendErrorEmail in the Catch block.

What type should I be using or is there an easier way to get this Exception.Data passed correctly with the extra pertinent information I included in it?

Muchas Gracias

preguntado el 09 de marzo de 12 a las 15:03

Create your own Exception if it has a definitive use. msdn.microsoft.com/en-us/library/ms229064.aspx -

Why don't you simply pass exc to your function and then access exc.data? -

@GianT971 because I have dozens and dozens of Methods that would need to have their Catch block restructured completely. I'm wanting to make only minimal changes so that the exc.Data can be passed and its values retrieved inside SendErrorEmail -

2 Respuestas

En cuanto al página de ayuda it seems that your problem is that the type of exc.Data is IDictionary whereas you are declaring it as IDictionary<object, object>.

You should have had an error message telling you the type that it was having trouble converting between...

Also unless this method is being used for things other than exceptions I would go with the suggestion in comments to pass the whole exception through and disect it afterwards rather than passing through multiple properties.

respondido 09 mar '12, 15:03

Was thinkikng of this type too - GianT971

Ahhh silly me. I see the issue now. I needed to declare my parameter on SendErrorEmail - IDictionary data instead of IDictionary<object, object> data - Cifrado

The type of Exception.Data is ListDictionaryInternal. It's a private framework class so you cannot use it directly, but it implements following public interfaces :

[0]: {Name = "IDictionary" FullName = "System.Collections.IDictionary"}
[1]: {Name = "ICollection" FullName = "System.Collections.ICollection"}
[2]: {Name = "IEnumerable" FullName = "System.Collections.IEnumerable"}

respondido 09 mar '12, 15:03

I think its more important what the documentation declares it as. In this case it declares it as IDictionary which means it has to implement the other two as well but looking at the implementation of the internal object is bad, you should only ever look at the public interface. - Chris

As I say, you cannot use the private class, looking for it's name isn't bad... FYI "public interface IDictionary : ICollection, IEnumerable", so it's ok the use any of the 3... - Julien

My point was that you shouldn't be looking at the implementation of exception to find anything out, if you are doing something that finds its name then you are probably going wrong (unless it is just for curiosity). You should just look at the Exception.Data Property and that it is IDictionary and use that. - Chris

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.