PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Catel.Core35/Diagnostics/TraceListenerBase.cs

#
C# | 177 lines | 91 code | 21 blank | 65 comment | 18 complexity | 9a814985b5a112e290d7f7a3ef4ff310 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="TraceListenerBase.cs" company="Catel development team">
  3. // Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
  4. // </copyright>
  5. // <summary>
  6. // Class that implements a trace listener.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. namespace Catel.Diagnostics
  10. {
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. /// <summary>
  14. /// Class that implements a trace listener.
  15. /// </summary>
  16. public abstract class TraceListenerBase : TraceListener
  17. {
  18. #region Constants
  19. /// <summary>
  20. /// Trace message for the ItemsSource timing issue. This line should be ignored.
  21. /// </summary>
  22. private const string ItemsSourceTimingIssueTrace = "ContentAlignment; DataItem=null;";
  23. #endregion
  24. #region Variables
  25. #endregion
  26. #region Constructor & destructor
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="TraceListenerBase"/> class.
  29. /// </summary>
  30. /// <param name="name">The name of the <see cref="T:System.Diagnostics.TraceListener"/>.</param>
  31. protected TraceListenerBase(string name)
  32. {
  33. Name = name;
  34. ActiveTraceLevel = TraceLevel.Info;
  35. TraceSourceCollection = new List<TraceSource>();
  36. TraceSourceCollection.Add(PresentationTraceSources.DataBindingSource);
  37. TraceSourceCollection.Add(PresentationTraceSources.DependencyPropertySource);
  38. TraceSourceCollection.Add(PresentationTraceSources.MarkupSource);
  39. TraceSourceCollection.Add(PresentationTraceSources.ResourceDictionarySource);
  40. foreach (TraceSource traceSource in TraceSourceCollection)
  41. {
  42. traceSource.Listeners.Add(this);
  43. }
  44. }
  45. #endregion
  46. #region Properties
  47. /// <summary>
  48. /// Gets or sets the trace source collection.
  49. /// </summary>
  50. /// <value>The trace source collection.</value>
  51. private List<TraceSource> TraceSourceCollection { get; set; }
  52. /// <summary>
  53. /// Gets or sets the active trace type.
  54. /// </summary>
  55. public TraceLevel ActiveTraceLevel { get; set; }
  56. #endregion
  57. #region Methods
  58. /// <summary>
  59. /// Writes trace information, a formatted array of objects and event information to the listener specific output.
  60. /// </summary>
  61. /// <param name="eventCache">A <see cref="T:System.Diagnostics.TraceEventCache"/> object that contains the current process ID, thread ID, and stack trace information.</param>
  62. /// <param name="source">A name used to identify the output, typically the name of the application that generated the trace event.</param>
  63. /// <param name="eventType">One of the <see cref="T:System.Diagnostics.TraceEventType"/> values specifying the type of event that has caused the trace.</param>
  64. /// <param name="id">A numeric identifier for the event.</param>
  65. /// <param name="format">A format string that contains zero or more format items, which correspond to objects in the <paramref name="args"/> array.</param>
  66. /// <param name="args">An object array containing zero or more objects to format.</param>
  67. /// <PermissionSet>
  68. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
  69. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode"/>
  70. /// </PermissionSet>
  71. public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
  72. {
  73. TraceEvent(eventCache, source, eventType, id, string.Format(format, args));
  74. }
  75. /// <summary>
  76. /// Writes trace information, a message, and event information to the listener specific output.
  77. /// </summary>
  78. /// <param name="eventCache">A <see cref="T:System.Diagnostics.TraceEventCache"/> object that contains the current process ID, thread ID, and stack trace information.</param>
  79. /// <param name="source">A name used to identify the output, typically the name of the application that generated the trace event.</param>
  80. /// <param name="eventType">One of the <see cref="T:System.Diagnostics.TraceEventType"/> values specifying the type of event that has caused the trace.</param>
  81. /// <param name="id">A numeric identifier for the event.</param>
  82. /// <param name="message">A message to write.</param>
  83. /// <PermissionSet>
  84. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/>
  85. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode"/>
  86. /// </PermissionSet>
  87. public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
  88. {
  89. if (message.Contains(ItemsSourceTimingIssueTrace))
  90. {
  91. return;
  92. }
  93. switch (eventType)
  94. {
  95. case TraceEventType.Error:
  96. if ((ActiveTraceLevel == TraceLevel.Error) ||
  97. (ActiveTraceLevel == TraceLevel.Warning) ||
  98. (ActiveTraceLevel == TraceLevel.Info) ||
  99. (ActiveTraceLevel == TraceLevel.Verbose))
  100. {
  101. OnTrace(message, eventType);
  102. }
  103. break;
  104. case TraceEventType.Warning:
  105. if ((ActiveTraceLevel == TraceLevel.Warning) ||
  106. (ActiveTraceLevel == TraceLevel.Info) ||
  107. (ActiveTraceLevel == TraceLevel.Verbose))
  108. {
  109. OnTrace(message, eventType);
  110. }
  111. break;
  112. case TraceEventType.Information:
  113. if ((ActiveTraceLevel == TraceLevel.Info) ||
  114. (ActiveTraceLevel == TraceLevel.Verbose))
  115. {
  116. OnTrace(message, eventType);
  117. }
  118. break;
  119. // Everything else is verbose
  120. ////case TraceEventType.Verbose:
  121. default:
  122. if (ActiveTraceLevel == TraceLevel.Verbose)
  123. {
  124. OnTrace(message, TraceEventType.Verbose);
  125. }
  126. break;
  127. }
  128. }
  129. /// <summary>
  130. /// Writes text to the output window.
  131. /// </summary>
  132. /// <param name="message">Message to write.</param>
  133. public override void Write(string message)
  134. {
  135. WriteLine(message);
  136. }
  137. /// <summary>
  138. /// Writes a line of text to the output window.
  139. /// </summary>
  140. /// <param name="message">Message to write.</param>
  141. public override void WriteLine(string message)
  142. {
  143. if (ActiveTraceLevel == TraceLevel.Verbose)
  144. {
  145. OnTrace(message, TraceEventType.Verbose);
  146. }
  147. }
  148. /// <summary>
  149. /// Called when a new trace has occurred.
  150. /// </summary>
  151. /// <param name="message">The message.</param>
  152. /// <param name="eventType">Type of the event.</param>
  153. protected abstract void OnTrace(string message, TraceEventType eventType);
  154. #endregion
  155. }
  156. }