/// <summary> /// Repeated LogEvent detected. Checks if it should activate filter-action /// </summary> FilterResult RefreshFilterInfo(LogEventInfo logEvent, FilterInfo filterInfo) { if (filterInfo.HasExpired(logEvent.TimeStamp, TimeoutSeconds) || logEvent.Level.Ordinal > filterInfo.LogLevel.Ordinal) { int filterCount = filterInfo.FilterCount; if (filterCount > 0 && filterInfo.IsObsolete(logEvent.TimeStamp, TimeoutSeconds)) { filterCount = 0; } filterInfo.Refresh(logEvent.Level, logEvent.TimeStamp, 0); if (filterCount > 0) { if (!string.IsNullOrEmpty(FilterCountPropertyName)) { object otherFilterCount; if (!logEvent.Properties.TryGetValue(FilterCountPropertyName, out otherFilterCount)) { logEvent.Properties[FilterCountPropertyName] = filterCount; } else if (otherFilterCount is int) { filterCount = Math.Max((int)otherFilterCount, filterCount); logEvent.Properties[FilterCountPropertyName] = filterCount; } } if (!string.IsNullOrEmpty(FilterCountMessageAppendFormat)) { if (logEvent.Message != null) { logEvent.Message += string.Format(FilterCountMessageAppendFormat, filterCount.ToString(System.Globalization.CultureInfo.InvariantCulture)); } } } return(FilterResult.Neutral); } else { filterInfo.Refresh(logEvent.Level, logEvent.TimeStamp, filterInfo.FilterCount + 1); return(Action); } }
/// <summary> /// Checks whether log event should be logged or not. In case the LogEvent has just been repeated. /// </summary> /// <param name="logEvent">Log event.</param> /// <returns> /// <see cref="FilterResult.Ignore"/> - if the log event should be ignored<br/> /// <see cref="FilterResult.Neutral"/> - if the filter doesn't want to decide<br/> /// <see cref="FilterResult.Log"/> - if the log event should be logged<br/> /// .</returns> protected override FilterResult Check(LogEventInfo logEvent) { lock (_repeatFilter) { using (var targetBuilder = OptimizeBufferReuse ? ReusableLayoutBuilder.Allocate() : ReusableLayoutBuilder.None) { if (OptimizeBufferReuse && targetBuilder.Result.Capacity != OptimizeBufferDefaultLength) { // StringBuilder.Equals only works when StringBuilder.Capacity is the same if (OptimizeBufferDefaultLength < MaxInitialRenderBufferLength) { OptimizeBufferDefaultLength = MaxLength; while (OptimizeBufferDefaultLength < targetBuilder.Result.Capacity && OptimizeBufferDefaultLength < MaxInitialRenderBufferLength) { OptimizeBufferDefaultLength *= 2; } } targetBuilder.Result.Capacity = OptimizeBufferDefaultLength; } FilterInfoKey filterInfoKey = RenderFilterInfoKey(logEvent, OptimizeBufferReuse ? targetBuilder.Result : null); FilterInfo filterInfo = null; if (_repeatFilter.TryGetValue(filterInfoKey, out filterInfo)) { return(RefreshFilterInfo(logEvent, filterInfo)); } filterInfo = CreateFilterInfo(logEvent); if (OptimizeBufferReuse && filterInfo.StringBuffer != null) { filterInfo.StringBuffer.ClearBuilder(); int length = Math.Min(targetBuilder.Result.Length, MaxLength); for (int i = 0; i < length; ++i) { filterInfo.StringBuffer.Append(targetBuilder.Result[i]); } } filterInfo.Refresh(logEvent.Level, logEvent.TimeStamp, 0); _repeatFilter.Add(new FilterInfoKey(filterInfo.StringBuffer, filterInfoKey.StringValue, filterInfoKey.StringHashCode), filterInfo); } } return(FilterResult.Neutral); }