Ejemplo n.º 1
0
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="primaryWriter">The <see cref="ILogWriter"/> instance to first attempt to write to.</param>
        /// <param name="secondaryWriter">The <see cref="ILogWriter"/> instance to attempt to write to if the first write attempt fails.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="primaryWriter"/> or <paramref name="secondaryWriter"/> is null.</exception>
        public FallbackWriter(ILogWriter primaryWriter, ILogWriter secondaryWriter)
        {
            if (primaryWriter == null)
            {
                throw new ArgumentNullException(nameof(primaryWriter));
            }
            if (secondaryWriter == null)
            {
                throw new ArgumentNullException(nameof(secondaryWriter));
            }

            var batchWriter = primaryWriter as IBatchLogWriter;

            if (batchWriter == null)
            {
                batchWriter = new BatchLogWriterAdapter(primaryWriter);
            }

            _PrimaryWriter = batchWriter;

            batchWriter = primaryWriter as IBatchLogWriter;
            if (batchWriter == null)
            {
                batchWriter = new BatchLogWriterAdapter(secondaryWriter);
            }

            _SecondaryWriter = batchWriter;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="logWriter">The log writer to forward events to from the background thread. If multiple output locations are required, usea  <see cref="AggregateLogWriter"/> instance.</param>
        /// <param name="batchSize">The number of items in the queue before they are actually passed to <paramref name="logWriter"/>.</param>
        /// <param name="writeTimeout">The time after the last event was logged to wait before writing items in the queue, even if the queue size is still less than <paramref name="batchSize"/>.</param>
        /// <param name="errorHandler">An <see cref="ILoggingErrorHandler"/> implementation used to handle errors that occur.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="logWriter"/> is null.</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown if <paramref name="batchSize"/> is less than or equal to zero.</exception>
        /// <remarks>
        /// <para>If the <paramref name="errorHandler"/> is null then <see cref="SuppressingErrorHandler.DefaultInstance"/> will be used. Ideally the same error handler instance as provided to the parent logger should be used.</para>
        /// </remarks>
        public AsyncQueueLogWriter(ILogWriter logWriter, int batchSize, TimeSpan writeTimeout, ILoggingErrorHandler errorHandler)
        {
#if !CONTRACTS_ONLY
            if (logWriter == null)
            {
                throw new ArgumentNullException(nameof(logWriter));
            }
            if (batchSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(batchSize));
            }

            _ErrorHandler = errorHandler ?? SuppressingErrorHandler.DefaultInstance;
            _BatchSize    = batchSize;
            _WriteTimeout = writeTimeout;
            _LogEventPool = new LogEventPool(batchSize * 2);

            if (writeTimeout.TotalMilliseconds > 0)
            {
                _BufferTimeoutTimer = new System.Threading.Timer((reserved) => SetWriteEventsSignal(), null, InfiniteTimespan, InfiniteTimespan);
            }

            _WriteBufferedEventsSignal = new System.Threading.ManualResetEvent(false);
            _BufferedLogEvents         = new System.Collections.Concurrent.ConcurrentQueue <PooledObject <LogEvent> >();

            var batchedWriter = logWriter as IBatchLogWriter;
            if (batchedWriter == null)
            {
                batchedWriter = new BatchLogWriterAdapter(logWriter);
            }

            _LogWriter = batchedWriter;

            StartBackgroundWriterThread();

#if SUPPORTS_APPDOMAINS
            AppDomain.CurrentDomain.DomainUnload       += CurrentDomain_DomainUnload;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            AppDomain.CurrentDomain.ProcessExit        += CurrentDomain_ProcessExit;
#endif
#endif
        }