|
1 |
| -using Serilog.Events; |
| 1 | +using Moq; |
| 2 | +using Serilog.Events; |
| 3 | +using Serilog.Formatting; |
2 | 4 | using Serilog.Parsing;
|
3 | 5 | using System;
|
4 | 6 | using System.Collections.Generic;
|
5 | 7 | using System.Data;
|
6 | 8 | using System.Globalization;
|
| 9 | +using System.IO; |
7 | 10 | using System.Linq;
|
8 | 11 | using Xunit;
|
9 | 12 |
|
@@ -99,10 +102,52 @@ public void GetColumnsAndValuesCreatesUtcConvertedTimeStampOfTypeDateTimeOffsetA
|
99 | 102 | Assert.Equal(new TimeSpan(0), timeStampColumnOffset.Offset);
|
100 | 103 | }
|
101 | 104 |
|
102 |
| - private void SetupTest(Serilog.Sinks.MSSqlServer.ColumnOptions options, DateTimeOffset testDateTimeOffset) |
| 105 | + [Fact] |
| 106 | + public void GetColumnsAndValuesWhenCalledWithCustomFormatterRendersLogEventPropertyUsingCustomFormatter() |
| 107 | + { |
| 108 | + // arrange |
| 109 | + const string testLogEventContent = "Content of LogEvent"; |
| 110 | + var options = new Serilog.Sinks.MSSqlServer.ColumnOptions(); |
| 111 | + options.Store.Add(StandardColumn.LogEvent); |
| 112 | + var logEventFormatterMock = new Mock<ITextFormatter>(); |
| 113 | + logEventFormatterMock.Setup(f => f.Format(It.IsAny<LogEvent>(), It.IsAny<TextWriter>())) |
| 114 | + .Callback<LogEvent, TextWriter>((e, w) => w.Write(testLogEventContent)); |
| 115 | + SetupTest(options, DateTimeOffset.UtcNow, logEventFormatterMock.Object); |
| 116 | + |
| 117 | + // act |
| 118 | + var columns = traits.GetColumnsAndValues(logEvent); |
| 119 | + |
| 120 | + // assert |
| 121 | + var logEventColumn = columns.Single(c => c.Key == options.LogEvent.ColumnName); |
| 122 | + Assert.Equal(testLogEventContent, logEventColumn.Value); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void GetColumnsAndValuesWhenCalledWithoutFormatterRendersLogEventPropertyUsingInternalJsonFormatter() |
| 127 | + { |
| 128 | + // arrange |
| 129 | + const string expectedLogEventContent = |
| 130 | + "{\"TimeStamp\":\"2020-01-01T09:00:00.0000000\",\"Level\":\"Information\",\"Message\":\"\",\"MessageTemplate\":\"\"}"; |
| 131 | + var options = new Serilog.Sinks.MSSqlServer.ColumnOptions(); |
| 132 | + options.Store.Add(StandardColumn.LogEvent); |
| 133 | + var testDateTimeOffset = new DateTimeOffset(2020, 1, 1, 9, 0, 0, TimeSpan.Zero); |
| 134 | + SetupTest(options, testDateTimeOffset, null); |
| 135 | + |
| 136 | + // act |
| 137 | + var columns = traits.GetColumnsAndValues(logEvent); |
| 138 | + |
| 139 | + // assert |
| 140 | + var logEventColumn = columns.Single(c => c.Key == options.LogEvent.ColumnName); |
| 141 | + Assert.Equal(expectedLogEventContent, logEventColumn.Value); |
| 142 | + } |
| 143 | + |
| 144 | + private void SetupTest( |
| 145 | + Serilog.Sinks.MSSqlServer.ColumnOptions options, |
| 146 | + DateTimeOffset testDateTimeOffset, |
| 147 | + ITextFormatter logEventFormatter = null) |
103 | 148 | {
|
104 | 149 | this.traits = new MSSqlServerSinkTraits("connectionString", "tableName", "schemaName",
|
105 |
| - options, CultureInfo.InvariantCulture, false, null); |
| 150 | + options, CultureInfo.InvariantCulture, false, logEventFormatter); |
106 | 151 | this.logEvent = new LogEvent(testDateTimeOffset, LogEventLevel.Information, null,
|
107 | 152 | new MessageTemplate(new List<MessageTemplateToken>()), new List<LogEventProperty>());
|
108 | 153 | }
|
|
0 commit comments