static void Main()
        {
            SimpleLayout layout = new SimpleLayout();

            layout.ActivateOptions();

            ConsoleAppender appender = new ConsoleAppender();

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

            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
            Logger    root      = hierarchy.Root;

            root.Level = log4net.Core.Level.All;

            BasicConfigurator.Configure(appender);

            ILog log = LogManager.GetLogger(typeof(Program));

            log.Debug("Debug");
            log.Info("Info");
            log.Warn("Warn");
            log.Error("Error");
            log.Fatal("Fatal");

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        private static void LoggingWithCodeConfiguration()
        {
            SimpleLayout layout = new SimpleLayout();

            layout.ActivateOptions();

            ConsoleAppender appender = new ConsoleAppender();

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

            FileAppender appender2 = new FileAppender();

            appender2.Layout = layout;
            appender2.File   = "FileLog2.txt";
            appender2.ActivateOptions();

            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
            Logger    root      = hierarchy.Root;

            root.Level = Level.All;

            BasicConfigurator.Configure(new IAppender[] { appender, appender2 });

            ILog log = LogManager.GetLogger(typeof(Program));

            log.Debug("debug");
            log.Warn("warn");
            log.Error("");
            log.Fatal("fatality");
            log.Info("Hello from log4net!");

            Console.ReadLine();
        }
        private static void ConfigureLog4netInCode()
        {
            // log4net
            // https://www.nuget.org/packages/log4net/
            // Install-Package -Id log4net -ProjectName 5_ConfigureInCode
            var layout = new SimpleLayout();

            layout.ActivateOptions();

            var appender = new ConsoleAppender();

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

            var    hierarchy = (Hierarchy)LogManager.GetRepository(Assembly.GetEntryAssembly());
            Logger root      = hierarchy.Root;

            root.Level = Level.All;

            BasicConfigurator.Configure(hierarchy, appender);
        }
Ejemplo n.º 4
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);
        }