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