Exemple #1
0
        private static void ConfigureLoggingByCode()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            PatternLayout patternLayout = new PatternLayout();
            patternLayout.ConversionPattern = "%date [%thread] %-5level %logger [%.30location| Method = %method] - %message%newline";
            patternLayout.ActivateOptions();

            ColoredConsoleAppender c = new ColoredConsoleAppender();
            ColoredConsoleAppender.LevelColors col = new ColoredConsoleAppender.LevelColors();
            col.Level = log4net.Core.Level.Error;
            col.ForeColor = ColoredConsoleAppender.Colors.White;
            col.BackColor = ColoredConsoleAppender.Colors.Red;

            c.Layout = patternLayout;
            c.AddMapping(col);
            c.ActivateOptions();
            hierarchy.Root.AddAppender(c);

            GelfTcpAppender remoteAppender = new GelfTcpAppender();
            remoteAppender.Layout = patternLayout;
            remoteAppender.AdditionalFields = "app:AllInOne,version:0.0.6.6,environment:STAGE";
            remoteAppender.Facility = "AllInOne";
            remoteAppender.RemoteAddress = "logs.expertus.local";
            remoteAppender.RemotePort = 12201;
            remoteAppender.ActivateOptions();
            hierarchy.Root.AddAppender(remoteAppender);

            hierarchy.Root.Level = log4net.Core.Level.All;
            hierarchy.Configured = true;
        }
Exemple #2
0
        public static void Start(LogTargets target) //server remote mode will be set auto when use SetRemote
        {
            type = target;
            List<IAppender> appenders = new List<IAppender>();
            if ((type & LogTargets.ServerRemote) != 0 && remoteIP != "")
            {
                UdpAppender appender = new UdpAppender();
                appender.Layout = new PatternLayout(format);
                appender.RemoteAddress = IPAddress.Parse(remoteIP);
                appender.RemotePort = remotePort;
                LevelRangeFilter filter = new LevelRangeFilter();
                filter.LevelMin = Level.Debug;
                filter.LevelMax = Level.Fatal;
                appender.AddFilter(filter);
                appender.ActivateOptions();
                appenders.Add(appender);
            }

            if ((type & LogTargets.ServerConsole) != 0)
            {
                ColoredConsoleAppender appender = new ColoredConsoleAppender();
                appender.Layout = new PatternLayout(format);
                ColoredConsoleAppender.LevelColors mapcolor = new ColoredConsoleAppender.LevelColors();
                mapcolor.Level = Level.Fatal;
                mapcolor.BackColor = ColoredConsoleAppender.Colors.Red;
                appender.AddMapping(mapcolor);
                mapcolor = new ColoredConsoleAppender.LevelColors();
                mapcolor.Level = Level.Error;
                mapcolor.BackColor = ColoredConsoleAppender.Colors.Red;
                appender.AddMapping(mapcolor);
                mapcolor = new ColoredConsoleAppender.LevelColors();
                mapcolor.Level = Level.Warn;
                mapcolor.ForeColor = ColoredConsoleAppender.Colors.Purple;
                appender.AddMapping(mapcolor);
                mapcolor = new ColoredConsoleAppender.LevelColors();
                mapcolor.Level = Level.Info;
                mapcolor.ForeColor = ColoredConsoleAppender.Colors.Green;
                appender.AddMapping(mapcolor);
                appender.ActivateOptions();
                appenders.Add(appender);
            }

            if ((type & LogTargets.File) != 0)
            {
                FileAppender appender = new FileAppender();
                appender.Layout = new PatternLayout(format);
                appender.File = string.Format("Log/{0}-{1}-{2}.log", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                appender.AppendToFile = true;
                appender.Name = "FileAppender";
                appender.ActivateOptions();
                appenders.Add(appender);
            }

            BasicConfigurator.Configure(appenders.ToArray());
        }
Exemple #3
0
        public void BasicConfig()
        {
            PatternLayout layout = new PatternLayout();
            layout.ConversionPattern = "%date [%thread] %-5level %C{2} %M - %message%newline";
            layout.ActivateOptions();

            //the ColoredConsoleAppender is not available in mono so use a standard console appender
            #if __MonoCS__
            log4net.Appender.ConsoleAppender appender = new log4net.Appender.ConsoleAppender();
            appender.Layout = layout;
            appender.ActivateOptions();

            BasicConfigurator.Configure(appender);
            #else
            log4net.Appender.ColoredConsoleAppender appender = new log4net.Appender.ColoredConsoleAppender();
            appender.Layout = layout;

            ColoredConsoleAppender.LevelColors errorMapping = new ColoredConsoleAppender.LevelColors();
            errorMapping.BackColor = ColoredConsoleAppender.Colors.HighIntensity | ColoredConsoleAppender.Colors.Red;
            errorMapping.ForeColor = ColoredConsoleAppender.Colors.White;
            errorMapping.Level = Level.Error;
            ColoredConsoleAppender.LevelColors infoMapping = new ColoredConsoleAppender.LevelColors();
            infoMapping.ForeColor = ColoredConsoleAppender.Colors.Green;
            infoMapping.Level = Level.Info;
            ColoredConsoleAppender.LevelColors debugMapping = new ColoredConsoleAppender.LevelColors();
            debugMapping.ForeColor = ColoredConsoleAppender.Colors.Blue;
            debugMapping.Level = Level.Debug;
            appender.AddMapping(errorMapping);
            appender.AddMapping(infoMapping);
            appender.AddMapping(debugMapping);

            appender.ActivateOptions();

            BasicConfigurator.Configure(appender);
            #endif
        }
    ColoredConsoleAppender.LevelColors[] GetDefaultLevelsColors() {
      // Define the default color mapping for the levels that have lower
      // priority than the the ERROR log. The level color mapping that will
      // be used to log a message will be the nearest mapping value for the
      // level that is equal to os less than the level of the message. So, in
      // order to log all messages to a specific color scheme we only need to
      // set the level of the lowest level in logger level hierarchy, that is
      // the lower that the level that is explicit specified, that is the
      // level of [ERROR].
      Level[] levels = new Level[] {
        Level.Warn, Level.Notice, Level.Info, Level.Debug, Level.Fine,
        Level.Trace, Level.Finer, Level.Verbose, Level.Finest, Level.All
      };

      int levels_length = levels.Length;
      ColoredConsoleAppender.LevelColors[] levels_colors =
        new ColoredConsoleAppender.LevelColors[levels_length + 1];

      for (int i = 0; i < levels_length; i++) {
        ColoredConsoleAppender.LevelColors level_colors =
          new ColoredConsoleAppender.LevelColors();

        level_colors.ForeColor = ColoredConsoleAppender.Colors.Green;
        level_colors.Level = levels[i];
        levels_colors[i] = level_colors;
      }

      // define the default color mapping for the ERROR level.
      levels_colors[levels_length] = new ColoredConsoleAppender.LevelColors();
      levels_colors[levels_length].Level = Level.Error;
      levels_colors[levels_length].ForeColor =
        ColoredConsoleAppender.Colors.Red;

      return levels_colors;
    }