Skip to content

Commit 6f9afa9

Browse files
author
par.dahlman
committed
Implement the core logic in the preformatter
Which basically uses the same approach as string.Format method does, but only for numeric formatted arguments and not serilog onces.
1 parent cfba93e commit 6f9afa9

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

Common.Logging.Serilog.Tests/SerilogPreformatterTests.cs

+29
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,36 @@ public void Should_Format_Entire_String_If_Only_Numeric_Formatting_Is_Used()
6161
Assert.That(_resultTemplate, Is.EqualTo(expected));
6262
}
6363

64+
[Test]
65+
public void Should_Keep_String_Unformatted_If_Serilog_Formatting_Used()
6466
{
67+
/* Setup */
68+
const string originalTemplate = "Look at this, a {serilog} formatted string!";
69+
var args = new object[] { "serilog" };
70+
71+
/* Test */
72+
var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, out _resultArgs);
73+
74+
/* Assert */
75+
Assert.That(result, Is.True);
76+
Assert.That(_resultTemplate, Is.EqualTo(originalTemplate));
77+
}
78+
79+
[Test]
80+
public void Should_Preformat_Numeric_Part_Of_String_But_Keep_Serilog_Formatted_String_And_Arguments()
81+
{
82+
/* Setup */
83+
const string originalTemplate = "This {@string} has {1} numeric match";
84+
var args = new object[] { "serilog", "1" };
85+
const string expectedTemplate = "This {@string} has 1 numeric match";
86+
87+
88+
/* Test */
89+
var result = _preformatter.TryPreformat(originalTemplate, args, out _resultTemplate, out _resultArgs);
90+
91+
/* Assert */
92+
Assert.That(result, Is.True);
93+
Assert.That(_resultTemplate, Is.EqualTo(expectedTemplate));
6594
}
6695
}
6796
}

src/SerilogPreformatter.cs

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Text;
34
using System.Text.RegularExpressions;
45

56
namespace Common.Logging.Serilog
@@ -22,17 +23,39 @@ public bool TryPreformat(string templateString, object[] args, out string newTem
2223
{
2324
return true;
2425
}
26+
27+
var templateBuilder = new StringBuilder(templateString);
28+
var numericArgs = new List<object>();
29+
var matches = _numericFormattedRegex.Matches(templateString);
30+
31+
for (var i = 0; i < matches.Count; i++)
32+
{
33+
var currentMatcher = matches[i];
34+
var argPosition = GetArgumentPosition(currentMatcher);
35+
var currentArg = args[argPosition];
36+
37+
templateBuilder.Remove(currentMatcher.Index, currentMatcher.Length);
38+
templateBuilder.Insert(currentMatcher.Index, currentArg);
39+
40+
numericArgs.Add(numericArgs);
41+
}
42+
43+
newTemplate = templateBuilder.ToString();
44+
newArgs = args
45+
.Except(numericArgs)
46+
.ToArray();
2547

26-
newTemplate = string.Format(templateString, args);
27-
newArgs = null;
2848
return true;
2949
}
3050

51+
private static int GetArgumentPosition(Match currentMatcher)
3152
{
53+
var nummeric = currentMatcher.Groups
3254
.OfType<Group>()
3355
.Skip(1)
3456
.First()
3557
.Value;
58+
return int.Parse(nummeric);
3659
}
3760
}
3861
}

0 commit comments

Comments
 (0)