コード例 #1
2
ファイル: Program.cs プロジェクト: jtorjo/logwizard
        static void nlog_to_csv_file() {
            FileTarget target = new FileTarget();
            target.FileName = "${basedir}/logs/csv_file.csv";

            CsvLayout layout = new CsvLayout();

            layout.Columns.Add(new CsvColumn("time", "${longdate}"));
            layout.Columns.Add(new CsvColumn("message", "${message}"));
            layout.Columns.Add(new CsvColumn("logger", "${logger}"));
            layout.Columns.Add(new CsvColumn("level", "${level}"));

            target.Layout = layout;

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);            
        }
コード例 #2
1
ファイル: Example.cs プロジェクト: CharlieBP/NLog
    static void Main(string[] args)
    {
        FileTarget target = new FileTarget();
        target.FileName = "${basedir}/file.csv";

        CsvLayout layout = new CsvLayout();

        layout.Columns.Add(new CsvColumn("time", "${longdate}"));
        layout.Columns.Add(new CsvColumn("message", "${message}"));
        layout.Columns.Add(new CsvColumn("logger", "${logger}"));
        layout.Columns.Add(new CsvColumn("level", "${level}"));

        target.Layout = layout;

        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);

        Logger logger = LogManager.GetLogger("Example");
        logger.Debug("log message");
        logger.Debug("Message with \"quotes\" and \nnew line characters.");
    }
コード例 #3
0
ファイル: ThreadAgnosticTests.cs プロジェクト: ExM/NLog
        public void CsvThreadAgnostic()
        {
            CsvLayout l = new CsvLayout()
            {
                Columns =
                {
                    new CsvColumn("name1", "${message}"),
                    new CsvColumn("name2", "${level}"),
                    new CsvColumn("name3", "${longdate}"),
                },
            };

            l.Initialize(CommonCfg);
            Assert.IsTrue(l.IsThreadAgnostic);
        }
コード例 #4
0
ファイル: CsvLayout.cs プロジェクト: pallmall/WCell
 /// <summary>
 /// Initializes a new instance of the <see cref="CsvHeaderLayout"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 public CsvHeaderLayout(CsvLayout parent)
 {
     _parent = parent;
 }
コード例 #5
0
ファイル: FileTargetTests.cs プロジェクト: shadowca/NLog
        public void CsvHeaderTest()
        {
            var tempFile = Path.GetTempFileName();
            try
            {

                for (var i = 0; i < 2; i++)
                {
                    var layout = new CsvLayout
                    {
                        Delimiter = CsvColumnDelimiterMode.Semicolon,
                        WithHeader = true,
                        Columns =
                        {
                            new CsvColumn("name", "${logger}"),
                            new CsvColumn("level", "${level}"),
                            new CsvColumn("message", "${message}"),
                        }
                    };

                    var ft = new FileTarget
                        {
                            FileName = SimpleLayout.Escape(tempFile),
                            LineEnding = LineEndingMode.LF,
                            Layout = layout,
                            OpenFileCacheTimeout = 0,
                            ReplaceFileContentsOnEachWrite = false
                        };
                    SimpleConfigurator.ConfigureForTargetLogging(ft, LogLevel.Debug);

                    logger.Debug("aaa");
                    LogManager.Configuration = null;
                }
                AssertFileContents(tempFile, "name;level;message\nNLog.UnitTests.Targets.FileTargetTests;Debug;aaa\nNLog.UnitTests.Targets.FileTargetTests;Debug;aaa\n", Encoding.UTF8);
            }
            finally
            {
                if (File.Exists(tempFile))
                    File.Delete(tempFile);
            }
        }
コード例 #6
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);
        }
コード例 #7
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()));
        }
コード例 #8
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));
            }
        }
コード例 #9
0
ファイル: MailTargetTests.cs プロジェクト: Xharze/NLog
        public void CsvLayoutTest()
        {
            var layout = new CsvLayout()
            {
                Delimiter = CsvColumnDelimiterMode.Semicolon,
                WithHeader = true,
                Columns =
                {
                    new CsvColumn("name", "${logger}"),
                    new CsvColumn("level", "${level}"),
                    new CsvColumn("message", "${message}"),
                }
            };

            var mmt = new MockMailTarget
            {
                From = "*****@*****.**",
                To = "*****@*****.**",
                SmtpServer = "server1",
                AddNewLines = true,
                Layout = layout,
            };

            layout.Initialize(null);

            mmt.Initialize(null);

            var exceptions = new List<Exception>();
            mmt.WriteAsyncLogEvents(
                new LogEventInfo(LogLevel.Info, "MyLogger1", "log message 1").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Debug, "MyLogger2", "log message 2").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Error, "MyLogger3", "log message 3").WithContinuation(exceptions.Add));
            Assert.Null(exceptions[0]);

            Assert.Equal(1, mmt.CreatedMocks.Count);

            var mock = mmt.CreatedMocks[0];
            Assert.Equal(1, mock.MessagesSent.Count);
            var msg = mock.MessagesSent[0];
            string expectedBody = "name;level;message\nMyLogger1;Info;log message 1\nMyLogger2;Debug;log message 2\nMyLogger3;Error;log message 3\n";
            Assert.Equal(expectedBody, msg.Body);
        }
コード例 #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CsvHeaderLayout"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 public CsvHeaderLayout(CsvLayout parent)
 {
     this.parent = parent;
 }
コード例 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CsvHeaderLayout"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 public CsvHeaderLayout(CsvLayout parent)
 {
     this.parent = parent;
 }
コード例 #12
0
ファイル: CsvLayout.cs プロジェクト: zhouweiaccp/NLog
 /// <summary>
 /// Initializes a new instance of the <see cref="CsvHeaderLayout"/> class.
 /// </summary>
 /// <param name="parent">The parent.</param>
 public CsvHeaderLayout(CsvLayout parent)
 {
     _parent = parent;
 }
コード例 #13
0
        public void CsvNonAgnostic()
        {
            CsvLayout l = new CsvLayout()
            {
                Columns =
                {
                    new CsvColumn("name1", "${message}"),
                    new CsvColumn("name2", "${threadname}"),
                    new CsvColumn("name3", "${longdate}"),
                },
            };

            l.Initialize(null);
            Assert.False(l.IsThreadAgnostic);
        }
コード例 #14
0
        public void CsvHeaderTest()
        {
            // test for the following changes 
            // https://github.com/NLog/NLog/commit/e1ed0d4857dddc95d5db09ee95e9a0c85afc7810
            // codeplex ticket 6370

            string tempFile = Path.GetTempFileName();
            try
            {

                for (int i = 0; i < 2; i++)
                {
                    var layout = new CsvLayout
                    {
                        Delimiter = CsvColumnDelimiterMode.Semicolon,
                        WithHeader = true,
                        Columns =
                        {
                            new CsvColumn("name", "${logger}"),
                            new CsvColumn("level", "${level}"),
                            new CsvColumn("message", "${message}"),
                        }
                    };

                    FileTarget ft = new FileTarget
                        {
                            FileName = SimpleLayout.Escape(tempFile),
                            LineEnding = LineEndingMode.LF,
                            Layout = layout,
                            OpenFileCacheTimeout = 0,
                            ReplaceFileContentsOnEachWrite = false
                        };
                    SimpleConfigurator.ConfigureForTargetLogging(ft, LogLevel.Debug);

                    logger.Debug("aaa");
                    LogManager.Configuration = null;
                }
                AssertFileContents(tempFile, "name;level;message\nNLog.UnitTests.Targets.FileTargetTests;Debug;aaa\nNLog.UnitTests.Targets.FileTargetTests;Debug;aaa\n", Encoding.UTF8);
            }
            finally
            {
                if (File.Exists(tempFile))
                    File.Delete(tempFile);
            }
        }