예제 #1
0
        /// <summary>
        /// 初始化一个<see cref="Log4NetLoggerAdapter"/>类型的新实例
        /// </summary>
        public Log4NetLoggerAdapter()
        {
            const string fileName   = "log4net.config";
            string       configFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);

            if (File.Exists(configFile))
            {
                XmlConfigurator.ConfigureAndWatch(new FileInfo(configFile));
                return;
            }
            RollingFileAppender appender = new RollingFileAppender
            {
                Name               = "root",
                File               = "logs\\log_",
                AppendToFile       = true,
                LockingModel       = new FileAppender.MinimalLock(),
                RollingStyle       = RollingFileAppender.RollingMode.Date,
                DatePattern        = "yyyyMMdd-HH\".log\"",
                StaticLogFileName  = false,
                MaxSizeRollBackups = 10,
                Layout             = new PatternLayout("[%d{yyyy-MM-dd HH:mm:ss.fff}] %-5p %c.%M %t %w %n%m%n")
            };

            appender.ClearFilters();
            appender.AddFilter(new LevelMatchFilter {
                LevelToMatch = Level.Info
            });
            BasicConfigurator.Configure(appender);
            appender.ActivateOptions();
        }
예제 #2
0
        /// <summary>
        /// 初始化一个<see cref="Log4NetLoggerProvider"/>类型的新实例
        /// </summary>
        public Log4NetLoggerProvider(string log4NetConfigFile)
        {
            string   file     = log4NetConfigFile ?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DefaultLog4NetFileName);
            Assembly assembly = Assembly.GetEntryAssembly() ?? GetCallingAssemblyFromStartup();

            _loggerRepository = LogManager.CreateRepository(assembly, typeof(Hierarchy));

            if (File.Exists(file))
            {
                XmlConfigurator.ConfigureAndWatch(_loggerRepository, new FileInfo(file));
                return;
            }
            RollingFileAppender appender = new RollingFileAppender
            {
                Name               = "root",
                File               = "log\\log_",
                AppendToFile       = true,
                LockingModel       = new FileAppender.MinimalLock(),
                RollingStyle       = RollingFileAppender.RollingMode.Date,
                DatePattern        = "yyyyMMdd-HH\".log\"",
                StaticLogFileName  = false,
                MaxSizeRollBackups = 10,
                Layout             = new PatternLayout("[%d{HH:mm:ss.fff}] %-5p %c T%t %n%m%n")
            };

            appender.ClearFilters();
            appender.AddFilter(new LevelMatchFilter()
            {
                LevelToMatch = Level.Debug
            });
            BasicConfigurator.Configure(_loggerRepository, appender);
            appender.ActivateOptions();
        }
예제 #3
0
        /// <summary>
        /// 初始化一个<see cref="Log4NetAdapter"/>类型的新实例
        /// </summary>k
        public Log4NetAdapter()
        {
            var configFile = Path.Combine(ConfigPath, FileName);

            if (File.Exists(configFile))
            {
                XmlConfigurator.ConfigureAndWatch(new FileInfo(configFile));
                return;
            }
            var appender = new RollingFileAppender
            {
                Name               = "root",
                File               = "logs\\log_",
                AppendToFile       = true,
                LockingModel       = new FileAppender.MinimalLock(),
                RollingStyle       = RollingFileAppender.RollingMode.Date,
                DatePattern        = "yyyyMMdd-HH\".log\"",
                StaticLogFileName  = false,
                MaxSizeRollBackups = 10,
                Layout             = new PatternLayout("[%d{yyyy-MM-dd HH:mm:ss.fff}] %-5p %c %t %w %n%m%n")
                                     //Layout = new PatternLayout("[%d [%t] %-5p %c [%x] - %m%n]")
            };

            appender.ClearFilters();
            appender.AddFilter(new LevelRangeFilter
            {
                LevelMin = Level.Debug,
                LevelMax = Level.Fatal
            });
            BasicConfigurator.Configure(appender);
            appender.ActivateOptions();
        }
예제 #4
0
        /// <summary>
        /// 初始化一个<see cref="Log4NetAdapter"/>类型的新实例
        /// </summary>k
        public Log4NetAdapter()
        {
            var configFile = Path.Combine(ConfigPath, FileName);

            if (File.Exists(configFile))
            {
                log4net.GlobalContext.Properties["LogSite"] = LogSite;
                XmlConfigurator.ConfigureAndWatch(new FileInfo(configFile));
                return;
            }
            var appender = new RollingFileAppender
            {
                Name               = "root",
                File               = $"_logs\\local\\{DateTime.Now:yyyyMM}\\",
                AppendToFile       = true,
                LockingModel       = new FileAppender.MinimalLock(),
                RollingStyle       = RollingFileAppender.RollingMode.Date,
                DatePattern        = "dd\".log\"",
                StaticLogFileName  = false,
                MaxSizeRollBackups = 100,
                MaximumFileSize    = "2MB",
                Layout             = new PatternLayout("[%d{yyyy-MM-dd HH:mm:ss.fff}] %-5p %c %t %w %n%m%n")
                                     //Layout = new PatternLayout("[%d [%t] %-5p %c [%x] - %m%n]")
            };

            appender.ClearFilters();
            appender.AddFilter(new LevelRangeFilter
            {
                LevelMin = Level.Debug,
                LevelMax = Level.Fatal
            });
            BasicConfigurator.Configure(appender);
            appender.ActivateOptions();
        }
        //LOGGER
        /// <summary>
        /// Static function that configures the log4net logger object.
        /// </summary>
        /// <returns>Interface to the logger.</returns>
        static log4net.ILog ConfigureLogger()
        {
            // Programmatic configuration
            // follows (with some streamlining) the example from Brendan Long and Ron Grabowski
            // org.apache.logging.log4net-user
            // These config statements create a RollingFile Appender.  Rolling File Appenders rollover on each execution of the test harness,
            // in this case, following the Composite RollingMode.  Alternative log4net appenders may be added  or replace this default appender at the programmer's discretion.

            // PatternLayout layout = new PatternLayout("%d [%t] %-5p %c - %m%n");

            PatternLayout layout = new PatternLayout("%d %-5p %c - %m%n");

            log4net.Appender.RollingFileAppender appender = new RollingFileAppender();

            appender.Layout            = layout;
            appender.AppendToFile      = true;
            appender.MaxFileSize       = 10000000;
            appender.RollingStyle      = RollingFileAppender.RollingMode.Composite;
            appender.StaticLogFileName = true;

            appender.File = Properties.Settings.Default.log4netDir; // all logs will be created in the subdirectory logs below where the test harness is executing

            // Configure filter to accept log messages of any level.
            log4net.Filter.LevelMatchFilter traceFilter = new log4net.Filter.LevelMatchFilter();
            traceFilter.LevelToMatch = log4net.Core.Level.Debug;
            appender.ClearFilters();
            appender.AddFilter(traceFilter);

            appender.ImmediateFlush = true;
            appender.ActivateOptions();

            // Attach appender into hierarchy
            log4net.Repository.Hierarchy.Logger root =
                ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
            root.AddAppender(appender);
            root.Repository.Configured = true;

            log4net.ILog log = log4net.LogManager.GetLogger("WBM-LOGGER");

            return(log);
        }
예제 #6
0
        public Log4NetLoggerAdapter()
        {
            var appender = new RollingFileAppender
            {
                Name               = "root",
                File               = "logs\\log4net.log",
                AppendToFile       = true,
                LockingModel       = new FileAppender.MinimalLock(),
                RollingStyle       = RollingFileAppender.RollingMode.Size,
                StaticLogFileName  = false,
                Threshold          = Level.Debug,
                MaxSizeRollBackups = 10,
                Layout             = new PatternLayout("%n[%d{yyyy-MM-dd HH:mm:ss.fff}] %-5p %c %t %w %n%m%n"),
            };

            appender.ClearFilters();
            appender.AddFilter(new LevelMatchFilter {
                LevelToMatch = Level.Info
            });
            BasicConfigurator.Configure(appender);
            appender.ActivateOptions();
        }