private static RollingFileAppender InitializeInfoLogger(string logFilePath)
    {
        var infoPatternLayout = new PatternLayout
        {
            ConversionPattern = "%date %logger %level - %message%newline",
        };

        infoPatternLayout.ActivateOptions();

        var filter = new LevelRangeFilter
        {
            LevelMin = Level.Debug,
            LevelMax = Level.Info,
        };

        filter.ActivateOptions();
        var infofileAppender = new RollingFileAppender
        {
            AppendToFile      = true,
            File              = logFilePath,
            Layout            = infoPatternLayout,
            LockingModel      = new FileAppender.MinimalLock(),
            MaximumFileSize   = "10MB",
            RollingStyle      = RollingFileAppender.RollingMode.Size,
            StaticLogFileName = false
        };

        infofileAppender.AddFilter(filter);
        infofileAppender.ActivateOptions();

        return(infofileAppender);
    }
    private static RollingFileAppender InitializeErrorLogger(string logFilePath)
    {
        var errorPatternLayout = new PatternLayout
        {
            ConversionPattern = "%date %logger %level - %message%newline%exception%stacktracedetail",
            IgnoresException  = false,
        };

        errorPatternLayout.ActivateOptions();
        var filter = new LevelRangeFilter
        {
            LevelMin = Level.Warn,
            LevelMax = Level.Fatal
        };

        filter.ActivateOptions();
        var errorFileAppender = new RollingFileAppender
        {
            AppendToFile      = true,
            File              = logFilePath,
            LockingModel      = new FileAppender.MinimalLock(),
            Layout            = errorPatternLayout,
            MaximumFileSize   = "10MB",
            RollingStyle      = RollingFileAppender.RollingMode.Size,
            StaticLogFileName = false
        };

        errorFileAppender.AddFilter(filter);
        errorFileAppender.ActivateOptions();
        return(errorFileAppender);
    }
Exemple #3
0
        /// <summary>
        /// configure method
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="repository">repository</param>
        public override void Configure(string key, ILoggerRepository repository)
        {
            var path = Path.Replace(":", "_");

            foreach (var level in Levels)
            {
                var appender = new log4net.Appender.RollingFileAppender();
                appender.File           = Folder + (string.IsNullOrEmpty(path) ? "" : path) + "/" + level.ToString().ToLower() + ".log";
                appender.AppendToFile   = true;
                appender.ImmediateFlush = true;
                appender.LockingModel   = new FileAppender.MinimalLock();
                appender.Threshold      = level;

                var layout = new PatternLayout(Pattern);
                layout.ActivateOptions();

                appender.Layout = layout;
                appender.Name   = key + "_" + level.ToString().ToLower();

                var filter = new LevelRangeFilter();
                filter.LevelMax = level;
                filter.LevelMin = level;
                filter.ActivateOptions();
                appender.AddFilter(filter);

                appender.ActivateOptions();

                BasicConfigurator.Configure(repository, appender);
            }
        }
        public static void InitLogging()
        {
            var layout = new PatternLayout {
                ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"
            };

            layout.ActivateOptions();

            var filter = new LevelRangeFilter {
                LevelMin = Level.Warn, LevelMax = Level.Fatal
            };

            filter.ActivateOptions();

            var eventLogAppender = new EventLogAppender
            {
                ApplicationName = "NServiceBus Sample Server",
                Threshold       = Level.All,
                Layout          = layout
            };

            eventLogAppender.AddFilter(filter);
            eventLogAppender.ActivateOptions();

            var consoleAppender = new ConsoleAppender
            {
                Threshold = Level.Info,
                Layout    = layout
            };

            BasicConfigurator.Configure(consoleAppender);

            LogManager.Use <Log4NetFactory>();
        }
Exemple #5
0
        private static ConsoleAppender InitializeErrorLogger()
        {
            var errorPatternLayout = new PatternLayout
            {
                ConversionPattern = "%date %logger %level - %message%newline%exception%stacktracedetail",
                IgnoresException  = false,
            };

            errorPatternLayout.ActivateOptions();
            var filter = new LevelRangeFilter
            {
                LevelMin = Level.Warn,
                LevelMax = Level.Fatal
            };

            filter.ActivateOptions();
            var errorFileAppender = new ConsoleAppender
            {
                Layout = errorPatternLayout
            };

            errorFileAppender.AddFilter(filter);
            errorFileAppender.ActivateOptions();
            return(errorFileAppender);
        }
Exemple #6
0
        private static ConsoleAppender InitializeInfoLogger()
        {
            var infoPatternLayout = new PatternLayout
            {
                ConversionPattern = "%date %logger %level - %message%newline",
            };

            infoPatternLayout.ActivateOptions();

            var filter = new LevelRangeFilter
            {
                LevelMin = Level.Debug,
                LevelMax = Level.Info,
            };

            filter.ActivateOptions();
            var infofileAppender = new ConsoleAppender
            {
                Layout = infoPatternLayout,
            };

            infofileAppender.AddFilter(filter);
            infofileAppender.ActivateOptions();

            return(infofileAppender);
        }
Exemple #7
0
        private static FilterSkeleton CreateLevelFilter(Level level)
        {
            var levelFilter = new LevelRangeFilter();

            levelFilter.LevelMin = level;
            levelFilter.LevelMax = level;
            levelFilter.ActivateOptions();

            return(levelFilter);
        }
        /// <summary>
        /// Appends inclusive filter to the log
        /// </summary>
        /// <param name="minRange">The min range.</param>
        /// <param name="maxRange">The max range.</param>
        /// <returns>same syntax</returns>
        public LogSyntax Filter(LogLevel minRange, LogLevel maxRange)
        {
            var filter = new LevelRangeFilter
            {
                LevelMin      = minRange.ToLog4Net(),
                LevelMax      = maxRange.ToLog4Net(),
                AcceptOnMatch = true
            };

            filter.ActivateOptions();
            _skeleton.AddFilter(filter);
            //_skeleton.ActivateOptions();
            return(this);
        }
        /// <summary>
        /// 设置日志格式
        /// </summary>
        /// <param name="repositoryName">Repository(版本库)名称</param>
        /// <param name="level">日志等级</param>
        static void CreateLogger(string repositoryName, Level level)
        {
            ILoggerRepository curRepository = LogManager.GetRepository(repositoryName);

            //设置日志属性
            RollingFileAppender curAppender = new RollingFileAppender()
            {
                //日志的名称:版本库+日志登记命名
                Name = repositoryName,
                //日志的保存目录
                File = string.Format("log\\{0}\\", repositoryName),
                //是否静态日志名,静态通过fileAppender.File设置,动态通过日期或者文件大小进行设置
                StaticLogFileName = false,
                //根据日期设置日志文件名
                RollingStyle = RollingFileAppender.RollingMode.Composite,
                //日志文件名的命名规则
                DatePattern = string.Format("yyyyMMdd{0}.lo\\g", level),
                //是否把日志加入到原有日志中。
                AppendToFile = true,
                //每个单独日志文件的最大大小。
                MaxFileSize = 50 * 1024 * 1024,
                //日志可以重写的数量,log,log.1,log.2....
                MaxSizeRollBackups = 1000,
                //选择UTF8编码,确保中文不乱码
                Encoding     = Encoding.UTF8,
                LockingModel = new log4net.Appender.FileAppender.MinimalLock()
            };

            //过滤日志等级
            LevelRangeFilter filter = new LevelRangeFilter()
            {
                LevelMax = level == Level.All ? Level.Fatal : level, LevelMin = level
            };

            curAppender.AddFilter(filter);
            filter.ActivateOptions();

            //设置日志记录的内容
            PatternLayout pattenLayout = new PatternLayout()
            {
                ConversionPattern = "%date [%thread] %-5level  %logger %message%newline"
            };

            curAppender.Layout = pattenLayout;
            pattenLayout.ActivateOptions();

            curAppender.ActivateOptions();
            BasicConfigurator.Configure(curRepository, curAppender);
        }
Exemple #10
0
        public override void Configure(string key, ILoggerRepository repository)
        {
            foreach (var level in Levels)
            {
                var appender = new log4net.Appender.ConsoleAppender();

                var layout = new PatternLayout(Pattern);
                layout.ActivateOptions();

                appender.Layout = layout;
                appender.Name   = key + "_" + level.ToString().ToLower();

                var filter = new LevelRangeFilter();
                filter.LevelMax = level;
                filter.LevelMin = level;
                filter.ActivateOptions();
                appender.AddFilter(filter);

                appender.ActivateOptions();

                BasicConfigurator.Configure(repository, appender);
            }
        }
Exemple #11
0
        private static RollingFileAppender GetNewFileApender(string appenderName, string file, int maxSizeRollBackups, bool appendToFile = true, bool staticLogFileName = false, string maximumFileSize = "5MB", RollingFileAppender.RollingMode rollingMode = RollingFileAppender.RollingMode.Composite, string layoutPattern = "%d [%t] %-5p %c  - %m%n")
        {
            var appender = new RollingFileAppender
            {
                Name         = appenderName,
                Encoding     = System.Text.Encoding.UTF8,
                File         = file,
                AppendToFile = appendToFile,
                LockingModel = new FileAppender.MinimalLock(),
                RollingStyle = rollingMode,
                //DatePattern = datePattern,
                StaticLogFileName  = staticLogFileName,
                MaxSizeRollBackups = maxSizeRollBackups,
                MaximumFileSize    = maximumFileSize,
                CountDirection     = 0,
                Threshold          = Level.All
            };
            var layout = new PatternLayout(layoutPattern)
            {
                Header = "[Header]" + Environment.NewLine,
                Footer = "[Footer]" + Environment.NewLine
            };

            appender.Layout = layout;
            var filter = new LevelRangeFilter
            {
                LevelMin = Level.All,
                LevelMax = Level.Fatal
            };

            appender.AddFilter(filter);

            layout.ActivateOptions();
            filter.ActivateOptions();
            appender.ActivateOptions();
            return(appender);
        }
Exemple #12
0
        public static void Configure(string logFilesFolderPath, Log4NetConfigurator configurator)
        {
            if (logFilesFolderPath.StartsWith("~"))
            {
                logFilesFolderPath = logFilesFolderPath.Substring(1);
            }
            if (logFilesFolderPath.StartsWith("/"))
            {
                logFilesFolderPath = logFilesFolderPath.Substring(1);
            }

            var hierarchy = (Hierarchy)LogManager.GetRepository();


            hierarchy.Root.Level = Level.Warn;
            hierarchy.Configured = true;

            var patternLayout = new PatternLayout
            {
                ConversionPattern = "%date [%thread] %-5level %logger - %P{Tenant} - %message%newline"
            };

            patternLayout.ActivateOptions();

            var debugFileAppender = BaseFileAppender(patternLayout);

            debugFileAppender.Name = "debug-file";
            debugFileAppender.File = Path.Combine(logFilesFolderPath, "orchard-debug");
            debugFileAppender.ActivateOptions();
            hierarchy.Root.AddAppender(debugFileAppender);

            var errorFileAppender = BaseFileAppender(patternLayout);

            errorFileAppender.Name = "error-file";
            errorFileAppender.File = Path.Combine(logFilesFolderPath, "orchard-error");
            var levelFilter = new LevelRangeFilter {
                LevelMin = Level.Error
            };

            levelFilter.ActivateOptions();
            errorFileAppender.AddFilter(levelFilter);
            errorFileAppender.ActivateOptions();
            hierarchy.Root.AddAppender(errorFileAppender);

            var simpleLayout = new SimpleLayout();

            simpleLayout.ActivateOptions();
            var debuggerAppender = new DebugAppender();

            debuggerAppender.Name           = "debugger";
            debuggerAppender.ImmediateFlush = true;
            debuggerAppender.Layout         = simpleLayout;
            debuggerAppender.ActivateOptions();
            hierarchy.Root.AddAppender(debuggerAppender);

            GetLogger("Orchard").AddAppender(debuggerAppender);
            GetLogger("Orchard.Localization").Level = Level.Warn;
            GetLogger("NHibernate.Cache").Level     = Level.Error;
            GetLogger("NHibernate.AdoNet.AbstractBatcher").Level    = Level.Off;
            GetLogger("NHibernate.Util.ADOExceptionReporter").Level = Level.Off;

            configurator(hierarchy);
        }