Advertisement
Guest User

UnhandledException handler

a guest
Sep 25th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  2. {
  3.     string msg = null;
  4.     try
  5.     {
  6.         if (e.ExceptionObject is Exception)
  7.         {
  8.             Exception ex = (Exception) e.ExceptionObject;
  9.             msg = renderException(ex);
  10.         }
  11.         else
  12.         {
  13.             msg = e.ExceptionObject.ToString();
  14.         }
  15.  
  16.         CFAPControlLibrary.Service<ICFAPService>.Use(srv =>
  17.         {
  18.             srv.Log(string.Format("Unhandled exception in client: " + msg)); // wcf service call
  19.         });
  20.     }
  21.     catch
  22.     {
  23.         // couldn't save the exception details at provider side
  24.         if (msg != null)
  25.         {
  26.             File.AppendAllText("error.log", msg);
  27.         }
  28.     }
  29.     try
  30.     {
  31.         if (File.Exists(dumperPath)) // path to procdump.exe
  32.         {
  33.             DirectoryInfo di = new DirectoryInfo(dumperPath);
  34.             string args = string.Format("-ma -accepteula {0}", System.AppDomain.CurrentDomain.FriendlyName);
  35.             Process.Start(di.FullName, args);
  36.         }
  37.     }
  38.     catch
  39.     {
  40.         // coudn't write crash dump, no way to handle this
  41.     }
  42. }
  43.  
  44. string renderException(Exception e, StringBuilder sw = null)
  45. {
  46.     if (sw == null)
  47.     {
  48.         sw = new StringBuilder();
  49.     }
  50.     sw.AppendLine(e.GetType().ToString());
  51.     sw.AppendLine(e.Message);
  52.     sw.AppendLine(e.StackTrace);
  53.  
  54.     if (e.InnerException != null)
  55.     {
  56.         sw.AppendLine("Inner exception: ");
  57.         renderException(e.InnerException, sw);
  58.     }
  59.  
  60.     return sw.ToString();
  61. }A
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement