ExceptionDlg.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* ========================================================================
  2. * Copyright (c) 2005-2019 The OPC Foundation, Inc. All rights reserved.
  3. *
  4. * OPC Foundation MIT License 1.00
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use,
  10. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * The complete license agreement can be found here:
  27. * http://opcfoundation.org/License/MIT/1.00/
  28. * ======================================================================*/
  29. using Opc.Ua;
  30. using System;
  31. using System.Collections.Generic;
  32. using System.ComponentModel;
  33. using System.Data;
  34. using System.Text;
  35. using System.Windows.Forms;
  36. namespace OpcUaHelper
  37. {
  38. /// <summary>
  39. /// A dialog that displays an exception trace in an HTML page.
  40. /// </summary>
  41. public partial class ExceptionDlg : Form
  42. {
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="ExceptionDlg"/> class.
  45. /// </summary>
  46. public ExceptionDlg()
  47. {
  48. InitializeComponent();
  49. }
  50. private Exception m_exception;
  51. /// <summary>
  52. /// Replaces all special characters in the message.
  53. /// </summary>
  54. private string ReplaceSpecialCharacters(string message)
  55. {
  56. message = message.Replace("&", "&#38;");
  57. message = message.Replace("<", "&lt;");
  58. message = message.Replace(">", "&gt;");
  59. message = message.Replace("\"", "&#34;");
  60. message = message.Replace("'", "&#39;");
  61. message = message.Replace("\r\n", "<br/>");
  62. return message;
  63. }
  64. private void AddBlock(StringBuilder buffer, string text)
  65. {
  66. AddBlock(buffer, text, 0);
  67. }
  68. private void AddBlock(StringBuilder buffer, string text, int level)
  69. {
  70. if (!String.IsNullOrEmpty(text))
  71. {
  72. if (level > 0)
  73. {
  74. if (level == 1)
  75. {
  76. buffer.Append("<tr style='background-color:#990000;");
  77. }
  78. else if (level == 2)
  79. {
  80. buffer.Append("<tr style='background-color:#CC6600;");
  81. }
  82. else
  83. {
  84. buffer.Append("<tr style='background-color:#999999;");
  85. }
  86. buffer.Append("color:#FFFFFF;font-weight:bold;font-size:10pt;font-family:Verdana'><td>");
  87. buffer.Append("<p>");
  88. }
  89. else
  90. {
  91. buffer.Append("<tr style='font-size:10pt;font-family:Verdana'><td>");
  92. buffer.Append("<p>");
  93. }
  94. buffer.Append(ReplaceSpecialCharacters(text));
  95. buffer.Append("</p>");
  96. buffer.Append("</td></tr>");
  97. }
  98. }
  99. private void Add(StringBuilder buffer, Exception e, bool showStackTrace)
  100. {
  101. AddBlock(buffer, "EXCEPTION (" + e.GetType().Name + ")", 1);
  102. AddBlock(buffer, e.Message);
  103. ServiceResultException sre = e as ServiceResultException;
  104. if (sre != null)
  105. {
  106. ServiceResult sr = new ServiceResult(sre);
  107. while (sr != null)
  108. {
  109. AddBlock(buffer, "SERVICE RESULT (" + new StatusCode(sr.Code).ToString() + ")", 2);
  110. string text = (sr.LocalizedText != null) ? sr.LocalizedText.Text : null;
  111. if (text != e.Message)
  112. {
  113. AddBlock(buffer, text);
  114. }
  115. AddBlock(buffer, sr.SymbolicId);
  116. AddBlock(buffer, sr.NamespaceUri);
  117. if (showStackTrace)
  118. {
  119. if (!String.IsNullOrEmpty(sre.AdditionalInfo))
  120. {
  121. AddBlock(buffer, "ADDITIONAL INFO (" + new StatusCode(sr.Code).ToString() + ")", 3);
  122. AddBlock(buffer, sre.AdditionalInfo);
  123. }
  124. }
  125. sr = sr.InnerResult;
  126. }
  127. }
  128. if (showStackTrace)
  129. {
  130. AddBlock(buffer, "STACK TRACE", 3);
  131. AddBlock(buffer, e.StackTrace);
  132. }
  133. }
  134. private void Show(bool showStackTrace)
  135. {
  136. StringBuilder buffer = new StringBuilder();
  137. buffer.Append("<html><body style='margin:0;width:100%'>");
  138. //buffer.Append(ExceptionBrowser.Parent.Width);
  139. //buffer.Append("px'>");
  140. buffer.Append("<table border='1' style='width:100%'>");
  141. Exception e = m_exception;
  142. while (e != null)
  143. {
  144. Add(buffer, e, showStackTrace);
  145. e = e.InnerException;
  146. }
  147. buffer.Append("</table>");
  148. buffer.Append("</body></html>");
  149. ExceptionBrowser.DocumentText = buffer.ToString();
  150. }
  151. /// <summary>
  152. /// Displays the exception in a dialog.
  153. /// </summary>
  154. public static void Show(string caption, Exception e)
  155. {
  156. // check if running as a service.
  157. if (!Environment.UserInteractive)
  158. {
  159. Utils.Trace(e, "Unexpected error in '{0}'.", caption);
  160. return;
  161. }
  162. new ExceptionDlg().ShowDialog(caption, e);
  163. }
  164. /// <summary>
  165. /// Display the exception in the dialog.
  166. /// </summary>
  167. public void ShowDialog(string caption, Exception e)
  168. {
  169. if (!String.IsNullOrEmpty(caption))
  170. {
  171. Text = caption;
  172. }
  173. m_exception = e;
  174. #if _DEBUG
  175. ShowStackTracesCK.Checked = true;
  176. #else
  177. ShowStackTracesCK.Checked = false;
  178. #endif
  179. Show(ShowStackTracesCK.Checked);
  180. ShowDialog();
  181. }
  182. private void OkButton_Click(object sender, EventArgs e)
  183. {
  184. Close();
  185. }
  186. private void ShowStackTracesCK_CheckedChanged(object sender, EventArgs e)
  187. {
  188. Show(ShowStackTracesCK.Checked);
  189. }
  190. }
  191. }