Skip to content

Commit

Permalink
Added NLog Layout support for options Host and Tag
Browse files Browse the repository at this point in the history
Signed-off-by: Rolf Kristensen <sweaty1@hotmail.com>
  • Loading branch information
snakefoot committed Aug 24, 2024
1 parent f07ff4d commit 81f3209
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="NLog">
<HintPath>..\packages\NLog.2.1.0\lib\net45\NLog.dll</HintPath>
<HintPath>..\packages\NLog.4.5.0\lib\net45\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion src/Demo/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NLog" version="2.1.0" targetFramework="net45" />
<package id="NLog" version="4.5.0" targetFramework="net45" />
</packages>
16 changes: 9 additions & 7 deletions src/NLog.Targets.Fluentd/Fluentd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
using System.Text;
using System.Net.Sockets;
using System.Diagnostics;
using System.Reflection;
using MsgPack;
using MsgPack.Serialization;
using NLog.Layouts;

namespace NLog.Targets
{
Expand Down Expand Up @@ -183,11 +183,11 @@ public FluentdEmitter(Stream stream)
[Target("Fluentd")]
public class Fluentd : NLog.Targets.TargetWithLayout
{
public string Host { get; set; }
public Layout Host { get; set; }

public int Port { get; set; }

public string Tag { get; set; }
public Layout Tag { get; set; }

public bool NoDelay { get; set; }

Expand Down Expand Up @@ -246,8 +246,9 @@ protected void EnsureConnected()

private void ConnectClient()
{
NLog.Common.InternalLogger.Debug("Fluentd Connecting to {0}:{1}", this.Host, this.Port);
this.client.Connect(this.Host, this.Port);
string hostName = RenderLogEvent(this.Host, LogEventInfo.CreateNullEvent());
NLog.Common.InternalLogger.Debug("Fluentd Connecting to {0}:{1}", hostName, this.Port);
this.client.Connect(hostName, this.Port);
this.stream = this.client.GetStream();
this.emitter = new FluentdEmitter(this.stream);
}
Expand Down Expand Up @@ -334,7 +335,8 @@ protected override void Write(LogEventInfo logEvent)

try
{
this.emitter?.Emit(logEvent.TimeStamp, this.Tag, record);
string tagName = RenderLogEvent(this.Tag, logEvent);
this.emitter?.Emit(logEvent.TimeStamp, tagName, record);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -366,7 +368,7 @@ public Fluentd()
this.LingerEnabled = true;
this.LingerTime = 1000;
this.EmitStackTraceWhenAvailable = false;
this.Tag = Assembly.GetCallingAssembly().GetName().Name;
this.Tag = "${processname}";
}
}
}

0 comments on commit 81f3209

Please sign in to comment.