ExceptionEvaluator.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. #if NETSTANDARD1_3
  21. using System.Reflection;
  22. #endif
  23. namespace log4net.Core
  24. {
  25. /// <summary>
  26. /// An evaluator that triggers on an Exception type
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// This evaluator will trigger if the type of the Exception
  31. /// passed to <see cref="M:IsTriggeringEvent(LoggingEvent)"/>
  32. /// is equal to a Type in <see cref="ExceptionType"/>. ///
  33. /// </para>
  34. /// </remarks>
  35. /// <author>Drew Schaeffer</author>
  36. public class ExceptionEvaluator : ITriggeringEventEvaluator
  37. {
  38. /// <summary>
  39. /// The type that causes the trigger to fire.
  40. /// </summary>
  41. private Type m_type;
  42. /// <summary>
  43. /// Causes subclasses of <see cref="ExceptionType"/> to cause the trigger to fire.
  44. /// </summary>
  45. private bool m_triggerOnSubclass;
  46. /// <summary>
  47. /// Default ctor to allow dynamic creation through a configurator.
  48. /// </summary>
  49. public ExceptionEvaluator()
  50. {
  51. // empty
  52. }
  53. /// <summary>
  54. /// Constructs an evaluator and initializes to trigger on <paramref name="exType"/>
  55. /// </summary>
  56. /// <param name="exType">the type that triggers this evaluator.</param>
  57. /// <param name="triggerOnSubClass">If true, this evaluator will trigger on subclasses of <see cref="ExceptionType"/>.</param>
  58. public ExceptionEvaluator(Type exType, bool triggerOnSubClass)
  59. {
  60. if (exType == null)
  61. {
  62. throw new ArgumentNullException("exType");
  63. }
  64. m_type = exType;
  65. m_triggerOnSubclass = triggerOnSubClass;
  66. }
  67. /// <summary>
  68. /// The type that triggers this evaluator.
  69. /// </summary>
  70. public Type ExceptionType
  71. {
  72. get { return m_type; }
  73. set { m_type = value; }
  74. }
  75. /// <summary>
  76. /// If true, this evaluator will trigger on subclasses of <see cref="ExceptionType"/>.
  77. /// </summary>
  78. public bool TriggerOnSubclass
  79. {
  80. get { return m_triggerOnSubclass; }
  81. set { m_triggerOnSubclass = value; }
  82. }
  83. #region ITriggeringEventEvaluator Members
  84. /// <summary>
  85. /// Is this <paramref name="loggingEvent"/> the triggering event?
  86. /// </summary>
  87. /// <param name="loggingEvent">The event to check</param>
  88. /// <returns>This method returns <c>true</c>, if the logging event Exception
  89. /// Type is <see cref="ExceptionType"/>.
  90. /// Otherwise it returns <c>false</c></returns>
  91. /// <remarks>
  92. /// <para>
  93. /// This evaluator will trigger if the Exception Type of the event
  94. /// passed to <see cref="M:IsTriggeringEvent(LoggingEvent)"/>
  95. /// is <see cref="ExceptionType"/>.
  96. /// </para>
  97. /// </remarks>
  98. public bool IsTriggeringEvent(LoggingEvent loggingEvent)
  99. {
  100. if (loggingEvent == null)
  101. {
  102. throw new ArgumentNullException("loggingEvent");
  103. }
  104. if (m_triggerOnSubclass && loggingEvent.ExceptionObject != null)
  105. {
  106. // check if loggingEvent.ExceptionObject is of type ExceptionType or subclass of ExceptionType
  107. Type exceptionObjectType = loggingEvent.ExceptionObject.GetType();
  108. return exceptionObjectType == m_type || exceptionObjectType.IsSubclassOf(m_type);
  109. }
  110. else if (!m_triggerOnSubclass && loggingEvent.ExceptionObject != null)
  111. { // check if loggingEvent.ExceptionObject is of type ExceptionType
  112. return loggingEvent.ExceptionObject.GetType() == m_type;
  113. }
  114. else
  115. { // loggingEvent.ExceptionObject is null
  116. return false;
  117. }
  118. }
  119. #endregion
  120. }
  121. }