-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: xml escape serialized properties (#52)
Correctly XML escape all properties serialized by `Log4jTextFormatter` and `Log4netTextFormatter`. Closes #51
- Loading branch information
1 parent
badce47
commit 83d0dd0
Showing
7 changed files
with
573 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/Serilog.Sinks.Udp/Sinks/Udp/Private/XmlSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2015-2019 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Serilog.Sinks.Udp.Private | ||
{ | ||
/// <remarks> | ||
/// The methods in this class where influenced by | ||
/// https://weblog.west-wind.com/posts/2018/Nov/30/Returning-an-XML-Encoded-String-in-NET. | ||
/// </remarks> | ||
internal class XmlSerializer | ||
{ | ||
private const char LtCharacter = '<'; | ||
private const char GtCharacter = '>'; | ||
private const char AmpCharacter = '&'; | ||
private const char QuotCharacter = '\"'; | ||
private const char AposCharacter = '\''; | ||
private const char LfCharacter = '\n'; | ||
private const char CrCharacter = '\r'; | ||
private const char TabCharacter = '\t'; | ||
|
||
private const string LfString = "\n"; | ||
private const string CrString = "\r"; | ||
private const string TabString = "\t"; | ||
|
||
private const string SerializedLt = "<"; | ||
private const string SerializedGt = ">"; | ||
private const string SerializedAmp = "&"; | ||
private const string SerializedQuot = """; | ||
private const string SerializedApos = "'"; | ||
private const string SerializedLf = "
"; | ||
private const string SerializedCr = "
"; | ||
private const string SerializedTab = "	"; | ||
|
||
internal void SerializeXmlValue(TextWriter output, string text, bool isAttribute) | ||
{ | ||
foreach (var character in text) | ||
{ | ||
output.Write(SerializeXmlValue(character, isAttribute)); | ||
} | ||
} | ||
|
||
internal string SerializeXmlValue(string text, bool isAttribute) | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
foreach (var character in text) | ||
{ | ||
builder.Append(SerializeXmlValue(character, isAttribute)); | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
private static string SerializeXmlValue(char character, bool isAttribute) | ||
{ | ||
if (character == LtCharacter) | ||
{ | ||
return SerializedLt; | ||
} | ||
|
||
if (character == GtCharacter) | ||
{ | ||
return SerializedGt; | ||
} | ||
|
||
if (character == AmpCharacter) | ||
{ | ||
return SerializedAmp; | ||
} | ||
|
||
// Special handling for quotes | ||
if (isAttribute && character == QuotCharacter) | ||
{ | ||
return SerializedQuot; | ||
} | ||
|
||
if (isAttribute && character == AposCharacter) | ||
{ | ||
return SerializedApos; | ||
} | ||
|
||
// Legal sub-chr32 characters | ||
if (character == LfCharacter) | ||
{ | ||
return isAttribute ? SerializedLf : LfString; | ||
} | ||
|
||
if (character == CrCharacter) | ||
{ | ||
return isAttribute ? SerializedCr : CrString; | ||
} | ||
|
||
if (character == TabCharacter) | ||
{ | ||
return isAttribute ? SerializedTab : TabString; | ||
} | ||
|
||
return character.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.