Пример #1
0
        private IAppender GetUDPAppender(int?localport = null, int?remoteport = null, string remote_address = null)
        {
            var appender = new log4net.Appender.UdpAppender();

            if (localport == null)
            {
                localport = 4440;
            }
            appender.LocalPort = (int)localport;
            if (remoteport == null)
            {
                remoteport = 4445;
            }
            appender.RemotePort = (int)remoteport;
            if (remote_address == null)
            {
                remote_address = "255.255.255.255";
            }
            // remote_address = "192.168.1.33";
            var ip = IPAddress.Parse(remote_address);

            appender.RemoteAddress = ip;
            var layout = new P8XmlLayoutSchemaLog4j();

            layout.ActivateOptions();
            appender.Layout = layout;
            appender.ActivateOptions();
            return(appender);
        }
Пример #2
0
        public static IAppender GetUDPAppender(int?localport = null, int?remoteport = null, string remote_address = null)
        {
            var appender = new log4net.Appender.UdpAppender();

            if (localport == null)
            {
                localport = 4444;
            }
            appender.LocalPort = (int)localport;
            if (remoteport == null)
            {
                remoteport = 4445;
            }
            appender.RemotePort = (int)remoteport;
            if (remote_address == null)
            {
                remote_address = "255.255.255.255";
            }
            remote_address = "10.8.0.4";
            var ip = IPAddress.Parse(remote_address);

            appender.RemoteAddress = ip;
            var layout = new XmlLayoutSchemaLog4j();

            layout.ActivateOptions();
            appender.Layout = layout;
            return(appender);
        }
Пример #3
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());
        }
Пример #4
0
        private static void ConfigureWithCode()
        {
            var udpAppender = new UdpAppender
            {
                RemoteAddress = Dns.GetHostAddresses("log4stuff.com").First(),
                RemotePort = 8080,
                Layout = new XmlLayoutSchemaLog4j()
            };
            udpAppender.ActivateOptions();

            BasicConfigurator.Configure(udpAppender);

            var applicationId = Guid.NewGuid();
            log4net.GlobalContext.Properties["ApplicationId"] = applicationId;
            System.Diagnostics.Process.Start("http://log4stuff.com/app/" + applicationId);
        }
Пример #5
0
        public static void AutoConfigureLogging(string applicationId, bool listenToTrace = true)
        {
            var udpAppender = new UdpAppender
            {
                RemoteAddress = Dns.GetHostAddresses("log4stuff.com").First(),
                RemotePort = 8080,
                Layout = new XmlLayoutSchemaLog4j()
            };
            udpAppender.ActivateOptions();
            BasicConfigurator.Configure(udpAppender);
            log4net.GlobalContext.Properties["ApplicationId"] = applicationId;

            if (listenToTrace)
            {
                Trace.Listeners.Add(new Log4NetTraceListener());
            }
        }
Пример #6
0
        private static void StartLogSimulator()
        {
            var traceAppender = new TraceAppender { Layout = new PatternLayout() };

            var udpAppender = new UdpAppender
            {
                RemoteAddress = IPAddress.Loopback,
                RemotePort = 8080,
                Name = "UDPAppender",
                Encoding = new ASCIIEncoding(),
                Threshold = Level.Debug,
                Layout = new XmlLayoutSchemaLog4j()
            };
            udpAppender.ActivateOptions();

            BasicConfigurator.Configure(traceAppender, udpAppender);

            var log = LogManager.GetLogger("Simulator");
            log4net.GlobalContext.Properties["ApplicationId"] = "log4stuff";

            var rand = new Random();
            SimulatorTimer = new Timer(state =>
            {
                var option = rand.Next(0, 3); //max is exclusive
                switch (option)
                {
                    case 0:
                        log.Info("This is an info message");
                        break;
                    case 1:
                        log.Debug("This is a really long message that will be truncated. It would never fit into a single row.");
                        break;
                    default:
                        log.Debug("This is a debug message");
                        break;
                }

            }, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));

        }