/// <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; }
/// <summary> /// Creates a new <see cref="ILogger"/> implementation which wraps this one, but with an alternate source. /// </summary> /// <param name="source">The source to apply to all log event entries. If null, the inner source or system supplied source will be used.</param> /// <param name="contextProviders">A set of <see cref="ILogEventContextProvider"/> instances used to add additional information to log events before they are written to the <see cref="ILogWriter"/>.</param> /// <returns>An implementation of <see cref="ILogger"/> with the specified source applied.</returns> public ILogger CreateChildLogger(string source, IEnumerable <ILogEventContextProvider> contextProviders) { var policy = new LogPolicy() { ContextProviders = contextProviders, Source = source }; return(CreateChildLogger(policy)); }
/// <summary> /// Creates a new <see cref="ILogger"/> implementation which wraps this one, applying the supplied policy to log events before forwarding them to this logger for processing. /// </summary> /// <remarks> /// <para>The returned logger wraps this instance, so log entries are written to this logger but only if the filters on both loggers pass. Properties provided by context providers are applied by the wrapped instance first.</para> /// <para>If the <paramref name="policy"/> specifies a non-null log writer, then log events are written to both that writer and this logger.</para> /// </remarks> /// <param name="policy">The policy to apply to the wrapping logger.</param> /// <returns>An implementation of <see cref="ILogger"/> that wraps this one.</returns> /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="policy"/> is null.</exception> public ILogger CreateChildLogger(LogPolicy policy) { if (policy == null) { throw new ArgumentNullException(nameof(policy)); } policy.ErrorHandler = _ErrorHandler; policy.Clock = policy.Clock ?? MinTimeClock.DefaultInstance; if (policy.LogWriter == null) { policy.LogWriter = new Writers.ForwardingLogWriter(this); } else { policy.LogWriter = new Writers.AggregateLogWriter(new ILogWriter[] { policy.LogWriter, new Writers.ForwardingLogWriter(this) }); } return(new Logger(policy)); }
// 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; }