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); }
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."); }
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); }
/// <summary> /// Initializes a new instance of the <see cref="CsvHeaderLayout"/> class. /// </summary> /// <param name="parent">The parent.</param> public CsvHeaderLayout(CsvLayout parent) { _parent = parent; }
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); } }
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); }
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())); }
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)); } }
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); }
/// <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; }
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); }
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); } }