Skip to content

Commit

Permalink
Allow RFC 3164 custom messages (#184 closes #181)
Browse files Browse the repository at this point in the history
  • Loading branch information
luigiberrettini authored Apr 15, 2019
1 parent 9d50c99 commit b747de2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ The maximum length of a message is detailed in many RFCs that can be summarized
* `trace` - `Emergency` / `Alert` / `Critical` / `Error` / `Warning` / `Notice` / `Informational` / `Debug` (default: `Notice`)
* `rfc` - `rfc3164` or `rfc5424` (default: `rfc5424`)
* `rfc3164` - settings related to RFC 3164:
* `hostname` ([Layout](http://github.com/NLog/NLog/wiki/Layouts)) - the HOSTNAME part (default: the hostname of the computer that is creating the message)
* `tag` ([Layout](http://github.com/NLog/NLog/wiki/Layouts)) - the TAG part (default: the name of the assembly that is creating the message)
* `outputPri` - `true` or `false` to output or not the PRI part (custom messages)
* `outputHeader` - `true` or `false` to output or not the HEADER part (custom messages)
* `hostname` ([Layout](http://github.com/NLog/NLog/wiki/Layouts)) - the HOSTNAME field of the HEADER part (default: the hostname of the computer that is creating the message)
* `tag` ([Layout](http://github.com/NLog/NLog/wiki/Layouts)) - the TAG field of the MSG part (default: the name of the assembly that is creating the message)
* `rfc5424` - settings related to RFC 5424:
* `hostname` ([Layout](http://github.com/NLog/NLog/wiki/Layouts)) - the HOSTNAME field of the HEADER part (default: the hostname of the computer that is creating the message)
* `appName` ([Layout](http://github.com/NLog/NLog/wiki/Layouts)) - the APPNAME field of the HEADER part (default: the name of the assembly that is creating the message)
Expand Down
4 changes: 4 additions & 0 deletions src/NLog.Targets.Syslog.Schema/NLog.Targets.Syslog.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@
<xs:element name="rfc3164" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="outputPri" type="xs:boolean" minOccurs="0" maxOccurs="1" />
<xs:element name="outputHeader" type="xs:boolean" minOccurs="0" maxOccurs="1" />
<xs:element name="hostname" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="tag" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:choice>
<xs:attribute name="outputPri" type="xs:boolean" />
<xs:attribute name="outputHeader" type="xs:boolean" />
<xs:attribute name="hostname" type="xs:string" />
<xs:attribute name="tag" type="xs:string" />
</xs:complexType>
Expand Down
10 changes: 9 additions & 1 deletion src/NLog.Targets.Syslog/MessageCreation/Rfc3164.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ internal class Rfc3164 : MessageBuilder
private const string TimestampFormat = "{0:MMM} {0,11:d HH:mm:ss}";
private static readonly byte[] SpaceBytes = { 0x20 };

private readonly bool outputPri;
private readonly bool outputHeader;
private readonly Layout hostnameLayout;
private readonly Layout tagLayout;
private readonly PlainHostnamePolicySet hostnamePolicySet;
Expand All @@ -28,6 +30,8 @@ public Rfc3164(Facility facility, LogLevelSeverityConfig logLevelSeverityConfig,
plainContentPolicySet = new PlainContentPolicySet(enforcementConfig);
asciiMessagePolicy = new AsciiMessagePolicy(enforcementConfig);

outputPri = rfc3164Config.OutputPri;
outputHeader = rfc3164Config.OutputHeader;
hostnameLayout = rfc3164Config.Hostname;
tagLayout = rfc3164Config.Tag;
}
Expand All @@ -44,14 +48,18 @@ protected override void PrepareMessage(ByteArray buffer, LogEventInfo logEvent,
asciiMessagePolicy.Apply(buffer);
}

private static void AppendPriBytes(ByteArray buffer, string pri, Encoding encoding)
private void AppendPriBytes(ByteArray buffer, string pri, Encoding encoding)
{
if (!outputPri)
return;
var priBytes = encoding.GetBytes(pri);
buffer.Append(priBytes);
}

private void AppendHeaderBytes(ByteArray buffer, LogEventInfo logEvent, Encoding encoding)
{
if (!outputHeader)
return;
var timestamp = string.Format(CultureInfo.InvariantCulture, TimestampFormat, logEvent.TimeStamp);
var host = hostnamePolicySet.Apply(hostnameLayout.Render(logEvent));
var header = $"{timestamp} {host}";
Expand Down
18 changes: 18 additions & 0 deletions src/NLog.Targets.Syslog/Settings/Rfc3164Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,25 @@ namespace NLog.Targets.Syslog.Settings
/// <summary>RFC 3164 configuration</summary>
public class Rfc3164Config : NotifyPropertyChanged
{
private bool outputPri;
private bool outputHeader;
private Layout hostname;
private Layout tag;

/// <summary>Whether to output or not the PRI part</summary>
public bool OutputPri
{
get => outputPri;
set => SetProperty(ref outputPri, value);
}

/// <summary>Whether to output or not the HEADER part</summary>
public bool OutputHeader
{
get => outputHeader;
set => SetProperty(ref outputHeader, value);
}

/// <summary>The HOSTNAME field of the HEADER part</summary>
public Layout Hostname
{
Expand All @@ -31,6 +47,8 @@ public Layout Tag
/// <summary>Builds a new instance of the Rfc3164 class</summary>
public Rfc3164Config()
{
outputPri = true;
outputHeader = true;
hostname = Dns.GetHostName();
tag = UniversalAssembly.EntryAssembly().Name();
}
Expand Down

0 comments on commit b747de2

Please sign in to comment.