Skip to content

Commit 560c009

Browse files
authored
Merge pull request #113 from GordoMalvado/dev
#112 bug fix for unicode non-xml supported chars
2 parents 5b740e2 + 70ce777 commit 560c009

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 Serilog Contributors
1+
// Copyright 2015 Serilog Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -28,6 +28,14 @@ namespace Serilog.Sinks.MSSqlServer
2828
/// </summary>
2929
public static class XmlPropertyFormatter
3030
{
31+
32+
/// <summary>
33+
/// Regex to trasnform any non-xml char into ?, acoiding any exceptions on inserting the xml properties into the database
34+
/// </summary>
35+
private static Regex _invalidXMLChars = new Regex(
36+
@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]",
37+
RegexOptions.Compiled);
38+
3139
/// <summary>
3240
/// Simplify the object so as to make handling the serialized
3341
/// representation easier.
@@ -204,7 +212,7 @@ static string SimplifyScalar(object value)
204212
{
205213
if (value == null) return null;
206214

207-
return new XText(value.ToString()).ToString();
215+
return new XText(_invalidXMLChars.Replace(value.ToString(), m => "\\u" + ((ushort)m.Value[0]).ToString("x4"))).ToString();
208216
}
209217
}
210-
}
218+
}

0 commit comments

Comments
 (0)