public static IDisposable TemporaryRootAppender([CanBeNull] IAppender appender) { if (appender == null) { return(null); } Log4NetUtils.AddRootAppender(appender); return(new DisposableCallback(() => Log4NetUtils.RemoveRootAppender(appender))); }
public void Attach() { // TODO Params to attach to specific repository/logger? if (_appender != null) { throw new InvalidOperationException("Already attached"); } // This should use the proper logger repository and // it's a no-op if the appender is already attached: Log4NetUtils.AddRootAppender(Interceptor); var appender = new BufferAppender(Math.Max(1, Capacity), DateTime.Now); var guid = Guid.NewGuid().ToString("N"); // hex digits only appender.Name = $"Inspector_{guid}"; appender.Threshold = ConvertLevel(Threshold); if (!string.IsNullOrEmpty(LoggerPrefix)) { appender.AddFilter(new LoggerMatchFilter { LoggerToMatch = LoggerPrefix }); appender.AddFilter(new DenyAllFilter()); } // An appender's Threshold and Filter chain interact as follows: // 1. ignore e if e.Level < a.Threshold // 2. walk filter chain until first Accept or Deny // 3. PreAppendCheck() and exit if false // 4. call Append(e) Interceptor.AddAppender(appender); // Just for the record: // Adding the same instance again is a no-op. Adding another // instance with the *same name* will be added to the list! _appender = appender; }