/// <summary> /// Initializes a new instance of the <see cref="Log4NetProvider"/> class. /// </summary> /// <param name="options">The options.</param> /// <exception cref="ArgumentNullException">options</exception> /// <exception cref="NotSupportedException">Wach cannot be true when you are overwriting config file values with values from configuration section.</exception> public Log4NetProvider(Log4NetProviderOptions options) { this.SetOptionsIfValid(options); Assembly loggingAssembly = GetLoggingReferenceAssembly(); this.CreateLoggerRepository(loggingAssembly) .ConfigureLog4NetLibrary(loggingAssembly); }
/// <summary> /// Creates the logger implementation. /// </summary> /// <param name="name">The name.</param> /// <returns>The <see cref="Log4NetLogger"/> instance.</returns> private Log4NetLogger CreateLoggerImplementation(string name) { var options = new Log4NetProviderOptions { Name = name, LoggerRepository = this.loggerRepository.Name, }; return(new Log4NetLogger(options)); }
/// <summary> /// Creates the logger implementation. /// </summary> /// <param name="name">The name.</param> /// <returns>The <see cref="Log4NetLogger"/> instance.</returns> private Log4NetLogger CreateLoggerImplementation(string name) { var loggerOptions = new Log4NetProviderOptions { Name = name, LoggerRepository = this.loggerRepository.Name, OverrideCriticalLevelWith = this.options.OverrideCriticalLevelWith, ScopeFactory = this.options.ScopeFactory ?? new Log4NetScopeFactory(new Log4NetScopeRegistry()) }; return(new Log4NetLogger(loggerOptions)); }
/// <summary> /// Initializes a new instance of the <see cref="Log4NetProvider"/> class. /// </summary> /// <param name="options">The options.</param> /// <exception cref="ArgumentNullException">options</exception> /// <exception cref="NotSupportedException">Wach cannot be true when you are overwriting config file values with values from configuration section.</exception> public Log4NetProvider(Log4NetProviderOptions options) { this.options = options ?? throw new ArgumentNullException(nameof(options)); if (options.Watch && options.PropertyOverrides.Any()) { throw new NotSupportedException("Wach cannot be true when you are overwriting config file values with values from configuration section."); } if (string.IsNullOrEmpty(this.options.LoggerRepository)) { #if NETCOREAPP1_1 Assembly assembly = Assembly.GetEntryAssembly(); #else Assembly assembly = Assembly.GetExecutingAssembly(); #endif this.loggerRepository = LogManager.CreateRepository( assembly ?? GetCallingAssemblyFromStartup(), typeof(log4net.Repository.Hierarchy.Hierarchy)); } else { this.loggerRepository = LogManager.CreateRepository( options.LoggerRepository, typeof(log4net.Repository.Hierarchy.Hierarchy)); } if (options.Watch) { XmlConfigurator.ConfigureAndWatch( this.loggerRepository, new FileInfo(Path.GetFullPath(options.Log4NetConfigFileName))); } else { var configXml = ParseLog4NetConfigFile(options.Log4NetConfigFileName); if (this.options.PropertyOverrides != null && this.options.PropertyOverrides.Any()) { configXml = UpdateNodesWithOverridingValues( configXml, this.options.PropertyOverrides); } XmlConfigurator.Configure(this.loggerRepository, configXml.DocumentElement); } }
/// <summary> /// Ensures that provided options combinations are valid, and sets the class field if everything is ok. /// </summary> /// <param name="options">The options to validate.</param> /// <exception cref="NotSupportedException"> /// Throws when the Watch option is set and there are properties to override. /// </exception> /// <exception cref="ArgumentNullException"> /// Throws when the options parameter is null. /// </exception> private void SetOptionsIfValid(Log4NetProviderOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (options.Watch && options.PropertyOverrides.Any()) { throw new NotSupportedException("Wach cannot be true when you are overwriting config file values with values from configuration section."); } this.options = options; }
/// <inheritdoc/> public Level TranslateLogLevel(LogLevel logLevel, Log4NetProviderOptions options) { Level log4NetLevel = null; switch (logLevel) { case LogLevel.Critical: string overrideCriticalLevelWith = options.OverrideCriticalLevelWith; log4NetLevel = !string.IsNullOrEmpty(overrideCriticalLevelWith) && overrideCriticalLevelWith.Equals(LogLevel.Critical.ToString(), StringComparison.OrdinalIgnoreCase) ? Level.Critical : Level.Fatal; break; case LogLevel.Debug: log4NetLevel = Level.Debug; break; case LogLevel.Error: log4NetLevel = Level.Error; break; case LogLevel.Information: log4NetLevel = Level.Info; break; case LogLevel.Warning: log4NetLevel = Level.Warn; break; case LogLevel.Trace: log4NetLevel = Level.Trace; break; } return(log4NetLevel); }
/// <summary> /// Adds the log4net logging provider. /// </summary> /// <param name="builder">The logging builder instance.</param> /// <param name="log4NetConfigFile">The log4net Config File.</param> /// <returns>The <see ref="ILoggingBuilder" /> passed as parameter with the new provider registered.</returns> public static ILoggingBuilder AddLog4Net(this ILoggingBuilder builder, Log4NetProviderOptions options) { builder.Services.AddSingleton <ILoggerProvider>(new Log4NetProvider(options)); return(builder); }
/// <summary> /// Adds the log4net logging provider. /// </summary> /// <param name="builder">The logging builder instance.</param> /// <param name="log4NetConfigFile">The log4net Config File.</param> /// <param name="watch">if set to <c>true</c>, the configuration will be reloaded when the xml configuration file changes.</param> /// <returns> /// The <see ref="ILoggingBuilder" /> passed as parameter with the new provider registered. /// </returns> public static ILoggingBuilder AddLog4Net(this ILoggingBuilder builder, string log4NetConfigFile, bool watch) { var options = new Log4NetProviderOptions(log4NetConfigFile, watch); return(builder.AddLog4Net(options)); }
/// <summary> /// Adds the log4net logging provider. /// </summary> /// <param name="builder">The logging builder instance.</param> /// <returns>The <see ref="ILoggingBuilder" /> passed as parameter with the new provider registered.</returns> public static ILoggingBuilder AddLog4Net(this ILoggingBuilder builder) { var options = new Log4NetProviderOptions(); return(builder.AddLog4Net(options)); }
/// <summary> /// Adds the log4net logging provider. /// </summary> /// <param name="factory">The logger factory.</param> /// <param name="options">The options for log4net provider.</param> /// <returns>The <see cref="ILoggerFactory"/> after adding the log4net provider.</returns> public static ILoggerFactory AddLog4Net(this ILoggerFactory factory, Log4NetProviderOptions options) { factory.AddProvider(new Log4NetProvider(options)); return(factory); }
/// <summary> /// Initializes a new instance of the <see cref="Log4NetLogger"/> class. /// </summary> /// <param name="options">The log4net provider options.</param> public Log4NetLogger(Log4NetProviderOptions options) { this.options = options ?? throw new ArgumentNullException(nameof(options)); this.logger = LogManager.GetLogger(options.LoggerRepository, options.Name).Logger; }
/// <inheritdoc/> public LoggingEvent CreateLoggingEvent <TState>(MessageCandidate <TState> messageCandidate, log4net.Core.ILogger logger, Log4NetProviderOptions options) { Type callerStackBoundaryDeclaringType = typeof(LoggerExtensions); string message = messageCandidate.Formatter(messageCandidate.State, messageCandidate.Exception); Level logLevel = options.LogLevelTranslator.TranslateLogLevel(messageCandidate.LogLevel, options); if (logLevel == null || (string.IsNullOrEmpty(message) && messageCandidate.Exception == null)) { return(null); } return(new LoggingEvent( callerStackBoundaryDeclaringType: callerStackBoundaryDeclaringType, repository: logger.Repository, loggerName: logger.Name, level: logLevel, message: message, exception: messageCandidate.Exception)); }