Ejemplo n.º 1
0
        private static void EnableConsoleDebugLog()
        {
            log4net.Appender.IAppender appender;
            var consoleAppender = new log4net.Appender.ColoredConsoleAppender
                {
                    Layout = new SimpleLayout(),
                    Threshold = Level.All
                };
            try
            {
                consoleAppender.ActivateOptions();
                appender = consoleAppender;
            }
            catch (EntryPointNotFoundException)
            {
                var fallbackAppender = new log4net.Appender.ConsoleAppender
                {
                    Layout = new SimpleLayout(),
                    Threshold = Level.All
                };

                fallbackAppender.ActivateOptions();
                appender = fallbackAppender;
            }

            var repo = (Hierarchy)log4net.LogManager.GetRepository();
            var root = repo.Root;
            root.AddAppender(appender);

            repo.Configured = true;

            log.Info("Verbose logging to console enabled.");
        }
Ejemplo n.º 2
0
        // <appender name="console" type="log4net.Appender.ColoredConsoleAppender">
        //      <mapping>
        //        <level value="ERROR" />
        //        <foreColor value="White" />
        //        <backColor value="Red, HighIntensity" />
        //      </mapping>
        //      <mapping>
        //        <level value="WARN" />
        //        <foreColor value="Yellow, HighIntensity" />
        //      </mapping>
        //      <mapping>
        //        <level value="INFO" />
        //        <foreColor value="Green" />
        //      </mapping>
        //      <layout type="log4net.Layout.PatternLayout">
        //        <conversionPattern value="%date %newline%message%newline%newline" />
        //      </layout>
        //  </appender>
        public static void AddConsoleAppender(this log4net.Repository.ILoggerRepository loggerRepo)
        {
            var appender = new log4net.Appender.ColoredConsoleAppender();
            appender.Name = "console";
            appender.Layout = new log4net.Layout.PatternLayout("%date %newline%message%newline%newline");

            var errorMapping = new log4net.Appender.ColoredConsoleAppender.LevelColors();
            errorMapping.ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.White;
            errorMapping.BackColor = log4net.Appender.ColoredConsoleAppender.Colors.Red & log4net.Appender.ColoredConsoleAppender.Colors.HighIntensity;
            errorMapping.Level = log4net.Core.Level.Error;
            appender.AddMapping(errorMapping);

            var warnMapping = new log4net.Appender.ColoredConsoleAppender.LevelColors();
            warnMapping.BackColor = log4net.Appender.ColoredConsoleAppender.Colors.Yellow & log4net.Appender.ColoredConsoleAppender.Colors.HighIntensity;
            warnMapping.Level = log4net.Core.Level.Warn;
            appender.AddMapping(warnMapping);

            var infoMapping = new log4net.Appender.ColoredConsoleAppender.LevelColors();
            infoMapping.BackColor = log4net.Appender.ColoredConsoleAppender.Colors.Green;
            infoMapping.Level = log4net.Core.Level.Info;
            appender.AddMapping(infoMapping);

            appender.ActivateOptions();

            loggerRepo.AddAppender(appender);
        }
Ejemplo n.º 3
0
        private static void EnableConsoleDebugLog()
        {
            var consoleAppender = new log4net.Appender.ColoredConsoleAppender
            {
                Layout    = new SimpleLayout(),
                Threshold = Level.All
            };

            consoleAppender.ActivateOptions();

            var repo = (Hierarchy)log4net.LogManager.GetRepository();
            var root = repo.Root;

            root.AddAppender(consoleAppender);

            repo.Configured = true;

            log.Info("Verbose logging to console enabled.");
        }
Ejemplo n.º 4
0
        private static ILog getDefaultLogger(String name)
        {
            ILog r = LogManager.GetCurrentLoggers().SingleOrDefault(x => x.Logger.Name == name);

            if (r != null)
            {
                return(r);
            }

            Hierarchy hierarchy = (Hierarchy)r;

            log4net.Appender.ColoredConsoleAppender appender = new
                                                               log4net.Appender.ColoredConsoleAppender();
            appender.Name = name;

            log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout();
            layout.ConversionPattern = "%-4timestamp [%thread] %-5level %logger %ndc - %message%newline";
            layout.ActivateOptions();

            appender.Layout = layout;

            log4net.Filter.LevelRangeFilter filter = new log4net.Filter.LevelRangeFilter();
            filter.LevelMax = log4net.Core.Level.Fatal;
            filter.LevelMin = log4net.Core.Level.Debug;

            appender.AddFilter(filter);
            appender.ActivateOptions();

            hierarchy.Root.AddAppender(appender);

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

            DummyLogger dummyILogger = new DummyLogger(name);

            dummyILogger.Hierarchy = hierarchy;
            dummyILogger.Level     = log4net.Core.Level.All;

            dummyILogger.AddAppender(appender);

            return(new log4net.Core.LogImpl(dummyILogger));;
        }
        /// <summary>
        /// See example at this place: http://aaubry.net/configuring-log4net-coloredconsoleappender-in-code.html
        /// </summary>
        /// <param name="threshold"></param>
        /// <param name="logPattern"></param>
        public static void AddColoredConsoleAppender(log4net.Core.Level threshold = null,
                                                     string logPattern            = CodeConfiguredUtilities.DefaultLogPattern)
        {
            var repo = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository();

            var coloredConsoleAppender = new log4net.Appender.ColoredConsoleAppender
            {
                Threshold = (threshold == null) ? log4net.Core.Level.All : threshold,
                Layout    = new log4net.Layout.PatternLayout(logPattern)
            };

            coloredConsoleAppender.AddMapping(new log4net.Appender.ColoredConsoleAppender.LevelColors
            {
                Level     = log4net.Core.Level.Info,
                ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.White | log4net.Appender.ColoredConsoleAppender.Colors.HighIntensity
            });

            coloredConsoleAppender.AddMapping(new log4net.Appender.ColoredConsoleAppender.LevelColors
            {
                Level     = log4net.Core.Level.Debug,
                ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.White | log4net.Appender.ColoredConsoleAppender.Colors.HighIntensity,
                BackColor = log4net.Appender.ColoredConsoleAppender.Colors.Blue
            });

            coloredConsoleAppender.AddMapping(new log4net.Appender.ColoredConsoleAppender.LevelColors
            {
                Level     = log4net.Core.Level.Warn,
                ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.Yellow | log4net.Appender.ColoredConsoleAppender.Colors.HighIntensity,
                BackColor = log4net.Appender.ColoredConsoleAppender.Colors.Purple
            });

            coloredConsoleAppender.AddMapping(new log4net.Appender.ColoredConsoleAppender.LevelColors
            {
                Level     = log4net.Core.Level.Error,
                ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.Yellow | log4net.Appender.ColoredConsoleAppender.Colors.HighIntensity,
                BackColor = log4net.Appender.ColoredConsoleAppender.Colors.Red
            });


            coloredConsoleAppender.ActivateOptions();
            repo.Root.AddAppender(coloredConsoleAppender);
        }
Ejemplo n.º 6
0
        protected override void ProcessRecord()
        {
            //todo set default to scriptname.log
            string test = MyInvocation.ScriptName;

            log4net.ILog       logger      = log4net.LogManager.GetLogger("Powershell");
            System.IO.FileInfo logFileInfo = new System.IO.FileInfo(_logConfigFile);
            log4net.Config.XmlConfigurator.ConfigureAndWatch(logFileInfo);
            WriteObject(logger);

            log4net.Appender.IAppender[] appenders = log4net.LogManager.GetRepository().GetAppenders();
            foreach (log4net.Appender.IAppender logAppender in appenders)
            {
                #region RollingFilleAppender
                if ((logAppender.GetType() == typeof(log4net.Appender.RollingFileAppender)) && logAppender.Name == "OSCCDefaultRollingFileAppender")
                {
                    log4net.Appender.FileAppender fileAppender = (log4net.Appender.RollingFileAppender)logAppender;
                    if (MyInvocation.BoundParameters.ContainsKey("LogFileName"))
                    {
                        log4net.Util.PatternString dynamicFileName         = new log4net.Util.PatternString(LogFileName);
                        log4net.Util.ConverterInfo adminuiLogConverterInfo = new log4net.Util.ConverterInfo();
                        adminuiLogConverterInfo.Name = "adminuilog";
                        adminuiLogConverterInfo.Type = typeof(Log4Net_CMTrace.CMAdminUILogFolderPatternConverter);
                        log4net.Util.ConverterInfo ccmLogConverterInfo = new log4net.Util.ConverterInfo();
                        ccmLogConverterInfo.Name = "ccmlog";
                        ccmLogConverterInfo.Type = typeof(Log4Net_CMTrace.CMClientLogFolderPatternConverter);
                        log4net.Layout.PatternLayout newLayout = new log4net.Layout.PatternLayout();
                        dynamicFileName.AddConverter(adminuiLogConverterInfo);
                        dynamicFileName.AddConverter(ccmLogConverterInfo);
                        dynamicFileName.ActivateOptions();
                        //fileAppender.File = LogFileName;
                        fileAppender.File = dynamicFileName.Format();
                    }
                    else
                    {
                        if (MyInvocation.ScriptName != "")
                        {
                            fileAppender.File = MyInvocation.ScriptName + ".log";
                        }
                    }
                    if (MyInvocation.BoundParameters.ContainsKey("FileLogLevel"))
                    {
                        switch (FileLogLevel.ToLower())
                        {
                        case "off": fileAppender.Threshold = log4net.Core.Level.Off; break;

                        case "info": fileAppender.Threshold = log4net.Core.Level.Info; break;

                        case "warn": fileAppender.Threshold = log4net.Core.Level.Warn; break;

                        case "error": fileAppender.Threshold = log4net.Core.Level.Error; break;

                        case "debug": fileAppender.Threshold = log4net.Core.Level.Debug; break;

                        default: fileAppender.Threshold = log4net.Core.Level.Info; break;
                        }
                    }
                    else
                    {
                        fileAppender.Threshold = log4net.Core.Level.Info;
                    }
                    fileAppender.ActivateOptions();
                }
                #endregion

                #region OMSFilleAppender
                if ((logAppender.GetType() == typeof(log4net.Appender.FileAppender)) && logAppender.Name == "OSCCOMSFileAppender")
                {
                    log4net.Appender.FileAppender fileAppender = (log4net.Appender.FileAppender)logAppender;
                    if (MyInvocation.BoundParameters.ContainsKey("OMSLogFileName"))
                    {
                        log4net.Util.PatternString dynamicFileName         = new log4net.Util.PatternString(LogFileName);
                        log4net.Util.ConverterInfo adminuiLogConverterInfo = new log4net.Util.ConverterInfo();
                        adminuiLogConverterInfo.Name = "adminuilog";
                        adminuiLogConverterInfo.Type = typeof(Log4Net_CMTrace.CMAdminUILogFolderPatternConverter);
                        log4net.Util.ConverterInfo ccmLogConverterInfo = new log4net.Util.ConverterInfo();
                        ccmLogConverterInfo.Name = "ccmlog";
                        ccmLogConverterInfo.Type = typeof(Log4Net_CMTrace.CMClientLogFolderPatternConverter);
                        log4net.Layout.PatternLayout newLayout = new log4net.Layout.PatternLayout();
                        dynamicFileName.AddConverter(adminuiLogConverterInfo);
                        dynamicFileName.AddConverter(ccmLogConverterInfo);
                        dynamicFileName.ActivateOptions();
                        //fileAppender.File = LogFileName;
                        fileAppender.File = dynamicFileName.Format();
                    }
                    else
                    {
                        if (MyInvocation.ScriptName != "")
                        {
                            fileAppender.File = MyInvocation.ScriptName + ".OMS." + DateTime.Today.ToString("yyyyMMdd") + ".log";
                        }
                    }
                    if (MyInvocation.BoundParameters.ContainsKey("OMSFileLogLevel"))
                    {
                        switch (FileLogLevel.ToLower())
                        {
                        case "off": fileAppender.Threshold = log4net.Core.Level.Off; break;

                        case "info": fileAppender.Threshold = log4net.Core.Level.Info; break;

                        case "warn": fileAppender.Threshold = log4net.Core.Level.Warn; break;

                        case "error": fileAppender.Threshold = log4net.Core.Level.Error; break;

                        case "debug": fileAppender.Threshold = log4net.Core.Level.Debug; break;

                        default: fileAppender.Threshold = log4net.Core.Level.Info; break;
                        }
                    }
                    fileAppender.ActivateOptions();
                }
                #endregion
                #region ConsoleAppender
                if (logAppender.GetType() == typeof(log4net.Appender.ConsoleAppender))
                {
                    log4net.Appender.ConsoleAppender consoleAppender = (log4net.Appender.ConsoleAppender)logAppender;
                    if (this.Host.Name.ToLower() == "consolehost")
                    {
                        if (MultiColor == false)
                        {
                            {
                                if (MyInvocation.BoundParameters.ContainsKey("ConsoleLogLevel"))
                                {
                                    switch (ConsoleLogLevel.ToLower())
                                    {
                                    case "off": consoleAppender.Threshold = log4net.Core.Level.Off; break;

                                    case "info": consoleAppender.Threshold = log4net.Core.Level.Info; break;

                                    case "warn": consoleAppender.Threshold = log4net.Core.Level.Warn; break;

                                    case "error": consoleAppender.Threshold = log4net.Core.Level.Error; break;

                                    case "debug": consoleAppender.Threshold = log4net.Core.Level.Debug; break;

                                    default: consoleAppender.Threshold = log4net.Core.Level.Info; break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            consoleAppender.Threshold = log4net.Core.Level.Off;
                        }
                    }
                    else
                    {
                        if (this.Host.Name.ToLower() == "windows powershell ise host")
                        {
                            if (MyInvocation.BoundParameters.ContainsKey("ConsoleLogLevel"))
                            {
                                switch (ConsoleLogLevel.ToLower())
                                {
                                case "off": consoleAppender.Threshold = log4net.Core.Level.Off; break;

                                case "info": consoleAppender.Threshold = log4net.Core.Level.Info; break;

                                case "warn": consoleAppender.Threshold = log4net.Core.Level.Warn; break;

                                case "error": consoleAppender.Threshold = log4net.Core.Level.Error; break;

                                case "debug": consoleAppender.Threshold = log4net.Core.Level.Debug; break;

                                default: consoleAppender.Threshold = log4net.Core.Level.Info; break;
                                }
                            }
                            if ((MyInvocation.BoundParameters.ContainsKey("MultiColor")) && (MultiColor == true))
                            {
                                Console.WriteLine("Powershell ISe does not support multi colored displays.");
                            }
                        }
                    }
                    consoleAppender.ActivateOptions();
                }
                #endregion
                #region coloredconsoleappender
                if (logAppender.GetType() == typeof(log4net.Appender.ColoredConsoleAppender))
                {
                    log4net.Appender.ColoredConsoleAppender coloredConsoleAppender = (log4net.Appender.ColoredConsoleAppender)logAppender;
                    if (this.Host.Name.ToLower() == "consolehost")
                    {
                        if (MultiColor == true)
                        {
                            if (MyInvocation.BoundParameters.ContainsKey("ConsoleLogLevel"))
                            {
                                switch (ConsoleLogLevel.ToLower())
                                {
                                case "off": coloredConsoleAppender.Threshold = log4net.Core.Level.Off; break;

                                case "info": coloredConsoleAppender.Threshold = log4net.Core.Level.Info; break;

                                case "warn": coloredConsoleAppender.Threshold = log4net.Core.Level.Warn; break;

                                case "error": coloredConsoleAppender.Threshold = log4net.Core.Level.Error; break;

                                case "debug": coloredConsoleAppender.Threshold = log4net.Core.Level.Debug; break;

                                default: coloredConsoleAppender.Threshold = log4net.Core.Level.Info; break;
                                }
                            }
                        }
                        else
                        {
                            coloredConsoleAppender.Threshold = log4net.Core.Level.Off;
                        }
                    }
                    if (this.Host.Name.ToLower() == "windows powershell ise host")
                    {
                        coloredConsoleAppender.Threshold = log4net.Core.Level.Off;
                    }
                    coloredConsoleAppender.ActivateOptions();
                }
                #endregion
            }
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            bool coloredLogging = true;
            bool printHelp = false;
            bool printVersion = false;

            // Name the main thread
            Thread.CurrentThread.Name = "Main";

            #region Command Line Argument Handling

            Mono.Options.OptionSet set = new Mono.Options.OptionSet()
            {
                { "nocolor", "Disable colored console logging", v => coloredLogging = false },
                { "h|?|help", "Shows launch options", v => printHelp = true },
                { "version", "Show version information", v => printVersion = true }
            };
            set.Parse(args);

            if (printHelp)
            {
                set.WriteOptionDescriptions(Console.Out);
                return;
            }

            if (printVersion)
            {
                string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                Console.WriteLine("Simian " + version);
                return;
            }

            #endregion Command Line Argument Handling

            #region log4net Setup

            // If error level reporting isn't enabled we assume no logger is configured and initialize a default appender
            if (!m_log.Logger.IsEnabledFor(log4net.Core.Level.Error))
            {
                log4net.Appender.AppenderSkeleton appender;

                if (coloredLogging)
                {
                    log4net.Appender.ColoredConsoleAppender coloredAppender = new log4net.Appender.ColoredConsoleAppender();

                    var mapping = new log4net.Appender.ColoredConsoleAppender.LevelColors();
                    mapping.Level = log4net.Core.Level.Debug;
                    mapping.ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.HighIntensity;
                    coloredAppender.AddMapping(mapping);

                    mapping = new log4net.Appender.ColoredConsoleAppender.LevelColors();
                    mapping.Level = log4net.Core.Level.Info;
                    mapping.ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.White;
                    coloredAppender.AddMapping(mapping);

                    mapping = new log4net.Appender.ColoredConsoleAppender.LevelColors();
                    mapping.Level = log4net.Core.Level.Warn;
                    mapping.BackColor = log4net.Appender.ColoredConsoleAppender.Colors.Purple;
                    mapping.ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.White;
                    coloredAppender.AddMapping(mapping);

                    mapping = new log4net.Appender.ColoredConsoleAppender.LevelColors();
                    mapping.Level = log4net.Core.Level.Error;
                    mapping.BackColor = log4net.Appender.ColoredConsoleAppender.Colors.Red;
                    mapping.ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.White;
                    coloredAppender.AddMapping(mapping);

                    appender = coloredAppender;
                }
                else
                {
                    appender = new log4net.Appender.ConsoleAppender();
                }

                appender.Layout = new log4net.Layout.PatternLayout("%timestamp [%thread] %-5level %logger - %message%newline");
                appender.ActivateOptions();
                BasicConfigurator.Configure(appender);

                m_log.Info("No log configuration found, defaulting to console logging");
            }

            // Hook up Debug.Assert statements to log4net
            Debug.Listeners.Insert(0, new log4netTraceListener());

            #endregion log4net Setup

            // Set the working directory to the application dir
            Directory.SetCurrentDirectory(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));

            // Initialize the Simian object
            m_simian = new Simian();

            // Handle Ctrl+C
            Console.CancelKeyPress +=
                delegate(object sender, ConsoleCancelEventArgs e)
                {
                    e.Cancel = true;

                    m_simian.Shutdown();
                    m_running = false;
                };

            // Attempt to load modules
            if (m_simian.LoadModules())
            {
                // Initialize the interactive console
                InteractiveConsole();
            }
            else
            {
                m_log.Error("Application module loading failed, shutting down");
            }
        }
Ejemplo n.º 8
0
        private static void EnableConsoleDebugLog()
        {
            var consoleAppender = new log4net.Appender.ColoredConsoleAppender
                {
                    Layout = new SimpleLayout(),
                    Threshold = Level.All
                };
            consoleAppender.ActivateOptions();

            var repo = (Hierarchy)log4net.LogManager.GetRepository();
            var root = repo.Root;
            root.AddAppender(consoleAppender);

            repo.Configured = true;

            log.Info("Verbose logging to console enabled.");
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            bool coloredLogging = true;
            bool printHelp      = false;
            bool printVersion   = false;

            // Name the main thread
            Thread.CurrentThread.Name = "Main";

            #region Command Line Argument Handling

            Mono.Options.OptionSet set = new Mono.Options.OptionSet()
            {
                { "nocolor", "Disable colored console logging", v => coloredLogging = false },
                { "h|?|help", "Shows launch options", v => printHelp = true },
                { "version", "Show version information", v => printVersion = true }
            };
            set.Parse(args);

            if (printHelp)
            {
                set.WriteOptionDescriptions(Console.Out);
                return;
            }

            if (printVersion)
            {
                string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                Console.WriteLine("Simian " + version);
                return;
            }

            #endregion Command Line Argument Handling

            #region log4net Setup

            // If error level reporting isn't enabled we assume no logger is configured and initialize a default appender
            if (!m_log.Logger.IsEnabledFor(log4net.Core.Level.Error))
            {
                log4net.Appender.AppenderSkeleton appender;

                if (coloredLogging)
                {
                    log4net.Appender.ColoredConsoleAppender coloredAppender = new log4net.Appender.ColoredConsoleAppender();

                    var mapping = new log4net.Appender.ColoredConsoleAppender.LevelColors();
                    mapping.Level     = log4net.Core.Level.Debug;
                    mapping.ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.HighIntensity;
                    coloredAppender.AddMapping(mapping);

                    mapping           = new log4net.Appender.ColoredConsoleAppender.LevelColors();
                    mapping.Level     = log4net.Core.Level.Info;
                    mapping.ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.White;
                    coloredAppender.AddMapping(mapping);

                    mapping           = new log4net.Appender.ColoredConsoleAppender.LevelColors();
                    mapping.Level     = log4net.Core.Level.Warn;
                    mapping.BackColor = log4net.Appender.ColoredConsoleAppender.Colors.Purple;
                    mapping.ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.White;
                    coloredAppender.AddMapping(mapping);

                    mapping           = new log4net.Appender.ColoredConsoleAppender.LevelColors();
                    mapping.Level     = log4net.Core.Level.Error;
                    mapping.BackColor = log4net.Appender.ColoredConsoleAppender.Colors.Red;
                    mapping.ForeColor = log4net.Appender.ColoredConsoleAppender.Colors.White;
                    coloredAppender.AddMapping(mapping);

                    appender = coloredAppender;
                }
                else
                {
                    appender = new log4net.Appender.ConsoleAppender();
                }

                appender.Layout = new log4net.Layout.PatternLayout("%timestamp [%thread] %-5level %logger - %message%newline");
                appender.ActivateOptions();
                BasicConfigurator.Configure(appender);

                m_log.Info("No log configuration found, defaulting to console logging");
            }

            // Hook up Debug.Assert statements to log4net
            Debug.Listeners.Insert(0, new log4netTraceListener());

            #endregion log4net Setup

            // Set the working directory to the application dir
            Directory.SetCurrentDirectory(Util.ExecutingDirectory());

            // Initialize the Simian object
            m_simian = new Simian();

            // Handle Ctrl+C
            Console.CancelKeyPress +=
                delegate(object sender, ConsoleCancelEventArgs e)
            {
                e.Cancel = true;

                m_simian.Shutdown();
                m_running = false;
            };

            // Attempt to load modules
            if (m_simian.LoadModules())
            {
                // Initialize the interactive console
                InteractiveConsole();
            }
            else
            {
                m_log.Error("Application module loading failed, shutting down");
            }
        }