예제 #1
0
파일: Logger.cs 프로젝트: Yortw/ScribeSharp
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="policy">A <see cref="LogPolicy"/> instance used to configure this logger.</param>
        /// <remarks>
        /// <para>The <paramref name="policy"/> should not be changed after being provided to the logger. Values from the policy are cached or copied before use and will not change even if the policy is updated after the logger is constructed.</para>
        /// </remarks>
        public Logger(LogPolicy policy)
        {
            if (policy == null)
            {
                throw new ArgumentNullException(nameof(policy));
            }
            if (policy.LogWriter == null)
            {
                throw new ArgumentException(String.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.PropertyCannotBeNull, "policy.LogWriter"), nameof(policy));
            }

            _ErrorHandler = policy.ErrorHandler ?? SuppressingErrorHandler.DefaultInstance;
            _EntryPool    = new LogEventPool(policy.LogEventPoolCapacity);
            _JobPool      = new LoggedJobPool(policy.JobPoolCapacity);

            _LogWriter                = policy.LogWriter;
            _LogClock                 = policy.Clock ?? new CachingClock(new LocalSystemClock(), TimeSpan.FromTicks(16));
            _Filter                   = policy.Filter;
            _FirstChanceFilter        = policy.FirstChanceFilter;
            _RendererMap              = policy.TypeRendererMap;
            _Source                   = policy.Source;
            _DefaultExceptionRenderer = policy.DefaultExceptionRenderer;

            if (policy.ContextProviders != null)
            {
                _ContextProviders = (policy.ContextProviders as ILogEventContextProvider[]) ?? policy.ContextProviders.ToArray();
            }

            _IsEnabled = true;
        }
예제 #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
        }