/Dependencies/log4net/Core/TimeEvaluator.cs

https://bitbucket.org/VirtualReality/software-testing · C# · 147 lines · 47 code · 11 blank · 89 comment · 5 complexity · f08dc5a0527e6989f59eb18a1282bfb3 MD5 · raw file

  1. #region Copyright & License
  2. //
  3. // Copyright 2001-2005 The Apache Software Foundation
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. #endregion
  18. using System;
  19. namespace log4net.Core
  20. {
  21. /// <summary>
  22. /// An evaluator that triggers after specified number of seconds.
  23. /// </summary>
  24. /// <remarks>
  25. /// <para>
  26. /// This evaluator will trigger if the specified time period
  27. /// <see cref="Interval"/> has passed since last check.
  28. /// </para>
  29. /// </remarks>
  30. /// <author>Robert Sevcik</author>
  31. public class TimeEvaluator : ITriggeringEventEvaluator
  32. {
  33. /// <summary>
  34. /// The time threshold for triggering in seconds. Zero means it won't trigger at all.
  35. /// </summary>
  36. private int m_interval;
  37. /// <summary>
  38. /// The time of last check. This gets updated when the object is created and when the evaluator triggers.
  39. /// </summary>
  40. private DateTime m_lasttime;
  41. /// <summary>
  42. /// The default time threshold for triggering in seconds. Zero means it won't trigger at all.
  43. /// </summary>
  44. const int DEFAULT_INTERVAL = 0;
  45. /// <summary>
  46. /// Create a new evaluator using the <see cref="DEFAULT_INTERVAL"/> time threshold in seconds.
  47. /// </summary>
  48. /// <remarks>
  49. /// <para>
  50. /// Create a new evaluator using the <see cref="DEFAULT_INTERVAL"/> time threshold in seconds.
  51. /// </para>
  52. /// <para>
  53. /// This evaluator will trigger if the specified time period
  54. /// <see cref="Interval"/> has passed since last check.
  55. /// </para>
  56. /// </remarks>
  57. public TimeEvaluator()
  58. : this(DEFAULT_INTERVAL)
  59. {
  60. }
  61. /// <summary>
  62. /// Create a new evaluator using the specified time threshold in seconds.
  63. /// </summary>
  64. /// <param name="interval">
  65. /// The time threshold in seconds to trigger after.
  66. /// Zero means it won't trigger at all.
  67. /// </param>
  68. /// <remarks>
  69. /// <para>
  70. /// Create a new evaluator using the specified time threshold in seconds.
  71. /// </para>
  72. /// <para>
  73. /// This evaluator will trigger if the specified time period
  74. /// <see cref="Interval"/> has passed since last check.
  75. /// </para>
  76. /// </remarks>
  77. public TimeEvaluator(int interval)
  78. {
  79. m_interval = interval;
  80. m_lasttime = DateTime.Now;
  81. }
  82. /// <summary>
  83. /// The time threshold in seconds to trigger after
  84. /// </summary>
  85. /// <value>
  86. /// The time threshold in seconds to trigger after.
  87. /// Zero means it won't trigger at all.
  88. /// </value>
  89. /// <remarks>
  90. /// <para>
  91. /// This evaluator will trigger if the specified time period
  92. /// <see cref="Interval"/> has passed since last check.
  93. /// </para>
  94. /// </remarks>
  95. public int Interval
  96. {
  97. get { return m_interval; }
  98. set { m_interval = value; }
  99. }
  100. /// <summary>
  101. /// Is this <paramref name="loggingEvent"/> the triggering event?
  102. /// </summary>
  103. /// <param name="loggingEvent">The event to check</param>
  104. /// <returns>This method returns <c>true</c>, if the specified time period
  105. /// <see cref="Interval"/> has passed since last check..
  106. /// Otherwise it returns <c>false</c></returns>
  107. /// <remarks>
  108. /// <para>
  109. /// This evaluator will trigger if the specified time period
  110. /// <see cref="Interval"/> has passed since last check.
  111. /// </para>
  112. /// </remarks>
  113. public bool IsTriggeringEvent(LoggingEvent loggingEvent)
  114. {
  115. if (loggingEvent == null)
  116. {
  117. throw new ArgumentNullException("loggingEvent");
  118. }
  119. // disable the evaluator if threshold is zero
  120. if (m_interval == 0) return false;
  121. lock (this) // avoid triggering multiple times
  122. {
  123. TimeSpan passed = DateTime.Now.Subtract(m_lasttime);
  124. if (passed.TotalSeconds > m_interval)
  125. {
  126. m_lasttime = DateTime.Now;
  127. return true;
  128. }
  129. else
  130. {
  131. return false;
  132. }
  133. }
  134. }
  135. }
  136. }