TimeEvaluator.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #region Copyright & 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. namespace log4net.Core
  21. {
  22. /// <summary>
  23. /// An evaluator that triggers after specified number of seconds.
  24. /// </summary>
  25. /// <remarks>
  26. /// <para>
  27. /// This evaluator will trigger if the specified time period
  28. /// <see cref="Interval"/> has passed since last check.
  29. /// </para>
  30. /// </remarks>
  31. /// <author>Robert Sevcik</author>
  32. public class TimeEvaluator : ITriggeringEventEvaluator
  33. {
  34. /// <summary>
  35. /// The time threshold for triggering in seconds. Zero means it won't trigger at all.
  36. /// </summary>
  37. private int m_interval;
  38. /// <summary>
  39. /// The UTC time of last check. This gets updated when the object is created and when the evaluator triggers.
  40. /// </summary>
  41. private DateTime m_lastTimeUtc;
  42. /// <summary>
  43. /// The default time threshold for triggering in seconds. Zero means it won't trigger at all.
  44. /// </summary>
  45. const int DEFAULT_INTERVAL = 0;
  46. /// <summary>
  47. /// Create a new evaluator using the <see cref="DEFAULT_INTERVAL"/> time threshold in seconds.
  48. /// </summary>
  49. /// <remarks>
  50. /// <para>
  51. /// Create a new evaluator using the <see cref="DEFAULT_INTERVAL"/> time threshold in seconds.
  52. /// </para>
  53. /// <para>
  54. /// This evaluator will trigger if the specified time period
  55. /// <see cref="Interval"/> has passed since last check.
  56. /// </para>
  57. /// </remarks>
  58. public TimeEvaluator()
  59. : this(DEFAULT_INTERVAL)
  60. {
  61. }
  62. /// <summary>
  63. /// Create a new evaluator using the specified time threshold in seconds.
  64. /// </summary>
  65. /// <param name="interval">
  66. /// The time threshold in seconds to trigger after.
  67. /// Zero means it won't trigger at all.
  68. /// </param>
  69. /// <remarks>
  70. /// <para>
  71. /// Create a new evaluator using the specified time threshold in seconds.
  72. /// </para>
  73. /// <para>
  74. /// This evaluator will trigger if the specified time period
  75. /// <see cref="Interval"/> has passed since last check.
  76. /// </para>
  77. /// </remarks>
  78. public TimeEvaluator(int interval)
  79. {
  80. m_interval = interval;
  81. m_lastTimeUtc = DateTime.UtcNow;
  82. }
  83. /// <summary>
  84. /// The time threshold in seconds to trigger after
  85. /// </summary>
  86. /// <value>
  87. /// The time threshold in seconds to trigger after.
  88. /// Zero means it won't trigger at all.
  89. /// </value>
  90. /// <remarks>
  91. /// <para>
  92. /// This evaluator will trigger if the specified time period
  93. /// <see cref="Interval"/> has passed since last check.
  94. /// </para>
  95. /// </remarks>
  96. public int Interval
  97. {
  98. get { return m_interval; }
  99. set { m_interval = value; }
  100. }
  101. /// <summary>
  102. /// Is this <paramref name="loggingEvent"/> the triggering event?
  103. /// </summary>
  104. /// <param name="loggingEvent">The event to check</param>
  105. /// <returns>This method returns <c>true</c>, if the specified time period
  106. /// <see cref="Interval"/> has passed since last check..
  107. /// Otherwise it returns <c>false</c></returns>
  108. /// <remarks>
  109. /// <para>
  110. /// This evaluator will trigger if the specified time period
  111. /// <see cref="Interval"/> has passed since last check.
  112. /// </para>
  113. /// </remarks>
  114. public bool IsTriggeringEvent(LoggingEvent loggingEvent)
  115. {
  116. if (loggingEvent == null)
  117. {
  118. throw new ArgumentNullException("loggingEvent");
  119. }
  120. // disable the evaluator if threshold is zero
  121. if (m_interval == 0) return false;
  122. lock (this) // avoid triggering multiple times
  123. {
  124. TimeSpan passed = DateTime.UtcNow.Subtract(m_lastTimeUtc);
  125. if (passed.TotalSeconds > m_interval)
  126. {
  127. m_lastTimeUtc = DateTime.UtcNow;
  128. return true;
  129. }
  130. else
  131. {
  132. return false;
  133. }
  134. }
  135. }
  136. }
  137. }