Ejemplo n.º 1
0
        /// <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));
        }
Ejemplo n.º 2
0
        /// <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.");
            }

            Assembly assembly = null;

            if (string.IsNullOrEmpty(this.options.LoggerRepository))
            {
#if NETCOREAPP1_1
                assembly = Assembly.GetEntryAssembly();
#else
                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));
            }

            string fileNamePath = options.Log4NetConfigFileName;
            if (!Path.IsPathRooted(fileNamePath))
            {
#if NETCOREAPP1_1
                if (!File.Exists(fileNamePath))
                {
                    fileNamePath = Path.Combine(Path.GetDirectoryName(assembly.Location), fileNamePath);
                }
#else
                fileNamePath = Path.Combine(AppContext.BaseDirectory, fileNamePath);
#endif
            }

            fileNamePath = Path.GetFullPath(fileNamePath);
            if (options.Watch)
            {
                XmlConfigurator.ConfigureAndWatch(
                    this.loggerRepository,
                    new FileInfo(fileNamePath));
            }
            else
            {
                var configXml = ParseLog4NetConfigFile(fileNamePath);
                if (this.options.PropertyOverrides != null &&
                    this.options.PropertyOverrides.Any())
                {
                    configXml = UpdateNodesWithOverridingValues(
                        configXml,
                        this.options.PropertyOverrides);
                }

                XmlConfigurator.Configure(this.loggerRepository, configXml.DocumentElement);
            }
        }
Ejemplo n.º 3
0
        /// <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, string log4NetConfigFile)
        {
            var options = new Log4NetProviderOptions(log4NetConfigFile);

            return(builder.AddLog4Net(options));
        }
Ejemplo n.º 4
0
        /// <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));
        }
Ejemplo n.º 5
0
 /// <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);
 }
Ejemplo n.º 6
0
 /// <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);
 }
Ejemplo n.º 7
0
 /// <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.log     = LogManager.GetLogger(options.LoggerRepository, options.Name);
 }