-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Avoid boxing of enumerators (https://stackoverflow.com/questions/3168311) and delegate capture - Fix line ending issues Co-authored-by: Luigi Berrettini <[email protected]>
- Loading branch information
1 parent
98b824d
commit ccd1b7f
Showing
9 changed files
with
171 additions
and
155 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
* text=auto | ||
|
||
*.md text eol=crlf | ||
|
||
*.yml text eol=crlf | ||
*.ps1 text eol=crlf | ||
*.sh text eol=lf | ||
|
||
*.sln text eol=crlf | ||
*.csproj text eol=crlf | ||
*.cs text eol=crlf | ||
*.config text eol=crlf | ||
*.json text eol=crlf | ||
*.xsd text eol=crlf | ||
*.txt text eol=crlf | ||
|
||
*.png binary | ||
*.snk binary |
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 |
---|---|---|
@@ -1,116 +1,116 @@ | ||
// Licensed under the BSD license | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System; | ||
using NLog.Layouts; | ||
using NLog.Targets.Syslog.Policies; | ||
using System.Globalization; | ||
using System.Text; | ||
using NLog.Targets.Syslog.MessageStorage; | ||
using NLog.Targets.Syslog.Settings; | ||
|
||
namespace NLog.Targets.Syslog.MessageCreation | ||
{ | ||
internal class Rfc5424 : MessageBuilder | ||
{ | ||
private const string BaseTimestampFormat = "yyyy-MM-ddTHH:mm:ss"; | ||
private const int Iso8601MaxTimestampFractionalDigits = 16; | ||
private const int DotNetDateTimeMaxFractionalDigits = 7; | ||
private static readonly byte[] SpaceBytes = { 0x20 }; | ||
|
||
private readonly string version; | ||
private readonly string timestampFormat; | ||
private readonly Layout hostnameLayout; | ||
private readonly Layout appNameLayout; | ||
private readonly Layout procIdLayout; | ||
private readonly Layout msgIdLayout; | ||
private readonly StructuredData structuredData; | ||
private readonly byte[] preamble; | ||
private readonly FqdnHostnamePolicySet hostnamePolicySet; | ||
private readonly AppNamePolicySet appNamePolicySet; | ||
private readonly ProcIdPolicySet procIdPolicySet; | ||
private readonly MsgIdPolicySet msgIdPolicySet; | ||
private readonly Utf8MessagePolicy utf8MessagePolicy; | ||
|
||
public Rfc5424(Facility facility, LogLevelSeverityConfig logLevelSeverityConfig, Rfc5424Config rfc5424Config, EnforcementConfig enforcementConfig) : base(facility, logLevelSeverityConfig, enforcementConfig) | ||
{ | ||
version = rfc5424Config.Version; | ||
timestampFormat = $"{{0:{TimestampFormat(rfc5424Config.TimestampFractionalDigits)}}}"; | ||
hostnameLayout = rfc5424Config.Hostname; | ||
appNameLayout = rfc5424Config.AppName; | ||
procIdLayout = rfc5424Config.ProcId; | ||
msgIdLayout = rfc5424Config.MsgId; | ||
structuredData = new StructuredData(rfc5424Config.StructuredData, enforcementConfig); | ||
preamble = rfc5424Config.DisableBom ? new byte[0] : Encoding.UTF8.GetPreamble(); | ||
hostnamePolicySet = new FqdnHostnamePolicySet(enforcementConfig, rfc5424Config.DefaultHostname); | ||
appNamePolicySet = new AppNamePolicySet(enforcementConfig, rfc5424Config.DefaultAppName); | ||
procIdPolicySet = new ProcIdPolicySet(enforcementConfig); | ||
msgIdPolicySet = new MsgIdPolicySet(enforcementConfig); | ||
utf8MessagePolicy = new Utf8MessagePolicy(enforcementConfig); | ||
} | ||
|
||
protected override void PrepareMessage(ByteArray buffer, LogEventInfo logEvent, string pri, string logEntry) | ||
{ | ||
AppendHeader(buffer, pri, logEvent); | ||
buffer.AppendBytes(SpaceBytes); | ||
AppendStructuredData(buffer, logEvent); | ||
buffer.AppendBytes(SpaceBytes); | ||
AppendMsg(buffer, logEntry); | ||
|
||
utf8MessagePolicy.Apply(buffer); | ||
} | ||
|
||
private static StringBuilder TimestampFormat(int fractionalDigits) | ||
{ | ||
// BaseTimestampFormat.Length + 1 decimal point + 1 K + Iso8601MaxTimestampFractionalDigits | ||
var maxTimestampFormatLength = BaseTimestampFormat.Length + 2 + Iso8601MaxTimestampFractionalDigits; | ||
var formatSb = new StringBuilder(BaseTimestampFormat, maxTimestampFormatLength); | ||
|
||
if (fractionalDigits <= 0) | ||
return formatSb.Append("K"); | ||
|
||
var fRepeatCount = Math.Min(fractionalDigits, DotNetDateTimeMaxFractionalDigits); | ||
var requestedMinusDotNet = fractionalDigits - DotNetDateTimeMaxFractionalDigits; | ||
const int isoMinusDotNet = Iso8601MaxTimestampFractionalDigits - DotNetDateTimeMaxFractionalDigits; | ||
var zeroRepeatCount = Math.Max(0, Math.Min(requestedMinusDotNet, isoMinusDotNet)); | ||
return formatSb | ||
.Append('.') | ||
.Append('f', fRepeatCount) | ||
.Append('0', Math.Max(0, zeroRepeatCount)) | ||
.Append("K"); | ||
} | ||
|
||
private void AppendHeader(ByteArray buffer, string pri, LogEventInfo logEvent) | ||
{ | ||
var timestamp = string.Format(CultureInfo.InvariantCulture, timestampFormat, logEvent.TimeStamp); | ||
var hostname = hostnamePolicySet.Apply(hostnameLayout.Render(logEvent)); | ||
var appName = appNamePolicySet.Apply(appNameLayout.Render(logEvent)); | ||
var procId = procIdPolicySet.Apply(procIdLayout.Render(logEvent)); | ||
var msgId = msgIdPolicySet.Apply(msgIdLayout.Render(logEvent)); | ||
|
||
buffer.AppendAscii(pri); | ||
buffer.AppendAscii(version); | ||
buffer.AppendBytes(SpaceBytes); | ||
buffer.AppendAscii(timestamp); | ||
buffer.AppendBytes(SpaceBytes); | ||
buffer.AppendAscii(hostname); | ||
buffer.AppendBytes(SpaceBytes); | ||
buffer.AppendAscii(appName); | ||
buffer.AppendBytes(SpaceBytes); | ||
buffer.AppendAscii(procId); | ||
buffer.AppendBytes(SpaceBytes); | ||
buffer.AppendAscii(msgId); | ||
} | ||
|
||
private void AppendStructuredData(ByteArray buffer, LogEventInfo logEvent) | ||
{ | ||
structuredData.Append(buffer, logEvent); | ||
} | ||
|
||
private void AppendMsg(ByteArray buffer, string logEntry) | ||
{ | ||
buffer.AppendBytes(preamble); | ||
buffer.AppendUtf8(logEntry); | ||
} | ||
} | ||
// Licensed under the BSD license | ||
// See the LICENSE file in the project root for more information | ||
|
||
using NLog.Layouts; | ||
using NLog.Targets.Syslog.MessageStorage; | ||
using NLog.Targets.Syslog.Policies; | ||
using NLog.Targets.Syslog.Settings; | ||
using System; | ||
using System.Globalization; | ||
using System.Text; | ||
|
||
namespace NLog.Targets.Syslog.MessageCreation | ||
{ | ||
internal class Rfc5424 : MessageBuilder | ||
{ | ||
private const string BaseTimestampFormat = "yyyy-MM-ddTHH:mm:ss"; | ||
private const int Iso8601MaxTimestampFractionalDigits = 16; | ||
private const int DotNetDateTimeMaxFractionalDigits = 7; | ||
private static readonly byte[] SpaceBytes = { 0x20 }; | ||
|
||
private readonly string version; | ||
private readonly string timestampFormat; | ||
private readonly Layout hostnameLayout; | ||
private readonly Layout appNameLayout; | ||
private readonly Layout procIdLayout; | ||
private readonly Layout msgIdLayout; | ||
private readonly StructuredData structuredData; | ||
private readonly byte[] preamble; | ||
private readonly FqdnHostnamePolicySet hostnamePolicySet; | ||
private readonly AppNamePolicySet appNamePolicySet; | ||
private readonly ProcIdPolicySet procIdPolicySet; | ||
private readonly MsgIdPolicySet msgIdPolicySet; | ||
private readonly Utf8MessagePolicy utf8MessagePolicy; | ||
|
||
public Rfc5424(Facility facility, LogLevelSeverityConfig logLevelSeverityConfig, Rfc5424Config rfc5424Config, EnforcementConfig enforcementConfig) : base(facility, logLevelSeverityConfig, enforcementConfig) | ||
{ | ||
version = rfc5424Config.Version; | ||
timestampFormat = $"{{0:{TimestampFormat(rfc5424Config.TimestampFractionalDigits)}}}"; | ||
hostnameLayout = rfc5424Config.Hostname; | ||
appNameLayout = rfc5424Config.AppName; | ||
procIdLayout = rfc5424Config.ProcId; | ||
msgIdLayout = rfc5424Config.MsgId; | ||
structuredData = new StructuredData(rfc5424Config.StructuredData, enforcementConfig); | ||
preamble = rfc5424Config.DisableBom ? new byte[0] : Encoding.UTF8.GetPreamble(); | ||
hostnamePolicySet = new FqdnHostnamePolicySet(enforcementConfig, rfc5424Config.DefaultHostname); | ||
appNamePolicySet = new AppNamePolicySet(enforcementConfig, rfc5424Config.DefaultAppName); | ||
procIdPolicySet = new ProcIdPolicySet(enforcementConfig); | ||
msgIdPolicySet = new MsgIdPolicySet(enforcementConfig); | ||
utf8MessagePolicy = new Utf8MessagePolicy(enforcementConfig); | ||
} | ||
|
||
protected override void PrepareMessage(ByteArray buffer, LogEventInfo logEvent, string pri, string logEntry) | ||
{ | ||
AppendHeader(buffer, pri, logEvent); | ||
buffer.AppendBytes(SpaceBytes); | ||
AppendStructuredData(buffer, logEvent); | ||
buffer.AppendBytes(SpaceBytes); | ||
AppendMsg(buffer, logEntry); | ||
|
||
utf8MessagePolicy.Apply(buffer); | ||
} | ||
|
||
private static StringBuilder TimestampFormat(int fractionalDigits) | ||
{ | ||
// BaseTimestampFormat.Length + 1 decimal point + 1 K + Iso8601MaxTimestampFractionalDigits | ||
var maxTimestampFormatLength = BaseTimestampFormat.Length + 2 + Iso8601MaxTimestampFractionalDigits; | ||
var formatSb = new StringBuilder(BaseTimestampFormat, maxTimestampFormatLength); | ||
|
||
if (fractionalDigits <= 0) | ||
return formatSb.Append("K"); | ||
|
||
var fRepeatCount = Math.Min(fractionalDigits, DotNetDateTimeMaxFractionalDigits); | ||
var requestedMinusDotNet = fractionalDigits - DotNetDateTimeMaxFractionalDigits; | ||
const int isoMinusDotNet = Iso8601MaxTimestampFractionalDigits - DotNetDateTimeMaxFractionalDigits; | ||
var zeroRepeatCount = Math.Max(0, Math.Min(requestedMinusDotNet, isoMinusDotNet)); | ||
return formatSb | ||
.Append('.') | ||
.Append('f', fRepeatCount) | ||
.Append('0', zeroRepeatCount) | ||
.Append("K"); | ||
} | ||
|
||
private void AppendHeader(ByteArray buffer, string pri, LogEventInfo logEvent) | ||
{ | ||
var timestamp = string.Format(CultureInfo.InvariantCulture, timestampFormat, logEvent.TimeStamp); | ||
var hostname = hostnamePolicySet.Apply(hostnameLayout.Render(logEvent)); | ||
var appName = appNamePolicySet.Apply(appNameLayout.Render(logEvent)); | ||
var procId = procIdPolicySet.Apply(procIdLayout.Render(logEvent)); | ||
var msgId = msgIdPolicySet.Apply(msgIdLayout.Render(logEvent)); | ||
|
||
buffer.AppendAscii(pri); | ||
buffer.AppendAscii(version); | ||
buffer.AppendBytes(SpaceBytes); | ||
buffer.AppendAscii(timestamp); | ||
buffer.AppendBytes(SpaceBytes); | ||
buffer.AppendAscii(hostname); | ||
buffer.AppendBytes(SpaceBytes); | ||
buffer.AppendAscii(appName); | ||
buffer.AppendBytes(SpaceBytes); | ||
buffer.AppendAscii(procId); | ||
buffer.AppendBytes(SpaceBytes); | ||
buffer.AppendAscii(msgId); | ||
} | ||
|
||
private void AppendStructuredData(ByteArray buffer, LogEventInfo logEvent) | ||
{ | ||
structuredData.Append(buffer, logEvent); | ||
} | ||
|
||
private void AppendMsg(ByteArray buffer, string logEntry) | ||
{ | ||
buffer.AppendBytes(preamble); | ||
buffer.AppendUtf8(logEntry); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.