Esempio n. 1
0
        private static IAppender GetRollingAppender(string arg)
        {
            var level = Level.All;

            var rollingFileAppenderLayout = new PatternLayout("%date{HH:mm:ss,fff}|T%2thread|%25.25logger|%5.5level| %message%newline");

            rollingFileAppenderLayout.ActivateOptions();

            var rollingFileAppenderName = string.Format("{0}{1}", RollingFileAppenderNameDefault, arg);

            var rollingFileAppender = new RollingFileAppender();

            rollingFileAppender.Name              = rollingFileAppenderName;
            rollingFileAppender.Threshold         = level;
            rollingFileAppender.CountDirection    = 0;
            rollingFileAppender.AppendToFile      = true;
            rollingFileAppender.LockingModel      = new FileAppender.MinimalLock();
            rollingFileAppender.StaticLogFileName = true;
            rollingFileAppender.RollingStyle      = RollingFileAppender.RollingMode.Date;
            rollingFileAppender.DatePattern       = ".yyyy-MM-dd'.log'";
            rollingFileAppender.Layout            = rollingFileAppenderLayout;
            rollingFileAppender.File              = string.Format("{0}.{1}", "log", arg);
            rollingFileAppender.ActivateOptions();

            return(rollingFileAppender);
        }
Esempio n. 2
0
        // Todo :定义日志文件路径格式
        static void InitLogPath(string repository)
        {
            RollingFileAppender appender = new RollingFileAppender();

            appender.File = repository + "\\Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";

            appender.AppendToFile = true;

            appender.MaxSizeRollBackups = -1;

            //appender.MaximumFileSize = "1MB";

            appender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;

            appender.DatePattern = "yyyy-MM-dd_HH\".log\"";

            appender.StaticLogFileName = false;

            appender.LockingModel = new log4net.Appender.FileAppender.MinimalLock();

            appender.Layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level - %message%newline");

            appender.ActivateOptions();

            log4net.Config.BasicConfigurator.Configure(appender);
        }
Esempio n. 3
0
        public static void Setup()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
            patternLayout.ActivateOptions();

            RollingFileAppender roller = new RollingFileAppender();

            roller.AppendToFile       = false;
            roller.File               = @"Logs\EventLog.txt";
            roller.Layout             = patternLayout;
            roller.MaxSizeRollBackups = 5;
            roller.MaximumFileSize    = "20MB";
            roller.RollingStyle       = RollingFileAppender.RollingMode.Size;
            roller.StaticLogFileName  = true;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);

            ConsoleAppender memory = new ConsoleAppender();

            memory.ActivateOptions();
            hierarchy.Root.AddAppender(memory);

            hierarchy.Root.Level = Level.Info;
            hierarchy.Configured = true;
        }
Esempio n. 4
0
        SystemTrace()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            PatternLayout layout = new PatternLayout();

            layout.ConversionPattern = "%date{yyyy-MM-dd HH:mm:ss.fff} %message%newline";
            layout.ActivateOptions();

            RollingFileAppender appender = new RollingFileAppender();

            appender.AppendToFile       = false;
            appender.File               = Path.Combine(Path.GetTempPath(), "StbInstaller-{0}.log".FormatWith(DateTime.Now.ToString("yyyyMMddHHmmssfff")));
            appender.Layout             = layout;
            appender.MaxSizeRollBackups = 5;
            appender.MaximumFileSize    = "1MB";
            appender.RollingStyle       = RollingFileAppender.RollingMode.Size;
            appender.StaticLogFileName  = true;
            appender.ActivateOptions();

            hierarchy.Root.AddAppender(appender);
            hierarchy.Root.Level = Level.All;
            hierarchy.Configured = true;

            _logger = hierarchy.Root;
        }
Esempio n. 5
0
        /// <summary>
        /// 加载对应日志类型配置
        /// </summary>
        /// <param name="type">日志类型</param>
        /// <returns></returns>
        private static ILog GetLog(LogType type)
        {
            //这里要清空配置 防止追加
            var repository = LogManager.GetRepository();

            repository.ResetConfiguration();

            var           desc          = GetModuleDescription(type);
            Hierarchy     hierarchy     = (Hierarchy)LogManager.GetRepository();
            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
            patternLayout.ActivateOptions();
            RollingFileAppender roller = new RollingFileAppender();

            roller.AppendToFile       = true;
            roller.File               = "logs/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + desc + ".log";
            roller.Layout             = patternLayout;
            roller.MaxSizeRollBackups = 20;
            roller.MaximumFileSize    = "10MB";
            roller.RollingStyle       = RollingFileAppender.RollingMode.Date;
            roller.Encoding           = Encoding.UTF8;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);
            hierarchy.Root.Level = Level.Info;
            hierarchy.Configured = true;
            return(LogManager.GetLogger(desc));
        }
Esempio n. 6
0
        private static IAppender CreateFileAppender(string filePath)
        {
            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = "%date %level %logger: %message%newline"; //"%date [%-4thread] %-5level %logger{1} - %message%newline";// "%date [%thread] %-5level %logger : %message%newline";  //"%date %level %logger: %message%newline";
            patternLayout.ActivateOptions();
            string applicationName       = string.Empty;
            RollingFileAppender appender = new RollingFileAppender();

            if (appName == string.Empty)
            {
                applicationName = Process.GetCurrentProcess().ProcessName.Replace(".vshost", string.Empty).Replace(".", "_");
            }
            else
            {
                applicationName = appName;
            }
            //appender.Name = name;
            string year  = DateTime.Now.Year.ToString();
            string month = DateTime.Now.Month.ToString().PadLeft(2, '0');
            string day   = DateTime.Now.Day.ToString().PadLeft(2, '0');

            appender.File               = string.Concat(filePath, "\\", applicationName, "\\", year, "\\", month, "\\", year, month, day, applicationName, "_log.txt");
            appender.AppendToFile       = true;
            appender.MaxSizeRollBackups = 15;
            appender.RollingStyle       = RollingFileAppender.RollingMode.Date;
            appender.MaximumFileSize    = "10MB";
            appender.Layout             = patternLayout;
            appender.LockingModel       = new FileAppender.MinimalLock();
            appender.StaticLogFileName  = true;
            appender.ActivateOptions();
            return(appender);
        }
        public static void Initialise(string logName, string logFile)
        {
            var hierarchy = (Hierarchy)LogManager.CreateRepository(logName);

            var patternLayout = new PatternLayout {
                ConversionPattern = "%date %-5level - %message%newline%exception"
            };

            patternLayout.ActivateOptions();

            var rollingFileAppender = new RollingFileAppender
            {
                AppendToFile       = true,
                Name               = logName,
                File               = logFile,
                Layout             = patternLayout,
                MaxSizeRollBackups = 10,
                RollingStyle       = RollingFileAppender.RollingMode.Date,
                StaticLogFileName  = true
            };

            rollingFileAppender.ActivateOptions();

            hierarchy.Root.AddAppender(rollingFileAppender);
            hierarchy.Root.Level = Level.All;
            hierarchy.Configured = true;

            _log = LogManager.GetLogger(logName, logName);
        }
Esempio n. 8
0
        private void SetupLogging()
        {
            if (!Config.filelogging)
            {
                BasicConfigurator.Configure(this);
                return;
            }

            var layout = new PatternLayout()
            {
                ConversionPattern = "%date %6level [%3thread] %-30.30logger{1} - %message%newline%exception"
            };

            layout.ActivateOptions();
            var fileAppender = new RollingFileAppender()
            {
                File               = logFile.FullName,
                Layout             = layout,
                MaximumFileSize    = "10MB",
                MaxSizeRollBackups = 3,
                RollingStyle       = RollingFileAppender.RollingMode.Size,
                ImmediateFlush     = false,
                Threshold          = Level.Info
            };

            fileAppender.ActivateOptions();

            BasicConfigurator.Configure(this, fileAppender);
        }
Esempio n. 9
0
        public static void Configure()
        {
            if (isSetupLogHandler)
            {
                return;
            }
            isSetupLogHandler = true;

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

            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
            patternLayout.ActivateOptions();

            RollingFileAppender roller = new RollingFileAppender();

            roller.AppendToFile       = true;
            roller.File               = Environment.ExpandEnvironmentVariables(@"%AppData%\MyGitHubProject\UseRegistryInWPF\UseRegistryInWPFLog.txt");
            roller.Layout             = patternLayout;
            roller.MaxSizeRollBackups = 20;
            roller.MaximumFileSize    = "20MB";
            roller.RollingStyle       = RollingFileAppender.RollingMode.Size;
            roller.StaticLogFileName  = true;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);

            MemoryAppender memory = new MemoryAppender();

            memory.ActivateOptions();
            hierarchy.Root.AddAppender(memory);

            hierarchy.Root.Level = Level.Info;
            hierarchy.Configured = true;
        }
Esempio n. 10
0
        private ILog GetLog4NetLogger()
        {
            BasicConfigurator.Configure();

            // from https://stackoverflow.com/questions/16336917/can-you-configure-log4net-in-code-instead-of-using-a-config-file
            var hierarchy = (Hierarchy)LogManager.GetRepository();

            hierarchy.ResetConfiguration();
            hierarchy.Clear();

            var patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = "%date [%thread] %-5level %logger %message%newline";
            patternLayout.ActivateOptions();

            var roller = new RollingFileAppender();

            roller.AppendToFile   = false;
            roller.File           = $"{_logsFolder}/log4net.txt";
            roller.Layout         = patternLayout;
            roller.ImmediateFlush = true;
            roller.RollingStyle   = RollingFileAppender.RollingMode.Once;
            roller.MaxFileSize    = 128 * 1000 * 1000;
            roller.ActivateOptions();

            hierarchy.Threshold  = Level.Info;
            hierarchy.Root.Level = Level.Info;
            hierarchy.Root.AddAppender(roller);

            hierarchy.Configured = true;

            return(LogManager.GetLogger(GetType()));
        }
Esempio n. 11
0
        /// <summary>
        /// 使用文本记录信息
        /// </summary>
        /// <Author>焰尾迭</Author>
        /// <date>2015-09-22</date>
        private static void LoadFileAppender(ILoggerRepository repository)
        {
            //string txtLogPath = FileHelper.GetAbsolutePath(string.Format("/Logs/{0}.log", repository.Name));
            string txtLogPath = string.Format(@"Logs\{0}\", repository.Name);
            RollingFileAppender fileAppender = new RollingFileAppender();

            fileAppender.Name               = "LogFileAppender";
            fileAppender.File               = txtLogPath;
            fileAppender.AppendToFile       = true;
            fileAppender.MaxSizeRollBackups = 100;
            fileAppender.MaximumFileSize    = "1MB";
            fileAppender.RollingStyle       = log4net.Appender.RollingFileAppender.RollingMode.Size;
            fileAppender.DatePattern        = "yyyyMMddHH'.log'";
            fileAppender.StaticLogFileName  = false;
            fileAppender.RollingStyle       = RollingFileAppender.RollingMode.Date;

            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = "%-5p %d [%c] %m%n";
            patternLayout.ActivateOptions();
            fileAppender.Layout = patternLayout;

            //选择UTF8编码,确保中文不乱码。
            fileAppender.Encoding = Encoding.UTF8;
            fileAppender.ActivateOptions();
            log4net.Config.BasicConfigurator.Configure(repository, fileAppender);
        }
Esempio n. 12
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();
        }
Esempio n. 13
0
        public static void Initialize(string logDir, string fileName, string category)
        {
            _logFile = System.IO.Path.Combine(logDir, fileName);

            Hierarchy     hierarchy     = (Hierarchy)LogManager.GetRepository();
            TraceAppender tracer        = new TraceAppender();
            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = LOG_PATTERN;
            patternLayout.ActivateOptions();

            tracer.Layout = patternLayout;
            tracer.ActivateOptions();
            hierarchy.Root.AddAppender(tracer);

            RollingFileAppender roller = new RollingFileAppender();

            roller.Layout             = patternLayout;
            roller.AppendToFile       = true;
            roller.RollingStyle       = RollingFileAppender.RollingMode.Size;
            roller.MaxSizeRollBackups = 4;
            roller.MaximumFileSize    = "100KB";
            roller.StaticLogFileName  = true;
            roller.File = _logFile;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);

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

            _log = log4net.LogManager.GetLogger(category);
        }
Esempio n. 14
0
        public static void Setup()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = "%-5p %d %5rms %-22.22c{1} %-18.50M - %m%n";
            patternLayout.ActivateOptions();

            RollingFileAppender roller = new RollingFileAppender();

            roller.AppendToFile       = true;;
            roller.File               = Path.GetFullPath(Path.Combine(Thread.GetDomain().BaseDirectory, @"..\..\", "Log4net/Logs/Logs.txt"));
            roller.Layout             = patternLayout;
            roller.MaxSizeRollBackups = 5;
            roller.MaximumFileSize    = "10MB";
            roller.RollingStyle       = RollingFileAppender.RollingMode.Size;
            roller.StaticLogFileName  = true;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);

            ConsoleAppender console = new ConsoleAppender();

            console.Layout = patternLayout;
            console.ActivateOptions();
            hierarchy.Root.AddAppender(console);

            hierarchy.Root.Level = Level.All;
            hierarchy.Configured = true;
        }
Esempio n. 15
0
        /// <summary>
        /// Configures the log4net environment
        /// </summary>
        private void Setup(Level logLevel)
        {
            Directory.CreateDirectory(_logFolder);


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

            var patternLayout = new PatternLayout("%date [%thread] %-5level - %message%newline");

            patternLayout.ActivateOptions();

            // Log to a file
            var roller = new RollingFileAppender();

            roller.AppendToFile       = false;
            roller.File               = _logFile;
            roller.Layout             = patternLayout;
            roller.MaxSizeRollBackups = 5;
            roller.RollingStyle       = RollingFileAppender.RollingMode.Size;
            roller.StaticLogFileName  = true;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);

            // Log to the visual studio console
            var appender = new TraceAppender();

            appender.ActivateOptions();
            appender.Layout = patternLayout;
            hierarchy.Root.AddAppender(appender);

            hierarchy.Root.Level = logLevel;
            hierarchy.Configured = true;
        }
Esempio n. 16
0
        public Logger(string fileName, int maxSizeKB)
        {
            _layout.ConversionPattern = LogPattern;
            _layout.ActivateOptions();

            string assemblyTitle = ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false)).Title;

            fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), assemblyTitle, fileName);

            Hierarchy     hierarchy     = (Hierarchy)LogManager.GetRepository();
            PatternLayout patternLayout = new PatternLayout {
                ConversionPattern = LogPattern
            };

            patternLayout.ActivateOptions();

            RollingFileAppender roller = new RollingFileAppender
            {
                Layout             = patternLayout,
                AppendToFile       = true,
                RollingStyle       = RollingFileAppender.RollingMode.Size,
                MaxSizeRollBackups = 4,
                MaximumFileSize    = maxSizeKB + "KB",
                StaticLogFileName  = true,
                File = fileName
            };

            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);

            hierarchy.Root.Level = Level.Debug;
            hierarchy.Configured = true;
        }
Esempio n. 17
0
        void UseFileLogConfig(Hierarchy repository, LogLevel logLevelMax, LogLevel logLevelMin)
        {
            //var ip = JimuHelper.GetLocalIPAddress();
            PatternLayout layout = new PatternLayout
            {
                ConversionPattern = "%date{yyyy-MM-dd HH:mm:ss.fff} %-5p [%thread] %c %m%n"
            };

            layout.ActivateOptions();

            RollingFileAppender roller = new RollingFileAppender
            {
                AppendToFile = true,
            };
            var path = string.IsNullOrEmpty(_options.FileLogPath) ? "log" : _options.FileLogPath;

            roller.File = $@"{path}/";
            roller.PreserveLogFileNameExtension = true;
            roller.StaticLogFileName            = false;
            roller.MaxSizeRollBackups           = 0;
            roller.DatePattern  = $@"yyyyMMdd"".log""";
            roller.RollingStyle = RollingFileAppender.RollingMode.Date;
            roller.Layout       = layout;
            roller.MaxFileSize  = 10000000;
            roller.AddFilter(new LevelRangeFilter()
            {
                LevelMax = ConvertToLevel(logLevelMax),
                LevelMin = ConvertToLevel(logLevelMin)
            });
            roller.ActivateOptions();
            repository.Root.AddAppender(roller);
            repository.Configured = true;
        }
Esempio n. 18
0
            static Logger()
            {
                Hierarchy     hierarchy     = (Hierarchy)LogManager.GetRepository();
                TraceAppender tracer        = new TraceAppender();
                PatternLayout patternLayout = new PatternLayout();

                patternLayout.ConversionPattern = LOG_PATTERN;
                patternLayout.ActivateOptions();

                tracer.Layout = patternLayout;
                tracer.ActivateOptions();
                hierarchy.Root.AddAppender(tracer);

                RollingFileAppender roller = new RollingFileAppender();

                roller.Layout             = patternLayout;
                roller.AppendToFile       = true;
                roller.RollingStyle       = RollingFileAppender.RollingMode.Size;
                roller.MaxSizeRollBackups = 4;
                roller.MaximumFileSize    = "1MB";
                roller.StaticLogFileName  = true;
                roller.File = "WinScaper.log";
                roller.ActivateOptions();
                hierarchy.Root.AddAppender(roller);

                hierarchy.Root.Level = Level.All;
                hierarchy.Configured = true;
            }
Esempio n. 19
0
    public void InCode()
    {
        #region Log4NetInCode

        var layout = new PatternLayout
        {
            ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n"
        };
        layout.ActivateOptions();
        var consoleAppender = new ColoredConsoleAppender
        {
            Threshold = Level.Debug,
            Layout    = layout
        };
        consoleAppender.ActivateOptions();
        var appender = new RollingFileAppender
        {
            DatePattern        = "yyyy-MM-dd'.txt'",
            RollingStyle       = RollingFileAppender.RollingMode.Composite,
            MaxFileSize        = 10 * 1024 * 1024,
            MaxSizeRollBackups = 10,
            LockingModel       = new FileAppender.MinimalLock(),
            StaticLogFileName  = false,
            File         = @"nsb_log_",
            Layout       = layout,
            AppendToFile = true,
            Threshold    = Level.Debug,
        };
        appender.ActivateOptions();

        BasicConfigurator.Configure(appender, consoleAppender);
        Log4NetConfigurator.Configure();

        #endregion
    }
Esempio n. 20
0
        /// <summary>
        /// Creates new logger
        /// </summary>
        public Logger()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            PatternLayout patternLayout = new PatternLayout("[%-5level] [%date] %message%newline");

            patternLayout.ActivateOptions();

            RollingFileAppender fileAppender = new RollingFileAppender()
            {
                AppendToFile       = true,
                File               = LOG_PATH,
                Layout             = patternLayout,
                MaxSizeRollBackups = 5,
                MaximumFileSize    = "1GB",
                RollingStyle       = RollingFileAppender.RollingMode.Size,
                StaticLogFileName  = true
            };

            fileAppender.ActivateOptions();

            hierarchy.Root.AddAppender(fileAppender);
            hierarchy.Root.Level = Level.Debug;

            BasicConfigurator.Configure(hierarchy);

            logger = LogManager.GetLogger("Logger");

            Trace.Listeners.Add(this);
        }
Esempio n. 21
0
        public static void DebugConfig()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            hierarchy.Root.RemoveAllAppenders();
            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = "%date|||[%thread]|||%-5level|||%property{method_name}|||%property{type}|||%property{model}|||%message%newline";
            patternLayout.ActivateOptions();
            RollingFileAppender roller = new RollingFileAppender();

            roller.AppendToFile                 = true;
            roller.LockingModel                 = new FileAppender.MinimalLock();
            roller.File                         = @"logs\";
            roller.Layout                       = patternLayout;
            roller.MaxSizeRollBackups           = 5;
            roller.MaximumFileSize              = "2048KB";
            roller.DatePattern                  = "dd.MM.yyyy'.log'";
            roller.RollingStyle                 = RollingFileAppender.RollingMode.Composite;
            roller.StaticLogFileName            = false;
            roller.PreserveLogFileNameExtension = true;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);
            MemoryAppender memory = new MemoryAppender();

            memory.ActivateOptions();
            hierarchy.Root.AddAppender(memory);
            hierarchy.Root.Level = Level.Debug;
            hierarchy.Configured = true;
        }
Esempio n. 22
0
        public static void SetAsyncLogging()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = @"%date{ISO8601} [%-5level] [%2thread] %logger{1} - %message%newline%exception";
            patternLayout.ActivateOptions();

            RollingFileAppender roller = new RollingFileAppender();

            roller.AppendToFile       = false;
            roller.File               = @"log4net\log.txt";
            roller.Layout             = patternLayout;
            roller.MaxSizeRollBackups = 5;
            roller.MaximumFileSize    = "1000MB";
            roller.RollingStyle       = RollingFileAppender.RollingMode.Composite;
            roller.StaticLogFileName  = true;
            roller.ActivateOptions();

            AsyncBufferingForwardingAppender asyncBufferingForwarder = new AsyncBufferingForwardingAppender();

            asyncBufferingForwarder.AddAppender(roller);
            asyncBufferingForwarder.BufferSize = 512;
            asyncBufferingForwarder.Lossy      = false;



            hierarchy.Root.AddAppender(asyncBufferingForwarder);
            hierarchy.Root.Level = Level.All;
            hierarchy.Configured = true;
        }
Esempio n. 23
0
        public static void SetupFileAppender(string fileName)
        {
            var patternLayout = new PatternLayout
            {
                ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"
            };

            patternLayout.ActivateOptions();

            var fileAppender = new RollingFileAppender
            {
                AppendToFile       = false,
                File               = fileName,
                Layout             = patternLayout,
                MaxSizeRollBackups = 20,
                MaximumFileSize    = "1GB",
                RollingStyle       = RollingFileAppender.RollingMode.Size,
                StaticLogFileName  = false
            };

            fileAppender.ActivateOptions();

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

            hierarchy.Root.AddAppender(fileAppender);
            hierarchy.Configured = true;
        }
        public override void Configure(Assembly sourceAssembly, ILoggerRepository targetRepository)
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
            patternLayout.ActivateOptions();

            RollingFileAppender roller = new RollingFileAppender();

            roller.AppendToFile       = false;
            roller.File               = ConfigurationManager.AppSettings["LogFileLocation"];
            roller.Layout             = patternLayout;
            roller.MaxSizeRollBackups = 5;
            roller.MaximumFileSize    = "1GB";
            roller.RollingStyle       = RollingFileAppender.RollingMode.Size;
            roller.StaticLogFileName  = true;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);

            MemoryAppender memory = new MemoryAppender();

            memory.ActivateOptions();
            hierarchy.Root.AddAppender(memory);

            hierarchy.Root.Level = Level.Info;
            hierarchy.Configured = true;
        }
Esempio n. 25
0
        private static ILog GetLogConfig(string fileName)
        {
            var repository = LogManager.GetRepository();

            repository.ResetConfiguration();
            Hierarchy     hierarchy     = (Hierarchy)LogManager.GetRepository();
            PatternLayout patternLayout = new PatternLayout();

            patternLayout.ConversionPattern = "%message%newline";
            patternLayout.ActivateOptions();
            RollingFileAppender roller = new RollingFileAppender();

            roller.AppendToFile       = true;
            roller.File               = "TXT/" + fileName + ".TXT";
            roller.Layout             = patternLayout;
            roller.MaxSizeRollBackups = 20;
            roller.MaximumFileSize    = "10MB";
            roller.RollingStyle       = RollingFileAppender.RollingMode.Date;
            roller.Encoding           = Encoding.UTF8;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);
            hierarchy.Root.Level = Level.Info;
            hierarchy.Configured = true;
            return(LogManager.GetLogger(fileName));
        }
        public static void Configure(string appfolder, string logFile)
        {
            bool isConfigured = _log.Logger.Repository.Configured;

            if (!isConfigured)
            {
                // Setup RollingFileAppender
                var fileAppender = new RollingFileAppender
                {
                    Layout =
                        new PatternLayout(
                            "%d [%t]%-5p %c [%x] - %m%n"),
                    MaximumFileSize    = "1000KB",
                    MaxSizeRollBackups = 5,
                    RollingStyle       = RollingFileAppender.RollingMode.Size,
                    AppendToFile       = true,
                    File           = logFile,
                    ImmediateFlush = true,
                    LockingModel   = new FileAppender.MinimalLock(),
                    Name           = "XXXRollingFileAppender"
                };
                fileAppender.ActivateOptions(); // IMPORTANT, creates the file
                BasicConfigurator.Configure(fileAppender);
#if DEBUG
                // Setup TraceAppender
                TraceAppender ta = new TraceAppender();
                ta.Layout = new PatternLayout("%d [%t]%-5p %c [%x] - %m%n");
                BasicConfigurator.Configure(ta);
#endif
            }
        }
Esempio n. 27
0
        static Logger()
        {
            const string filename = "log4net.config";
            string       basePath = HttpContext.Current != null
                ? AppDomain.CurrentDomain.SetupInformation.PrivateBinPath
                : AppDomain.CurrentDomain.BaseDirectory;
            string configFile = Path.Combine(basePath, filename);

            if (File.Exists(configFile))
            {
                XmlConfigurator.ConfigureAndWatch(new FileInfo(configFile));
                return;
            }

            //默认设置
            RollingFileAppender appender = new RollingFileAppender
            {
                Name               = "root",
                File               = "logs\\log",
                AppendToFile       = true,
                RollingStyle       = RollingFileAppender.RollingMode.Composite,
                DatePattern        = "yyyyMMdd\".log\"",
                MaxSizeRollBackups = 10
            };

            PatternLayout layout = new PatternLayout("[%d{yyyy-MM-dd HH:mm:ss.fff}] %c.%M %t %n%m%n");

            appender.Layout = layout;
            BasicConfigurator.Configure(appender);
            appender.ActivateOptions();
        }
Esempio n. 28
0
        public static IAppender CreateFileAppender()
        {
            RollingFileAppender appender = new RollingFileAppender
            {
                Name                         = "RollingFileAppender",
                File                         = _logPath + "log.txt",
                AppendToFile                 = true,
                RollingStyle                 = RollingFileAppender.RollingMode.Size,
                MaxSizeRollBackups           = 1,
                MaxFileSize                  = ConfigManager.GeneralConfig.LogMaxFileSize,
                PreserveLogFileNameExtension = true,
                Encoding                     = System.Text.Encoding.Unicode
            };

            PatternLayout layout = new PatternLayout
            {
                ConversionPattern = "[%date{yyyy-MM-dd HH:mm:ss}] [%level] %message%newline"
            };

            layout.ActivateOptions();

            appender.Layout = layout;
            appender.ActivateOptions();

            return(appender);
        }
        public static void Setup()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

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

            patternLayout.ActivateOptions();

            var roller = new RollingFileAppender
            {
                AppendToFile       = true,
                File               = Paths.GitClientLogFilePath,
                Layout             = patternLayout,
                MaxSizeRollBackups = 10,
                MaximumFileSize    = "1GB",
                RollingStyle       = RollingFileAppender.RollingMode.Size,
                StaticLogFileName  = true
            };

            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);

            hierarchy.Root.Level = Level.All;
            hierarchy.Configured = true;
        }
Esempio n. 30
0
        /// <summary>
        ///     Creates the appender.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="file">The file.</param>
        /// <param name="logLevel">The log level.</param>
        /// <returns>
        ///     Returns a <see cref="IAppender" /> representing the appender.
        /// </returns>
        protected override FileAppender CreateAppender(string name, string file, LogLevel logLevel)
        {
            PatternLayout layout = new PatternLayout("%date{yyyy-MM-dd hh:mm:ss tt} - [%level]: %message%newline%exception");

            LevelMatchFilter filter = new LevelMatchFilter();

            filter.LevelToMatch = logLevel.ToLevel();
            filter.ActivateOptions();

            var ext = Path.GetExtension(file);

            var appender = new RollingFileAppender();

            appender.Name              = name;
            appender.File              = !string.IsNullOrEmpty(ext) ? file.Replace(ext, "") : file;
            appender.ImmediateFlush    = true;
            appender.RollingStyle      = RollingFileAppender.RollingMode.Date;
            appender.StaticLogFileName = false;
            appender.DatePattern       = "_yyyy-MM-dd'.log'";
            appender.AppendToFile      = true;
            appender.LockingModel      = new FileAppender.MinimalLock();
            appender.AddFilter(filter);
            appender.Layout = layout;
            appender.ActivateOptions();

            return(appender);
        }
    public static void ConfigureLog4Net()
    {
        var layout = new PatternLayout
        {
            ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n"
        };
        layout.ActivateOptions();
        var consoleAppender = new ColoredConsoleAppender
        {
            Threshold = Level.Debug,
            Layout = layout
        };
        consoleAppender.ActivateOptions();
        var appender = new RollingFileAppender
        {
            DatePattern = "yyyy-MM-dd'.txt'",
            RollingStyle = RollingFileAppender.RollingMode.Composite,
            MaxFileSize = 10 * 1024 * 1024,
            MaxSizeRollBackups = 10,
            LockingModel = new FileAppender.MinimalLock(),
            StaticLogFileName = false,
            File =  @"nsb_log_",
            Layout = layout,
            AppendToFile = true,
            Threshold = Level.Debug,
        };
        appender.ActivateOptions();

        BasicConfigurator.Configure(appender, consoleAppender);
    }
Esempio n. 32
0
  private void ConfigureAppenders ()
  {
    PatternLayout layout = new PatternLayout();
    layout.ConversionPattern = "%date [%thread] %-5level %logger - %message%n";
    layout.ActivateOptions();

    ValionLogAppender logAppender = new ValionLogAppender ();
    logAppender.MaximumEntries = 600;
    logAppender.Threshold = Level.Debug;
    logAppender.Layout = layout;
    logAppender.ActivateOptions();
    
    OSType osType = GetOperatingSystem ();
    if (osType == OSType.WebPlayer)
    {
      UnityEngine.Debug.Log ("Detected Web-Player, only using appended logger");
      BasicConfigurator.Configure(logAppender);
      return;
    }

    ConsoleAppender consoleAppener = new ConsoleAppender();
    consoleAppener.Layout = layout;
    consoleAppener.Threshold = Level.Debug;
    consoleAppener.Target = ConsoleAppender.ConsoleOut; 
    consoleAppener.ActivateOptions();

#if UNITY_WEBPLAYER
    BasicConfigurator.Configure (logAppender, consoleAppener);
#else
    PatternLayout fileLayout = new PatternLayout ();
    fileLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%n";
    fileLayout.ActivateOptions ();

    RollingFileAppender fileAppender = new RollingFileAppender ();
    fileAppender.AppendToFile = true;
    fileAppender.MaxFileSize = 2048 * 1024;
    fileAppender.MaxSizeRollBackups = 10;
    fileAppender.RollingStyle = RollingFileAppender.RollingMode.Composite;
    fileAppender.Threshold = Level.Debug;
    fileAppender.File = GetLogDirectory(osType) + "/log.txt";
    fileAppender.Layout = fileLayout;
    fileAppender.PreserveLogFileNameExtension = true;
    fileAppender.ImmediateFlush = true;
    fileAppender.ActivateOptions();

    UnityEngine.Debug.Log ("Detected " + osType + ", using file Logging: " + fileAppender.File);
    BasicConfigurator.Configure (fileAppender, logAppender, consoleAppener);
#endif
  }