Example #1
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="logEvents">Log events.</param>
 protected internal override void Write(LogEventInfo[] logEvents)
 {
     using (DoImpersonate())
     {
         WrappedTarget.Write(logEvents);
     }
 }
Example #2
0
 /// <summary>
 /// Flush any pending log messages (in case of asynchronous targets).
 /// </summary>
 /// <param name="asyncContinuation">The asynchronous continuation.</param>
 protected override void FlushAsync(AsyncContinuation asyncContinuation)
 {
     using (DoImpersonate())
     {
         WrappedTarget.Flush(asyncContinuation);
     }
 }
Example #3
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="logEvents">Log events.</param>
 protected override void Write(IList <AsyncLogEventInfo> logEvents)
 {
     using (DoImpersonate())
     {
         WrappedTarget.WriteAsyncLogEvents(logEvents);
     }
 }
Example #4
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);
     }
 }
 /// <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>
 public override void Write(LogEventInfo logEvent)
 {
     for (int i = 0; i < RepeatCount; ++i)
     {
         WrappedTarget.Write(logEvent);
     }
 }
Example #6
0
        private void LazyWriterTimerCallback(object state)
        {
            lock (_inLazyWriterMonitor)
            {
                try
                {
                    do
                    {
                        // Console.WriteLine("q: {0}", RequestQueue.RequestCount);
                        List <LogEventInfo> pendingRequests = RequestQueue.DequeueBatch(BatchSize);

                        try
                        {
                            if (pendingRequests.Count == 0)
                            {
                                break;
                            }

                            LogEventInfo[] events = pendingRequests.ToArray();
                            WrappedTarget.Write(events);
                        }
                        finally
                        {
                            RequestQueue.BatchProcessed(pendingRequests);
                        }
                    } while (_flushAll);
                }
                catch (Exception ex)
                {
                    InternalLogger.Error("Error in lazy writer timer procedure: {0}", ex);
                }
            }
        }
Example #7
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>
 public override void Write(LogEventInfo logEvent)
 {
     for (int i = 0; i < RetryCount; ++i)
     {
         try
         {
             if (i > 0)
             {
                 InternalLogger.Warn("Retry #{0}", i);
             }
             WrappedTarget.Write(logEvent);
             // success, return
             return;
         }
         catch (Exception ex)
         {
             InternalLogger.Warn("Error while writing to '{0}': {1}", WrappedTarget, ex);
             if (i == RetryCount - 1)
             {
                 throw ex;
             }
             System.Threading.Thread.Sleep(RetryDelayMilliseconds);
         }
     }
 }
Example #8
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>
        ///     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));
        }
 /// <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="logEvents">Log events.</param>
 public override void Write(LogEventInfo[] logEvents)
 {
     using (DoImpersonate())
     {
         WrappedTarget.Write(logEvents);
     }
 }
Example #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 internal override void Write(LogEventInfo logEvent)
 {
     for (int i = 0; i < RepeatCount; ++i)
     {
         WrappedTarget.Write(logEvent);
     }
 }
 /// <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="logEvents">Log events.</param>
 protected override void Write(IList <AsyncLogEventInfo> logEvents)
 {
     if (_writeLogEvents == null)
     {
         _writeLogEvents = (l) => WrappedTarget.WriteAsyncLogEvents(l);
     }
     RunImpersonated(_newIdentity, _writeLogEvents, logEvents);
 }
 /// <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);
 }
Example #15
0
        protected override void Write(IList <AsyncLogEventInfo> logEvents)
        {
            var timer = new Stopwatch();

            timer.Start();
            WrappedTarget.WriteAsyncLogEvents(logEvents);
            timer.Stop();
            InternalLogger.Trace($"Logging to target '{WrappedTarget.Name}' {logEvents.Count} logs took {timer.ElapsedMilliseconds} milliseconds.");
        }
Example #16
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>
        public override void Write(LogEventInfo logEvent)
        {
            object v = _condition.Evaluate(logEvent);

            if (v != null && v is bool && (bool)v)
            {
                WrappedTarget.Write(logEvent);
            }
        }
        /// <summary>
        /// Adds the specified log event to the buffer.
        /// </summary>
        /// <param name="logEvent">The log event.</param>
        public override void Write(LogEventInfo logEvent)
        {
            WrappedTarget.PrecalculateVolatileLayouts(logEvent);
            LogEventInfoBuffer buffer = GetRequestBuffer();

            if (buffer != null)
            {
                buffer.Append(logEvent);
            }
        }
        protected override void FlushAsync(AsyncContinuation asyncContinuation)
        {
            foreach (var log in logs)
            {
                WrappedTarget.WriteAsyncLogEvent(log);
            }

            logs.Clear();
            base.FlushAsync(asyncContinuation);
        }
Example #19
0
 void FlushCallback(object state)
 {
     lock (this)
     {
         LogEventInfo[] events = _buffer.GetEventsAndClear();
         if (events.Length > 0)
         {
             WrappedTarget.Write(events);
         }
     }
 }
Example #20
0
        private void OnEndRequest(object sender, EventArgs args)
        {
            LogEventInfoBuffer buffer = GetRequestBuffer();

            if (buffer != null)
            {
                InternalLogger.Trace("Sending buffered events to wrapped target: {0}.", WrappedTarget);
                AsyncLogEventInfo[] events = buffer.GetEventsAndClear();
                WrappedTarget.WriteAsyncLogEvents(events);
            }
        }
Example #21
0
        /// <inheritdoc />
        /// <summary>
        /// The first record is to log immediately, then accumulate for a time and flush by timer. Equivalence is taken into account.
        /// </summary>
        protected override void Write(AsyncLogEventInfo e)
        {
            Tuple <int, StringBuilder, AsyncLogEventInfo> count;

#if USECONCURRENT
            count = _entriesCounts.AddOrUpdate(e,
                                               /*do not store first - it is logged out immediately*/
                                               new Tuple <int, StringBuilder, AsyncLogEventInfo>(0, NeedsStringBuilder(e.LogEvent)
                    ? new StringBuilder()
                    : null, default(AsyncLogEventInfo)),
                                               (k, v) =>
            {
                // but store all the others
                if (NeedsStringBuilder(e.LogEvent))
                {
                    v.Item2.Append(Escape(e.LogEvent.FormattedMessage));
                    v.Item2.Append(this.GroupByTemplateSeparator);
                }
                return(new Tuple <int, StringBuilder, AsyncLogEventInfo>(v.Item1 + 1, v.Item2,
                                                                         e /*in flush it will be the last*/));
            });
#else
            lock (_lockObject)
            {
                if (_entriesCounts.TryGetValue(e, out count))
                {
                    if (NeedsStringBuilder(e.LogEvent))
                    {
                        count.Item2.Append(Escape(e.LogEvent.FormattedMessage));
                        count.Item2.Append(this.GroupByTemplateSeparator);
                    }
                    count = new Tuple <int, StringBuilder, AsyncLogEventInfo>(count.Item1 + 1, count.Item2,
                                                                              e /*in flush it will be the last*/);
                }
                else
                {
                    count = new Tuple <int, StringBuilder, AsyncLogEventInfo>(0,
                                                                              NeedsStringBuilder(e.LogEvent)
                            ? new StringBuilder()
                            : null, default(AsyncLogEventInfo));
                }
                _entriesCounts[e] = count;
            }
#endif

            if (count.Item1 == 0)
            {
                e.LogEvent.Properties["IsFirst"] = "true";
                WrappedTarget.WriteAsyncLogEvents(e);
                TurnOnTimerIfOffline();
            }
        }
Example #22
0
        /// <summary>
        ///     Flushes pending events in the buffer (if any).
        /// </summary>
        /// <param name="asyncContinuation">The asynchronous continuation.</param>
        protected override void FlushAsync(AsyncContinuation asyncContinuation)
        {
            var events = buffer.GetEventsAndClear();

            if (events.Length == 0)
            {
                WrappedTarget.Flush(asyncContinuation);
            }
            else
            {
                WrappedTarget.WriteAsyncLogEvents(events, ex => WrappedTarget.Flush(asyncContinuation));
            }
        }
        private void OnEndRequest(object sender, EventArgs args)
        {
            LogEventInfoBuffer buffer = GetRequestBuffer();

            if (buffer != null)
            {
                LogEventInfo[] events = buffer.GetEventsAndClear();
                if (events.Length > 0)
                {
                    WrappedTarget.Write(events);
                }
            }
        }
Example #24
0
        /// <summary>
        /// Flushes pending events in the buffer (if any).
        /// </summary>
        public override void Flush(TimeSpan timeout)
        {
            base.Flush(timeout);

            lock (this)
            {
                LogEventInfo[] events = _buffer.GetEventsAndClear();
                if (events.Length > 0)
                {
                    WrappedTarget.Write(events);
                }
            }
        }
Example #25
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);
            }
        }
Example #26
0
        /// <summary>
        /// Forwards the array of log events to the <see cref="WrapperTargetBase.WrappedTarget"/> by calling the <see cref="Target.Write(LogEventInfo[])"/> method <see cref="RepeatCount"/> times.
        /// </summary>
        /// <param name="logEvents">The array of log events.</param>
        protected internal override void Write(LogEventInfo[] logEvents)
        {
            LogEventInfo[] newEvents = new LogEventInfo[logEvents.Length * RepeatCount];
            int            pos       = 0;

            for (int i = 0; i < logEvents.Length; ++i)
            {
                for (int j = 0; j < RepeatCount; ++j)
                {
                    newEvents[pos++] = logEvents[i];
                }
            }
            WrappedTarget.Write(newEvents);
        }
Example #27
0
        // <inheritdoc />
        protected override void Write(IList <AsyncLogEventInfo> logEvents)
        {
            if (_buildKeyStringDelegate == null)
            {
                _buildKeyStringDelegate = logEvent => RenderLogEvent(Key, logEvent.LogEvent);
            }

            var buckets = logEvents.BucketSort(_buildKeyStringDelegate);

            foreach (var bucket in buckets)
            {
                WrappedTarget.WriteAsyncLogEvents(bucket.Value);
            }
        }
Example #28
0
 private void FlushCallback(object state)
 {
     lock (SyncRoot)
     {
         if (IsInitialized)
         {
             var events = buffer.GetEventsAndClear();
             if (events.Length > 0)
             {
                 WrappedTarget.WriteAsyncLogEvents(events);
             }
         }
     }
 }
Example #29
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);
            }
        }
Example #30
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!");
         }
     }
 }