コード例 #1
0
ファイル: CsvLayoutTests.cs プロジェクト: bhaeussermann/NLog
        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()));
        }
コード例 #2
0
ファイル: CsvLayoutTests.cs プロジェクト: bhaeussermann/NLog
        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.Same(r11, r12);
            Assert.Same(r21, r22);

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

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

            Assert.NotSame(h11, h21);
            Assert.NotSame(h12, h22);
        }
コード例 #3
0
ファイル: CsvLayoutTests.cs プロジェクト: bhaeussermann/NLog
        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 = "hello, world";

                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));
            }
        }