예제 #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
        public ForwardingLogWriter(ILogger logger, ILogEventFilter filter) : base(filter)
        {
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            _Logger = logger;
        }
예제 #3
0
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="filter">The filter to check before writing events. Only events that are passed by the filter are written.</param>
        /// <param name="logWriter">Another <seealso cref="ILogWriter"/> implementation that filtered events will be passed to.</param>
        public FilteringLogWriter(ILogEventFilter filter, ILogWriter logWriter)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }
            if (logWriter == null)
            {
                throw new ArgumentNullException(nameof(logWriter));
            }

            _Filter    = filter;
            _LogWriter = logWriter as IBatchLogWriter;
            if (_LogWriter == null)
            {
                _LogWriter = new BatchLogWriterAdapter(logWriter);
            }
        }
예제 #4
0
        // ILogWriter logWriter, IEnumerable<ILogEventContextProvider> contextProviders, ILogClock logClock
        public Logger(LogPolicy policy)
        {
            if (policy == null)
            {
                throw new ArgumentNullException(nameof(policy));
            }
            if (policy.LogWriter == null)
            {
                throw new ArgumentException("policy.LogWriter cannot be null", nameof(policy));
            }

            _EntryPool = new LogEventPool();
            _LogWriter = policy.LogWriter;
            _LogClock  = policy.Clock;
            _Filter    = policy.Filter;

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

            _IsEnabled = true;
        }
예제 #5
0
        public ListLogWriter(IList <LogEvent> events, int maximumCapacity, ILogEventFilter filter) : base(filter)
        {
            if (events == null)
            {
                throw new ArgumentNullException(nameof(events));
            }
            if (maximumCapacity <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maximumCapacity), "maximumCapacity must be greater than zero.");
            }

            _MaximumCapacity = maximumCapacity;
            _Events          = events ?? new List <LogEvent>(maximumCapacity);
            _Pool            = new Pool <LogEvent>(new PoolPolicy <LogEvent>()
            {
                Factory = (pool) => new LogEvent(),
                InitializationPolicy = PooledItemInitialization.Return,
                MaximumPoolSize      = maximumCapacity,
                ReinitializeObject   = (entry) =>
                {
                    entry.Clear();
                }
            });
        }
 internal static LoggerConfiguration With(LoggerFilterConfiguration loggerFilterConfiguration,
                                          ILogEventFilter filter)
 {
     return(loggerFilterConfiguration.With(filter));
 }
예제 #7
0
 public ThreadPrincipalLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
 /// <summary>
 /// Full constructor.
 /// </summary>
 /// <param name="filter">A <see cref="ILogEventFilter"/> instance used to determine if the property should be added or not.</param>
 public OSVersionDescriptionLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
 /// <summary>
 /// Full constructor.
 /// </summary>
 /// <param name="filter">A <see cref="ILogEventFilter"/> instance used to determine if the property should be added or not.</param>
 public AppDomainIdLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
예제 #10
0
        public WindowsEventLogWriter(string eventLogName, string eventSourceName, string eventLogMachineName, bool createEventLog, System.Diagnostics.OverflowAction overflowAction, int retentionDays, ILogEventFormatter eventFormatter, ILogEventFilter filter) : base(filter)
        {
            eventLogName = eventLogName ?? "Application";
            if (String.IsNullOrWhiteSpace(eventLogName))
            {
                throw new ArgumentException(nameof(eventLogName) + " cannot be empty or whitespace.", nameof(eventLogName));
            }
            if (eventSourceName == null)
            {
                throw new ArgumentNullException(nameof(eventSourceName));
            }
            if (String.IsNullOrWhiteSpace(eventSourceName))
            {
                throw new ArgumentException(nameof(eventSourceName) + " cannot be empty or whitespace.", nameof(eventSourceName));
            }

            _EventFormatter  = eventFormatter;
            _EventLogName    = eventLogName;
            _EventSourceName = eventSourceName;

            if (createEventLog)
            {
                if (String.IsNullOrWhiteSpace(eventLogMachineName))
                {
                    eventLogMachineName = ".";
                }
                if (!System.Diagnostics.EventLog.SourceExists(eventSourceName, eventLogMachineName))
                {
                    var creationData = new System.Diagnostics.EventSourceCreationData(eventSourceName, eventLogName);
                    creationData.MachineName = eventLogMachineName;
                    System.Diagnostics.EventLog.CreateEventSource(creationData);

                    var log = new System.Diagnostics.EventLog(eventLogName, eventLogMachineName);
                    log.ModifyOverflowPolicy(overflowAction, retentionDays);
                }
            }

            _Log = new System.Diagnostics.EventLog(eventLogName, eventLogMachineName, eventSourceName);
        }
예제 #11
0
 public EntryAssemblyLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
예제 #12
0
 public DebugLogWriter(ILogEventFormatter eventFormatter, ILogEventFilter filter) : base(filter)
 {
     _LogEventFormatter = eventFormatter ?? SimpleLogEventFormatter.DefaultInstance;
 }
예제 #13
0
 /// <summary>
 /// Full constructor.
 /// </summary>
 /// <param name="filter">A <see cref="ILogEventFilter"/> instance used to determine if the property should be added or not.</param>
 public StackTraceLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
 /// <summary>
 /// Full constructor.
 /// </summary>
 /// <param name="filter">A <see cref="ILogEventFilter"/> instance used to determine if the property should be added or not.</param>
 public GuidEventInstanceIdLogEntryContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
예제 #15
0
 /// <summary>
 /// Filter out log events from the stream based on the provided filter.
 /// </summary>
 /// <param name="filter">The filter to apply.</param>
 /// <returns>Configuration object allowing method chaining.</returns>
 public LoggerConfiguration FilteredBy(ILogEventFilter filter)
 {
     if (filter == null) throw new ArgumentNullException("filter");
     _filters.Add(filter);
     return this;
 }
 /// <summary>
 /// Full constructor.
 /// </summary>
 /// <param name="filter">A <see cref="ILogEventFilter"/> instance used to determine if the property should be added or not.</param>
 public ApplicationNameLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
예제 #17
0
 /// <summary>
 /// Full constructor.
 /// </summary>
 /// <param name="filter">A <see cref="ILogEventFilter"/> instance used to determine if the property should be added or not.</param>
 public TerminalServerSessionIdLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
예제 #18
0
 public ProcessNameLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
예제 #19
0
 /// <summary>
 /// Full constructor.
 /// </summary>
 /// <param name="filter">A <see cref="ILogEventFilter"/> instance used to determine if the property should be added or not.</param>
 public MachineNameLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
        /// <summary>
        /// Applies a property with the specified name and string value.
        /// </summary>
        /// <param name="propertyName">The name of the property to apply.</param>
        /// <param name="propertyValue">The value of the property to apply.</param>
        /// <param name="filter">An optional <see cref="ILogEventFilter"/> used to decide if this property should be applied or not.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="propertyName"/> is null.</exception>
        /// <exception cref="System.ArgumentException">Thrown if <paramref name="propertyName"/> is empty or only whitespace.</exception>
        public FixedValueLogEventContextProvider(string propertyName, object propertyValue, ILogEventFilter filter) : base(filter)
        {
            if (propertyName == null)
            {
                throw new ArgumentNullException(nameof(propertyName));
            }
            if (String.IsNullOrWhiteSpace(propertyName))
            {
                throw new ArgumentException(String.Format(System.Globalization.CultureInfo.CurrentCulture, Properties.Resources.PropertyCannotBeEmptyOrWhitespace, nameof(propertyName)));
            }

            _PropertyName  = propertyName;
            _PropertyValue = propertyValue;
        }
 /// <summary>
 /// Full constructor.
 /// </summary>
 /// <param name="filter">A <see cref="ILogEventFilter"/> instance used to determine if the property should be added or not.</param>
 public LogCallContextLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
예제 #22
0
        public WindowsEventLogWriter(System.Diagnostics.EventLog eventLog, ILogEventFormatter eventFormatter, ILogEventFilter filter) : base(filter)
        {
            if (eventLog == null)
            {
                throw new ArgumentNullException(nameof(eventLog));
            }

            _EventFormatter = eventFormatter;
            _Log            = eventLog;
        }
예제 #23
0
        public StreamLogWriter(System.IO.Stream stream, System.Text.Encoding encoding, ILogEventFormatter eventFormatter, ILogEventFilter filter) : base(filter)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            _Writer            = new System.IO.StreamWriter(stream, encoding ?? System.Text.UTF8Encoding.UTF8);
            _LogEventFormatter = eventFormatter ?? SimpleLogEventFormatter.DefaultInstance;
        }
예제 #24
0
        /// <summary>
        /// Partial constructor.
        /// </summary>
        /// <param name="propertyName">The name of the property to add.</param>
        /// <param name="requestContextValueCallback">A <see cref="Func{TResult}"/> to call to obtain the property value.</param>
        /// <param name="filter">A <see cref="ILogEventFilter"/> instance used to determine if the property should be added or not.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="propertyName"/> or <paramref name="requestContextValueCallback"/> is null.</exception>
        /// <exception cref="System.ArgumentException">Thrown if <paramref name="propertyName"/> is empty or only whitespace.</exception>
        public DelegateLogEventContextProvider(string propertyName, Func <object> requestContextValueCallback, ILogEventFilter filter) : base(filter)
        {
            if (propertyName == null)
            {
                throw new ArgumentNullException(nameof(propertyName));
            }
            if (String.IsNullOrWhiteSpace(propertyName))
            {
                throw new ArgumentException("propertyName cannot be null, empty or whitespace.", nameof(propertyName));
            }
            if (requestContextValueCallback == null)
            {
                throw new ArgumentNullException(nameof(requestContextValueCallback));
            }

            _PropertyName = propertyName;
            _RequestContextValueCallback = requestContextValueCallback;
        }
 public ThreadNameLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
예제 #26
0
        public AggregateLogWriter(IEnumerable <ILogWriter> writers, AggregateLoggerWriteOptions writeOptions, ILogEventFilter filter) : base(filter)
        {
            if (writers == null)
            {
                throw new ArgumentNullException(nameof(writers));
            }

            _WriteOptions            = writeOptions;
            _Writers                 = new List <ILogWriter>(writers);
            _RequiresSynchronisation = _Writers.Any((w) => w.RequiresSynchronisation);
        }
예제 #27
0
        public AsyncQueueLogWriter(ILogWriter logWriter, int batchSize, TimeSpan writeTimeout, ILogEventFilter filter) : base(filter)
        {
            if (logWriter == null)
            {
                throw new ArgumentNullException(nameof(logWriter));
            }

            _BatchSize    = batchSize;
            _WriteTimeout = writeTimeout;

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

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

            //TODO: Start thread that does write here.
            //TODO: Implement IDisposable.
        }
 public OsVersionLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
 /// <summary>
 /// Full constructor.
 /// </summary>
 /// <param name="filter">A <see cref="ILogEventFilter"/> instance used to determine if the property should be added or not.</param>
 public CallingAssemblyLogEventContextProvider(ILogEventFilter filter) : base(filter)
 {
 }
예제 #30
0
 // don't support (yet?) arrays in the parameter list (ILogEventEnricher[])
 internal static LoggerConfiguration With(LoggerFilterConfiguration loggerFilterConfiguration, ILogEventFilter filter)
 => loggerFilterConfiguration.With(filter);
예제 #31
0
        public TextLogWriter(System.IO.TextWriter writer, ILogEventFormatter eventFormatter, ILogEventFilter filter) : base(filter)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            _Writer            = writer;
            _LogEventFormatter = eventFormatter ?? SimpleLogEventFormatter.DefaultInstance;
        }