LogConfigSubFilter.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Globalization;
  2. using System.Text.RegularExpressions;
  3. using System.Xml;
  4. namespace Wayne.Lib.Log
  5. {
  6. /// <summary>
  7. /// Subfilter for ancestor and/ or category of the logging
  8. /// </summary>
  9. public class LogConfigSubFilter
  10. {
  11. /// <summary>
  12. /// Entitytype for ancestor
  13. /// </summary>
  14. public string AncestorEntityTypeRegexFilter { get; set; }
  15. /// <summary>
  16. /// Entitysubtype for the ancestor
  17. /// </summary>
  18. public string AncestorEntitySubTypeRegexFilter { get; set; }
  19. /// <summary>
  20. /// Id of the ancestor
  21. /// </summary>
  22. public string AncestorIdRegexFilter { get; set; }
  23. /// <summary>
  24. /// Category name of the log entry
  25. /// </summary>
  26. public string CategoryNameRegexFilter { get; set; }
  27. /// <summary>
  28. /// Filter level.
  29. /// </summary>
  30. public DebugLogLevel FilterLevel { get; set; }
  31. /// <summary>
  32. /// Constructor
  33. /// </summary>
  34. /// <param name="ancestorEntityTypeRegexFilter"></param>
  35. /// <param name="ancestorEntitySubTypeRegexFilter"></param>
  36. /// <param name="ancestorIdRegexFilter"></param>
  37. /// <param name="categoryNameRegexFilter"></param>
  38. /// <param name="filterLevel"></param>
  39. public LogConfigSubFilter(string ancestorEntityTypeRegexFilter, string ancestorEntitySubTypeRegexFilter, string ancestorIdRegexFilter, string categoryNameRegexFilter, DebugLogLevel filterLevel)
  40. {
  41. AncestorEntityTypeRegexFilter = ancestorEntityTypeRegexFilter;
  42. AncestorEntitySubTypeRegexFilter = ancestorEntitySubTypeRegexFilter;
  43. AncestorIdRegexFilter = ancestorIdRegexFilter;
  44. CategoryNameRegexFilter = categoryNameRegexFilter;
  45. FilterLevel = filterLevel;
  46. }
  47. /// <summary>
  48. /// Deserialization constructor
  49. /// </summary>
  50. /// <param name="logFilterAncestorNode"></param>
  51. internal LogConfigSubFilter(XmlNode logFilterAncestorNode)
  52. {
  53. XmlAttribute entityTypeAttribute = logFilterAncestorNode.Attributes["AncestorEntityType"];
  54. if (entityTypeAttribute != null)
  55. AncestorEntityTypeRegexFilter = entityTypeAttribute.Value;
  56. else
  57. AncestorEntityTypeRegexFilter = string.Empty;
  58. XmlAttribute entitySubTypeAttribute = logFilterAncestorNode.Attributes["AncestorEntitySubType"];
  59. if (entitySubTypeAttribute != null)
  60. AncestorEntitySubTypeRegexFilter = entitySubTypeAttribute.Value;
  61. else
  62. AncestorEntitySubTypeRegexFilter = string.Empty;
  63. XmlAttribute idAttribute = logFilterAncestorNode.Attributes["AncestorId"];
  64. if (idAttribute != null)
  65. AncestorIdRegexFilter = idAttribute.Value;
  66. else
  67. AncestorIdRegexFilter = string.Empty;
  68. XmlAttribute categoryNameAttribute = logFilterAncestorNode.Attributes["CategoryName"];
  69. if (categoryNameAttribute != null)
  70. CategoryNameRegexFilter = categoryNameAttribute.Value;
  71. else
  72. CategoryNameRegexFilter = string.Empty;
  73. XmlAttribute filterLevelttribute = logFilterAncestorNode.Attributes["FilterLevel"];
  74. if (filterLevelttribute != null)
  75. FilterLevel = EnumSupport.Parse(filterLevelttribute.Value, false, DebugLogLevel.Normal);
  76. else
  77. FilterLevel = DebugLogLevel.Normal;
  78. }
  79. /// <summary>
  80. /// ToString
  81. /// </summary>
  82. /// <returns></returns>
  83. public override string ToString()
  84. {
  85. return string.Concat("AncestorEntityType=\"", AncestorEntityTypeRegexFilter ?? string.Empty,
  86. "\", AncestorSubType=\"", AncestorEntitySubTypeRegexFilter ?? string.Empty,
  87. "\", AncestorId=\"", AncestorIdRegexFilter ?? string.Empty,
  88. "\", Category=\"", CategoryNameRegexFilter ?? string.Empty,
  89. "\", Level=\"", FilterLevel, "\"");
  90. }
  91. /// <summary>
  92. /// Serialization
  93. /// </summary>
  94. /// <param name="xmlWriter"></param>
  95. internal void WriteXml(XmlWriter xmlWriter)
  96. {
  97. xmlWriter.WriteStartElement("SubFilter");
  98. if (!string.IsNullOrEmpty(AncestorEntityTypeRegexFilter))
  99. xmlWriter.WriteAttributeString("AncestorEntityType", AncestorEntityTypeRegexFilter);
  100. if (!string.IsNullOrEmpty(AncestorEntitySubTypeRegexFilter))
  101. xmlWriter.WriteAttributeString("AncestorEntitySubType", AncestorEntitySubTypeRegexFilter);
  102. if (!string.IsNullOrEmpty(AncestorIdRegexFilter))
  103. xmlWriter.WriteAttributeString("AncestorId", AncestorIdRegexFilter);
  104. if (!string.IsNullOrEmpty(CategoryNameRegexFilter))
  105. xmlWriter.WriteAttributeString("CategoryName", CategoryNameRegexFilter);
  106. if (FilterLevel != DebugLogLevel.Normal)
  107. xmlWriter.WriteAttributeString("FilterLevel", FilterLevel.ToString());
  108. xmlWriter.WriteEndElement(); // SubFilter
  109. }
  110. internal bool MatchFilter(EntityCategory entityCategory, out DebugLogLevel debugLogLevel)
  111. {
  112. if (string.IsNullOrEmpty(CategoryNameRegexFilter) || Regex.IsMatch(entityCategory.CategoryString, CategoryNameRegexFilter, RegexOptions.IgnoreCase))
  113. {
  114. if (string.IsNullOrEmpty(AncestorEntityTypeRegexFilter) && string.IsNullOrEmpty(AncestorEntitySubTypeRegexFilter) && string.IsNullOrEmpty(AncestorIdRegexFilter))
  115. {
  116. debugLogLevel = FilterLevel;
  117. return true;
  118. }
  119. foreach (var identifableValue in entityCategory.GetAncestors())
  120. {
  121. bool entityTypeMatch = (string.IsNullOrEmpty(AncestorEntityTypeRegexFilter) || Regex.IsMatch(identifableValue.EntityType, AncestorEntityTypeRegexFilter, RegexOptions.IgnoreCase));
  122. if (!entityTypeMatch)
  123. continue;
  124. bool entitySubTypeMatch = (string.IsNullOrEmpty(AncestorEntitySubTypeRegexFilter) || Regex.IsMatch(identifableValue.EntitySubType, AncestorEntitySubTypeRegexFilter, RegexOptions.IgnoreCase));
  125. if (!entitySubTypeMatch)
  126. continue;
  127. bool idMatch = string.IsNullOrEmpty(AncestorIdRegexFilter);
  128. if (!idMatch)
  129. {
  130. if (identifableValue.Id == IdentifiableEntity.NoId)
  131. idMatch = Regex.IsMatch(string.Empty, AncestorIdRegexFilter, RegexOptions.IgnoreCase);
  132. else
  133. idMatch = Regex.IsMatch(identifableValue.Id.ToString(CultureInfo.InvariantCulture), AncestorIdRegexFilter, RegexOptions.IgnoreCase);
  134. }
  135. if (idMatch)
  136. {
  137. debugLogLevel = FilterLevel;
  138. return true;
  139. }
  140. }
  141. }
  142. debugLogLevel = DebugLogLevel.Excluded;
  143. return false;
  144. }
  145. }
  146. }