Ejemplo n.º 1
0
        public void CsvLayoutRenderingAutoQuoting()
        {
            var csvLayout = new CsvLayout()
            {
                Quoting = CsvQuotingMode.Auto,
                Columns =
                {
                    new CsvColumn("date",         "${longdate}"),
                    new CsvColumn("level",        "${level}"),
                    new CsvColumn("message;text", "${message}"),
                },
                QuoteChar = "'",
                Delimiter = CsvColumnDelimiterMode.Semicolon,
            };

            // no quoting
            Assert.Equal(
                "2010-01-01 12:34:56.0000;Info;hello, world",
                csvLayout.Render(new LogEventInfo
            {
                TimeStamp = new DateTime(2010, 01, 01, 12, 34, 56),
                Level     = LogLevel.Info,
                Message   = "hello, world"
            }));

            // multi-line string - requires quoting
            Assert.Equal(
                "2010-01-01 12:34:56.0000;Info;'hello\rworld'",
                csvLayout.Render(new LogEventInfo
            {
                TimeStamp = new DateTime(2010, 01, 01, 12, 34, 56),
                Level     = LogLevel.Info,
                Message   = "hello\rworld"
            }));

            // multi-line string - requires quoting
            Assert.Equal(
                "2010-01-01 12:34:56.0000;Info;'hello\nworld'",
                csvLayout.Render(new LogEventInfo
            {
                TimeStamp = new DateTime(2010, 01, 01, 12, 34, 56),
                Level     = LogLevel.Info,
                Message   = "hello\nworld"
            }));

            // quote character used in string, will be quoted and doubled
            Assert.Equal(
                "2010-01-01 12:34:56.0000;Info;'hello''world'",
                csvLayout.Render(new LogEventInfo
            {
                TimeStamp = new DateTime(2010, 01, 01, 12, 34, 56),
                Level     = LogLevel.Info,
                Message   = "hello'world"
            }));

            Assert.Equal("date;level;'message;text'", csvLayout.Header.Render(LogEventInfo.CreateNullEvent()));
        }
Ejemplo n.º 2
0
        public void CsvLayoutCachingTest()
        {
            var csvLayout = new CsvLayout()
            {
                Quoting = CsvQuotingMode.Auto,
                Columns =
                {
                    new CsvColumn("date",    "${longdate}"),
                    new CsvColumn("level",   "${level}"),
                    new CsvColumn("message", "${message}"),
                },
                QuoteChar = "'",
                Delimiter = CsvColumnDelimiterMode.Semicolon,
            };

            var e1 = new LogEventInfo
            {
                TimeStamp = new DateTime(2010, 01, 01, 12, 34, 56),
                Level     = LogLevel.Info,
                Message   = "hello, world"
            };

            var e2 = new LogEventInfo
            {
                TimeStamp = new DateTime(2010, 01, 01, 12, 34, 57),
                Level     = LogLevel.Info,
                Message   = "hello, world"
            };

            var r11 = csvLayout.Render(e1);
            var r12 = csvLayout.Render(e1);
            var r21 = csvLayout.Render(e2);
            var r22 = csvLayout.Render(e2);

            var h11 = csvLayout.Header.Render(e1);
            var h12 = csvLayout.Header.Render(e1);
            var h21 = csvLayout.Header.Render(e2);
            var h22 = csvLayout.Header.Render(e2);

            Assert.AreSame(r11, r12);
            Assert.AreSame(r21, r22);

            Assert.AreNotSame(r11, r21);
            Assert.AreNotSame(r12, r22);

            Assert.AreSame(h11, h12);
            Assert.AreSame(h21, h22);

            Assert.AreNotSame(h11, h21);
            Assert.AreNotSame(h12, h22);
        }
Ejemplo n.º 3
0
        public void CsvLayoutRenderingFullQuoting()
        {
            var delimiters = new Dictionary <CsvColumnDelimiterMode, string>
            {
                { CsvColumnDelimiterMode.Auto, CultureInfo.CurrentCulture.TextInfo.ListSeparator },
                { CsvColumnDelimiterMode.Comma, "," },
                { CsvColumnDelimiterMode.Semicolon, ";" },
                { CsvColumnDelimiterMode.Space, " " },
                { CsvColumnDelimiterMode.Tab, "\t" },
                { CsvColumnDelimiterMode.Pipe, "|" },
                { CsvColumnDelimiterMode.Custom, "zzz" },
            };

            foreach (var delim in delimiters)
            {
                var csvLayout = new CsvLayout()
                {
                    Quoting = CsvQuotingMode.All,
                    Columns =
                    {
                        new CsvColumn("date",         "${longdate}"),
                        new CsvColumn("level",        "${level}"),
                        new CsvColumn("message;text", "${message}"),
                    },
                    QuoteChar             = "'",
                    Delimiter             = delim.Key,
                    CustomColumnDelimiter = "zzz",
                };

                var ev = new LogEventInfo();
                ev.TimeStamp = new DateTime(2010, 01, 01, 12, 34, 56);
                ev.Level     = LogLevel.Info;
                ev.Message   = string.Concat(csvLayout.QuoteChar, "hello, world", csvLayout.QuoteChar);

                string sep = delim.Value;
                Assert.Equal("'2010-01-01 12:34:56.0000'" + sep + "'Info'" + sep + "'''hello, world'''", csvLayout.Render(ev));
                Assert.Equal("'date'" + sep + "'level'" + sep + "'message;text'", csvLayout.Header.Render(ev));
            }
        }