Пример #1
0
        /// <summary>
        ///     Writes the specified log event to the wrapped target, retrying and pausing in case of an error.
        /// </summary>
        /// <param name="logEvent">The log event.</param>
        protected override void Write(AsyncLogEventInfo logEvent)
        {
            AsyncContinuation continuation = null;
            var counter = 0;

            continuation = ex =>
            {
                if (ex == null)
                {
                    logEvent.Continuation(null);
                    return;
                }

                var retryNumber = Interlocked.Increment(ref counter);
                InternalLogger.Warn("Error while writing to '{0}': {1}. Try {2}/{3}", WrappedTarget, ex, retryNumber, RetryCount);

                // exceeded retry count
                if (retryNumber >= RetryCount)
                {
                    InternalLogger.Warn("Too many retries. Aborting.");
                    logEvent.Continuation(ex);
                    return;
                }

                // sleep and try again
                Thread.Sleep(RetryDelayMilliseconds);
                WrappedTarget.WriteAsyncLogEvent(logEvent.LogEvent.WithContinuation(continuation));
            };

            WrappedTarget.WriteAsyncLogEvent(logEvent.LogEvent.WithContinuation(continuation));
        }
Пример #2
0
 /// <summary>
 /// Changes the security context, forwards the call to the <see cref="WrapperTargetBase.WrappedTarget"/>.Write()
 /// and switches the context back to original.
 /// </summary>
 /// <param name="logEvent">The log event.</param>
 protected override void Write(AsyncLogEventInfo logEvent)
 {
     using (DoImpersonate())
     {
         WrappedTarget.WriteAsyncLogEvent(logEvent);
     }
 }
Пример #3
0
 /// <summary>
 /// Forwards the call to the <see cref="WrapperTargetBase.WrappedTarget"/>.Write()
 /// and calls <see cref="Target.Flush(AsyncContinuation)"/> on it if LogEvent satisfies
 /// the flush condition or condition is null.
 /// </summary>
 /// <param name="logEvent">Logging event to be written out.</param>
 protected override void Write(AsyncLogEventInfo logEvent)
 {
     if (Condition == null || Condition.Evaluate(logEvent.LogEvent).Equals(true))
     {
         if (AsyncFlush)
         {
             AsyncContinuation currentContinuation = logEvent.Continuation;
             AsyncContinuation wrappedContinuation = (ex) =>
             {
                 if (ex == null)
                 {
                     WrappedTarget.Flush((e) => { });
                 }
                 _pendingManualFlushList.CompleteOperation(ex);
                 currentContinuation(ex);
             };
             _pendingManualFlushList.BeginOperation();
             WrappedTarget.WriteAsyncLogEvent(logEvent.LogEvent.WithContinuation(wrappedContinuation));
         }
         else
         {
             WrappedTarget.WriteAsyncLogEvent(logEvent);
             FlushAsync((e) => { });
         }
     }
     else
     {
         WrappedTarget.WriteAsyncLogEvent(logEvent);
     }
 }
        /// <summary>
        /// Writes the provided <see cref="AsyncLogEventInfo"/> to the underlying target.
        /// </summary>
        /// <param name="logEvent">The log event to write.</param>
        protected override void Write(AsyncLogEventInfo logEvent)
        {
            // check if current event is different from previous
            if (IsDifferentFromPrevious(logEvent.LogEvent))
            {
                // current event is different - we need to write the count of same as previous messages
                if (_previousLogEvent != null && _previousCount > 0)
                {
                    var e = LogEventInfo.Create(_previousLogEvent.Level,
                                                _previousLogEvent.LoggerName,
                                                CultureInfo.CurrentCulture,
                                                "{0} more of previous message.",
                                                new object[] { _previousCount });

                    WrappedTarget.WriteAsyncLogEvent(new AsyncLogEventInfo(e, ex => { }));
                }

                // reset counters
                _previousLogEvent = logEvent.LogEvent;
                _previousCount    = 0;

                // write current event
                WrappedTarget.WriteAsyncLogEvent(logEvent);
            }
            else
            {
                // if current event is same as previous - simply increase the count and don't write it to the wrapped target
                _previousCount++;
            }
        }
 /// <summary>
 /// Changes the security context, forwards the call to the <see cref="WrapperTargetBase.WrappedTarget"/>.Write()
 /// and switches the context back to original.
 /// </summary>
 /// <param name="logEvent">The log event.</param>
 protected override void Write(AsyncLogEventInfo logEvent)
 {
     if (_writeLogEvent == null)
     {
         _writeLogEvent = (l) => WrappedTarget.WriteAsyncLogEvent(l);
     }
     RunImpersonated(_newIdentity, _writeLogEvent, logEvent);
 }
Пример #6
0
        protected override void FlushAsync(AsyncContinuation asyncContinuation)
        {
            foreach (var log in logs)
            {
                WrappedTarget.WriteAsyncLogEvent(log);
            }

            logs.Clear();
            base.FlushAsync(asyncContinuation);
        }
Пример #7
0
        /// <summary>
        /// Checks the condition against the passed log event.
        /// If the condition is met, the log event is forwarded to
        /// the wrapped target.
        /// </summary>
        /// <param name="logEvent">Log event.</param>
        protected override void Write(AsyncLogEventInfo logEvent)
        {
            object v = Condition.Evaluate(logEvent.LogEvent);

            if (boxedBooleanTrue.Equals(v))
            {
                WrappedTarget.WriteAsyncLogEvent(logEvent);
            }
            else
            {
                logEvent.Continuation(null);
            }
        }
Пример #8
0
        /// <summary>
        /// Adds the specified log event to the buffer.
        /// </summary>
        /// <param name="logEvent">The log event.</param>
        protected override void Write(AsyncLogEventInfo logEvent)
        {
            LogEventInfoBuffer buffer = GetRequestBuffer();

            if (buffer != null)
            {
                WrappedTarget.PrecalculateVolatileLayouts(logEvent.LogEvent);

                buffer.Append(logEvent);
                InternalLogger.Trace("Appending log event {0} to ASP.NET request buffer.", logEvent.LogEvent.SequenceID);
            }
            else
            {
                InternalLogger.Trace("ASP.NET request buffer does not exist. Passing to wrapped target.");
                WrappedTarget.WriteAsyncLogEvent(logEvent);
            }
        }
Пример #9
0
 protected override void Write(AsyncLogEventInfo logEvent)
 {
     if ((DateTime.Now - _lastLogEvent).TotalMinutes >= MinLogInterval)
     {
         _lastLogEvent = DateTime.Now;
         WrappedTarget.WriteAsyncLogEvent(logEvent);
     }
     else
     {
         logEvent.Continuation(null);
         // EXPERIMENTAL: check if the level of the log event is higher than "WARN" (3) and
         // log a debug message about keeping back the email message:
         if (logEvent.LogEvent.Level.Ordinal > 3)
         {
             Log.Debug("RateLimitWrapper: not sending email, frequency too high!");
         }
     }
 }
Пример #10
0
        /// <summary>
        /// Writes the specified log event to the wrapped target, retrying and pausing in case of an error.
        /// </summary>
        /// <param name="logEvent">The log event.</param>
        protected override void Write(AsyncLogEventInfo logEvent)
        {
            AsyncContinuation continuation = null;
            int counter = 0;

            continuation = ex =>
            {
                if (ex == null)
                {
                    logEvent.Continuation(null);
                    return;
                }

                int retryNumber = Interlocked.Increment(ref counter);
                InternalLogger.Warn("Error while writing to '{0}': {1}. Try {2}/{3}", WrappedTarget, ex, retryNumber, RetryCount);

                // exceeded retry count
                if (retryNumber >= RetryCount)
                {
                    InternalLogger.Warn("Too many retries. Aborting.");
                    logEvent.Continuation(ex);
                    return;
                }

                // sleep and try again (Check every 100 ms if target have been closed)
                for (int i = 0; i < RetryDelayMilliseconds;)
                {
                    int retryDelay = Math.Min(100, RetryDelayMilliseconds - i);
                    Thread.Sleep(retryDelay);
                    i += retryDelay;
                    if (!IsInitialized)
                    {
                        InternalLogger.Warn("Target closed. Aborting.");
                        logEvent.Continuation(ex);
                        return;
                    }
                }

                WrappedTarget.WriteAsyncLogEvent(logEvent.LogEvent.WithContinuation(continuation));
            };

            WrappedTarget.WriteAsyncLogEvent(logEvent.LogEvent.WithContinuation(continuation));
        }
Пример #11
0
        /// <summary>
        /// Writes log event to the wrapped target if the current <see cref="MessagesWrittenCount"/> is lower than <see cref="MessageLimit"/>.
        /// If the <see cref="MessageLimit"/> is already reached, no log event will be written to the wrapped target.
        /// <see cref="MessagesWrittenCount"/> resets when the current <see cref="Interval"/> is expired.
        /// </summary>
        /// <param name="logEvent">Log event to be written out.</param>
        protected override void Write(AsyncLogEventInfo logEvent)
        {
            if (IsIntervalExpired())
            {
                ResetInterval();
                InternalLogger.Debug("LimitingWrapper(Name={0}): New interval of '{1}' started.", Name, Interval);
            }

            if (MessagesWrittenCount < MessageLimit)
            {
                WrappedTarget.WriteAsyncLogEvent(logEvent);
                MessagesWrittenCount++;
            }
            else
            {
                logEvent.Continuation(null);
                InternalLogger.Trace("LimitingWrapper(Name={0}): Discarded event, because MessageLimit of '{1}' was reached.", Name, MessageLimit);
            }
        }
Пример #12
0
 /// <summary>
 /// Forwards the log message to the <see cref="WrapperTargetBase.WrappedTarget"/> by calling the <see cref="Target.Write(LogEventInfo)"/> method <see cref="RepeatCount"/> times.
 /// </summary>
 /// <param name="logEvent">The log event.</param>
 protected override void Write(AsyncLogEventInfo logEvent)
 {
     AsyncHelpers.Repeat(RepeatCount, logEvent.Continuation, cont => WrappedTarget.WriteAsyncLogEvent(logEvent.LogEvent.WithContinuation(cont)));
 }
Пример #13
0
 // <inheritdoc />
 protected override void Write(AsyncLogEventInfo logEvent)
 {
     WrappedTarget.WriteAsyncLogEvent(logEvent);
 }
Пример #14
0
 /// <summary>
 ///     Forwards the call to the <see cref="WrapperTargetBase.WrappedTarget" />.Write()
 ///     and calls <see cref="Target.Flush(AsyncContinuation)" /> on it.
 /// </summary>
 /// <param name="logEvent">Logging event to be written out.</param>
 protected override void Write(AsyncLogEventInfo logEvent)
 {
     WrappedTarget.WriteAsyncLogEvent(logEvent.LogEvent.WithContinuation(AsyncHelpers.PrecededBy(logEvent.Continuation, WrappedTarget.Flush)));
 }
 protected override void Write(AsyncLogEventInfo logEvent)
 {
     Thread.Sleep(TimeSpan.FromSeconds(1));
     WrappedTarget.WriteAsyncLogEvent(logEvent);
 }