Skip to content

Commit a8b55bf

Browse files
authored
Merge pull request #282 from ckadluba/refactor-SinkTraits
Completed unit tests for StandarrdColumnDataGenerator
2 parents 3ad2e49 + 30bb491 commit a8b55bf

File tree

6 files changed

+361
-22
lines changed

6 files changed

+361
-22
lines changed

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Dependencies/SinkDependenciesFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ internal static SinkDependencies Create(
2626
sinkOptions.AzureServiceTokenProviderResource));
2727
var logEventDataGenerator =
2828
new LogEventDataGenerator(columnOptions,
29-
new StandardColumnDataGenerator(columnOptions, formatProvider, logEventFormatter),
29+
new StandardColumnDataGenerator(columnOptions, formatProvider,
30+
new XmlPropertyFormatter(),
31+
logEventFormatter),
3032
new PropertiesColumnDataGenerator(columnOptions));
3133

3234
var sinkDependencies = new SinkDependencies
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Serilog.Events;
16+
17+
namespace Serilog.Sinks.MSSqlServer.Output
18+
{
19+
internal interface IXmlPropertyFormatter
20+
{
21+
string GetValidElementName(string name);
22+
string Simplify(LogEventPropertyValue value, ColumnOptions.PropertiesColumnOptions options);
23+
}
24+
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Output/StandardColumnDataGenerator.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@ internal class StandardColumnDataGenerator : IStandardColumnDataGenerator
1414
{
1515
private readonly ColumnOptions _columnOptions;
1616
private readonly IFormatProvider _formatProvider;
17+
private readonly IXmlPropertyFormatter _xmlPropertyFormatter;
1718
private readonly ITextFormatter _logEventFormatter;
1819
private readonly ISet<string> _additionalColumnPropertyNames;
1920

2021
public StandardColumnDataGenerator(
2122
ColumnOptions columnOptions,
2223
IFormatProvider formatProvider,
24+
IXmlPropertyFormatter xmlPropertyFormatter,
2325
ITextFormatter logEventFormatter)
2426
{
2527
_columnOptions = columnOptions ?? throw new ArgumentNullException(nameof(columnOptions));
2628

2729
_formatProvider = formatProvider;
2830

31+
_xmlPropertyFormatter = xmlPropertyFormatter ?? throw new ArgumentNullException(nameof(xmlPropertyFormatter));
32+
2933
if (_columnOptions.Store.Contains(StandardColumn.LogEvent))
3034
_logEventFormatter = logEventFormatter ?? new JsonLogEventFormatter(_columnOptions, this);
3135

@@ -113,15 +117,15 @@ private string ConvertPropertiesToXmlStructure(IEnumerable<KeyValuePair<string,
113117

114118
foreach (var property in properties)
115119
{
116-
var value = XmlPropertyFormatter.Simplify(property.Value, options);
120+
var value = _xmlPropertyFormatter.Simplify(property.Value, options);
117121
if (options.OmitElementIfEmpty && string.IsNullOrEmpty(value))
118122
{
119123
continue;
120124
}
121125

122126
if (options.UsePropertyKeyAsElementName)
123127
{
124-
sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>{1}</{0}>", XmlPropertyFormatter.GetValidElementName(property.Key), value);
128+
sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>{1}</{0}>", _xmlPropertyFormatter.GetValidElementName(property.Key), value);
125129
}
126130
else
127131
{

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Output/XmlPropertyFormatter.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,13 @@
2121

2222
namespace Serilog.Sinks.MSSqlServer.Output
2323
{
24-
/// <summary>
25-
/// Converts <see cref="LogEventProperty" /> values into simple scalars,
26-
/// dictionaries and lists so that they can be persisted in MSSqlServer.
27-
/// </summary>
28-
internal static class XmlPropertyFormatter
24+
internal class XmlPropertyFormatter : IXmlPropertyFormatter
2925
{
30-
/// <summary>
31-
/// Regex to trasnform any non-xml char into ?, acoiding any exceptions on inserting the xml properties into the database
32-
/// </summary>
3326
private static readonly Regex _invalidXMLChars = new Regex(
3427
@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]",
3528
RegexOptions.Compiled);
3629

37-
/// <summary>
38-
/// Simplify the object so as to make handling the serialized
39-
/// representation easier.
40-
/// </summary>
41-
/// <param name="value">The value to simplify (possibly null).</param>
42-
/// <param name="options">Options to use during formatting</param>
43-
/// <returns>A simplified representation.</returns>
44-
public static string Simplify(LogEventPropertyValue value, ColumnOptions.PropertiesColumnOptions options)
30+
public string Simplify(LogEventPropertyValue value, ColumnOptions.PropertiesColumnOptions options)
4531
{
4632
if (value is ScalarValue scalar)
4733
return SimplifyScalar(scalar.Value);
@@ -183,7 +169,7 @@ public static string Simplify(LogEventPropertyValue value, ColumnOptions.Propert
183169
return null;
184170
}
185171

186-
public static string GetValidElementName(string name)
172+
public string GetValidElementName(string name)
187173
{
188174
if (string.IsNullOrWhiteSpace(name))
189175
{

test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Output/JsonLogEventFormatterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public JsonLogEventFormatterTests()
2323
_testColumnOptions.Store.Add(StandardColumn.LogEvent);
2424

2525
// TODO use mock for _testColumnsDataGenerator
26-
_testStandardColumnDataGenerator = new StandardColumnDataGenerator(_testColumnOptions, formatProvider: null, logEventFormatter: null);
26+
_testStandardColumnDataGenerator = new StandardColumnDataGenerator(_testColumnOptions, formatProvider: null, new XmlPropertyFormatter(), logEventFormatter: null);
2727

2828
_sut = new JsonLogEventFormatter(_testColumnOptions, _testStandardColumnDataGenerator);
2929
}

0 commit comments

Comments
 (0)