code_junkie

How to Trace all local variables when an exception occurs

Nov 12th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. private static void TraceMethodArguments(MethodExecutionEventArgs eventArgs)
  2. {
  3. object[] parameters = eventArgs.GetReadOnlyArgumentArray();
  4.  
  5. if (parameters != null)
  6. {
  7. string paramValue = null;
  8. foreach (object p in parameters)
  9. {
  10. Type _type = p.GetType();
  11. if (_type == typeof(string) || _type == typeof(int) || _type == typeof(double) || _type == typeof(decimal))
  12. {
  13. paramValue = (string)p;
  14. }
  15. else if (_type == typeof(XmlDocument))
  16. {
  17. paramValue = ((XmlDocument)p).OuterXml;
  18. }
  19. else
  20. { //try to serialize
  21. try
  22. {
  23. XmlSerializer _serializer = new XmlSerializer(p.GetType());
  24. StringWriter _strWriter = new StringWriter();
  25.  
  26. _serializer.Serialize(_strWriter, p);
  27. paramValue = _strWriter.ToString();
  28. }
  29. catch
  30. {
  31. paramValue = "Unable to Serialize Parameter";
  32. }
  33. }
  34. Trace.TraceInformation("[" + Process.GetCurrentProcess().Id + "-" + Thread.CurrentThread.ManagedThreadId.ToString() + "]" + " Parameter: " + paramValue);
  35. }
  36. }
  37. }
Add Comment
Please, Sign In to add comment