Log4net layout class with default support for the Syslog message format as described in Syslog IETF 5424 standard: http://tools.ietf.org/html/rfc5424
Inheritance: log4net.Layout.LayoutSkeleton
Beispiel #1
0
        public static void PrepareLog(string logRoot, string serviceName)
        {
            Hierarchy hier = log4net.LogManager.GetRepository() as Hierarchy;

            string logRootFullName = Path.Combine(logRoot,
                serviceName, "Starter");

            if (null != hier)
            {
                RollingFileAppender commonAppender = (RollingFileAppender)hier.GetAppenders().Where(a => a.Name.Equals("CommonAppender")).FirstOrDefault();

                if (null != commonAppender)
                {
                    commonAppender.File = Path.Combine(logRootFullName, "logs.txt");

                    var filter = new LogExceptionToFileFilter()
                    {
                        ExceptionLogFolder = Path.Combine(logRootFullName, "Exceptions")
                    };
                    commonAppender.AddFilter(filter);
                    filter.ActivateOptions();

                    var layout = new SyslogLayout()
                    {
                        StructuredDataPrefix = "CStarterD@" + serviceName
                    };
                    commonAppender.Layout = layout;
                    layout.ActivateOptions();

                    commonAppender.ActivateOptions();
                }
            }
        }
 public void ActivateOptionsTestNoStructuredDataPrefixSet()
 {
     SyslogLayout layout = new SyslogLayout();
     Assert.That(
         () => layout.ActivateOptions(),
         Throws.Exception
         .TypeOf<ArgumentNullException>()
         .With.Property("ParamName").EqualTo("StructuredDataPrefix")
         );
 }
        public void TestFormat()
        {
            SyslogLayout layout = new SyslogLayout();
            layout.StructuredDataPrefix = "TEST@12345";
            layout.ActivateOptions();

            var exception = new Exception("test exception message");
            ILoggerRepository logRepository = Substitute.For<ILoggerRepository>();
            var evt = new LoggingEvent(typeof(SyslogLayoutTests), logRepository, "test logger", Level.Debug, "test message", exception);

            StringWriter writer = new StringWriter();

            layout.Format(writer, evt);

            string result = writer.ToString();

            // it's hard to test the whole message, because it depends on your machine name, process id, time & date, etc.
            // just test the message's invariant portions
            Assert.IsTrue(result.StartsWith("<135>1 "));
            Assert.IsTrue(result.Contains("[TEST@12345 EventSeverity=\"DEBUG\" ExceptionType=\"System.Exception\" ExceptionMessage=\"test exception message\"]"));
            Assert.IsTrue(result.EndsWith("test message" + Environment.NewLine));
        }
        public void TestThatWeTruncateLongMessages()
        {
            SyslogLayout layout = new SyslogLayout();
            layout.StructuredDataPrefix = "TEST@12345";
            layout.ActivateOptions();

            StringBuilder longMessage = new StringBuilder();
            for (int i = 0; i < 2048; i++)
            {
                longMessage.Append("test message");
            }

            var exception = new ArgumentNullException();
            ILoggerRepository logRepository = Substitute.For<ILoggerRepository>();
            var evt = new LoggingEvent(typeof(SyslogLayoutTests), logRepository, "test logger", Level.Debug, longMessage.ToString(), exception);

            StringWriter writer = new StringWriter();

            layout.Format(writer, evt);

            string result = writer.ToString();

            Assert.AreEqual(2048, result.Length);
        }
Beispiel #5
0
        static void PrepareLogger(string domain)
        {
            Hierarchy hier = log4net.LogManager.GetRepository() as Hierarchy;

            string logRootFullName = Path.Combine(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' })).FullName,
                "logs", "CStarterd");

            if(null != hier)
            {
                RollingFileAppender commonAppender = (RollingFileAppender)hier.GetAppenders().Where(a => a.Name.Equals("CommonAppender")).FirstOrDefault();

                if (null != commonAppender)
                {
                    commonAppender.File = Path.Combine(logRootFullName, "Common", "logs.txt");

                    var filter = new LogExceptionToFileFilter()
                    {
                        ExceptionLogFolder = Path.Combine(logRootFullName, "Common", "Exceptions")
                    };
                    commonAppender.AddFilter(filter);
                    filter.ActivateOptions();

                    var layout = new SyslogLayout()
                    {
                        StructuredDataPrefix = "CStarterD@" + domain
                    };
                    commonAppender.Layout = layout;
                    layout.ActivateOptions();

                    commonAppender.ActivateOptions();
                }

                RollingFileAppender starterAppender = (RollingFileAppender)hier.GetAppenders().Where(a => a.Name.Equals("StarterAppender")).FirstOrDefault();

                if(null != starterAppender)
                {
                    starterAppender.File = Path.Combine(logRootFullName, "Starterd", "logs.txt");

                    var filter = new LogExceptionToFileFilter()
                    {
                        ExceptionLogFolder = Path.Combine(logRootFullName, "Starterd", "Exceptions")
                    };
                    starterAppender.AddFilter(filter);
                    filter.ActivateOptions();

                    var layout = new SyslogLayout()
                    {
                        StructuredDataPrefix = "CStarterD@" + domain
                    };
                    starterAppender.Layout = layout;
                    layout.ActivateOptions();

                    starterAppender.ActivateOptions();
                }
            }
        }
Beispiel #6
0
        static void PrepareAppender(string domain, string minLevel)
        {
            string logRootFullName = Path.Combine(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' })).FullName,
                "logs", "CStarterd");

            foreach (ILoggerRepository respository in log4net.LogManager.GetAllRepositories())
            {
                Hierarchy hier = (Hierarchy)respository;

                foreach (var appender in hier.GetAppenders())
                {
                    MethodInfo addFilterMethod = appender.GetType().GetMethod("AddFilter", BindingFlags.Public | BindingFlags.Instance);

                    if (null != addFilterMethod)
                    {
                        var filter = new LogExceptionToFileFilter()
                        {
                            ExceptionLogFolder = Path.Combine(logRootFullName, appender.Name, "Exceptions")
                        };
                        addFilterMethod.Invoke(appender, new object[] { filter });
                        filter.ActivateOptions();
                    }

                    PropertyInfo fileProp = appender.GetType().GetProperty("File", BindingFlags.Public | BindingFlags.Instance);

                    if (null != fileProp)
                    {
                        fileProp.SetValue(appender, Path.Combine(logRootFullName, appender.Name, "logs.txt"), null);
                    }

                    PropertyInfo layoutProp = appender.GetType().GetProperty("Layout", BindingFlags.Public | BindingFlags.Instance);

                    if (null != layoutProp)
                    {
                        var layout = new SyslogLayout()
                        {
                            StructuredDataPrefix = "CStarterD@" + domain
                        };

                        layoutProp.SetValue(appender,
                            layout, null);

                        layout.ActivateOptions();
                    }

                    MethodInfo activeMethod = appender.GetType().GetMethod("ActivateOptions", BindingFlags.Public | BindingFlags.Instance);

                    if (null != activeMethod)
                        activeMethod.Invoke(appender, null);
                }

                PrepareLoggerLevel(respository, domain, minLevel);
            }

            PrepareRootLoggerLevel(minLevel);
        }