Skip to content

Commit

Permalink
ads comments to packer and cleans up Write()
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Featherston Lago committed Mar 26, 2019
1 parent 9e7a6a3 commit 325b352
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
12 changes: 11 additions & 1 deletion src/NLog.Fluentd/FluentdPacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ internal class FluentdPacker
private static DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private readonly Packer packer;

/// <summary>
/// Packs an event in the stream.
/// </summary>
/// <remarks>
/// The event is packed using the Message Mode.
/// </remarks>
public void Pack(DateTime timestamp, string tag, IDictionary<string, string> data)

{
Expand All @@ -27,9 +33,13 @@ public void Pack(DateTime timestamp, string tag, IDictionary<string, string> dat
this.packer.Pack(data, SerializationContext);
}

/// <summary>
/// Initializes a new instance of MsgPack.Packer
/// </summary>
/// <param name="stream">Stream object to be wrapped by the Packer</param>
public FluentdPacker(Stream stream)
{
this.packer = Packer.Create(stream);
this.packer = Packer.Create(stream);
}

private static SerializationContext InstantiateSerializationContext()
Expand Down
20 changes: 9 additions & 11 deletions src/NLog.Fluentd/FluentdTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace NLog.Fluentd
public partial class FluentdTarget : TargetWithLayout, IFluentdTarget
{
private string _fluentdHost;
private string _fluentdTag;
private bool _fluentdEnabled;
private TcpClient _client;
private Stream _stream;
Expand Down Expand Up @@ -178,25 +177,24 @@ protected override void Write(LogEventInfo logEvent)
_fluentdEnabled = bool.Parse(Enabled?.Render(logEvent));
if (!_fluentdEnabled)
{
InternalLogger.Trace("Fluentd is disabled.");
InternalLogger.Trace("Fluentd target is disabled.");
return;
}

string renderedFluentdHost = Host?.Render(logEvent);
_fluentdTag = Tag?.Render(logEvent);
CheckConnectionIsValid(Host.Render(logEvent));

string fluentdTag = Tag?.Render(logEvent);
Dictionary<string, string> record = new Dictionary<string, string>();
record.Add("message", Layout.Render(logEvent));

CheckConnectionIsValid(renderedFluentdHost);
InternalLogger.Trace("Fluentd (Name={0}): Sending to address: '{1}:{2}'", Name, _fluentdHost, Port);
var record = new Dictionary<string, string>();
var logMessage = Layout.Render(logEvent);;
record.Add("message", logMessage);
try
{
this._packer.Pack(logEvent.TimeStamp, _fluentdTag, record);
InternalLogger.Trace("Fluentd (Name={0}): Sending to address: '{1}:{2}'", Name, _fluentdHost, Port);
this._packer.Pack(logEvent.TimeStamp, fluentdTag, record);
}
catch (Exception ex)
{
InternalLogger.Warn("Fluentd Emit - " + ex.ToString());
InternalLogger.Warn("Fluentd: Error Packing event - " + ex.ToString());
ResetConnection();
throw; // Notify NLog of failure
}
Expand Down

0 comments on commit 325b352

Please sign in to comment.