-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
161 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Serilog.Logfmt | ||
{ | ||
enum DoubleQuotesAction | ||
{ | ||
None, | ||
ConvertToSingle, | ||
Escape, | ||
Remove | ||
} | ||
} |
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,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Serilog.Logfmt | ||
{ | ||
public interface IDoubleQuotesOptions | ||
{ | ||
void Escape(); | ||
void ConvertToSingle(); | ||
void Preserve(); | ||
void Remove(); | ||
} | ||
} |
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,17 @@ | ||
using System; | ||
|
||
namespace Serilog.Logfmt | ||
{ | ||
internal class LogfmtFormatProvider : IFormatProvider, ICustomFormatter | ||
{ | ||
public string Format(string format, object arg, IFormatProvider formatProvider) | ||
{ | ||
return arg.ToString(); | ||
} | ||
|
||
public object GetFormat(Type formatType) | ||
{ | ||
return this; | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Serilog.Events; | ||
using Serilog.Parsing; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
|
||
namespace Serilog.Logfmt | ||
{ | ||
static class StringWriterExtensions | ||
{ | ||
public static string ToLogfmtQuotedString(this StringWriter sw, DoubleQuotesAction action) | ||
{ | ||
var sb = sw.GetStringBuilder(); | ||
switch (action) | ||
{ | ||
case DoubleQuotesAction.ConvertToSingle: | ||
sb.Replace('"', '\''); | ||
break; | ||
case DoubleQuotesAction.Escape: | ||
sb.Replace(@"""", @"\"""); | ||
break; | ||
case DoubleQuotesAction.Remove: | ||
sb.Replace(@"""", ""); | ||
break; | ||
default: // None | ||
break; | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
// From https://github.com/serilog/serilog/issues/936 for avoiding extra quotes on strings | ||
public static void WriteMessage(this TextWriter tw, LogEvent logEvent) | ||
{ | ||
Predicate<LogEventPropertyValue> isString = pv => | ||
{ | ||
var sv = pv as ScalarValue; | ||
return (sv != null) && (sv.Value as string) != null; | ||
}; | ||
foreach (var t in logEvent.MessageTemplate.Tokens) | ||
{ | ||
var pt = t as PropertyToken; | ||
LogEventPropertyValue propVal; | ||
if (pt != null && | ||
logEvent.Properties.TryGetValue(pt.PropertyName, out propVal) && | ||
isString(propVal)) | ||
{ | ||
tw.Write(((ScalarValue)propVal).Value); | ||
} | ||
else | ||
{ | ||
t.Render(logEvent.Properties, tw, null); | ||
} | ||
} | ||
} | ||
} | ||
} |
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