Skip to content

Commit

Permalink
Merge pull request #5 from GokGokalp/develop
Browse files Browse the repository at this point in the history
Some refactoring
  • Loading branch information
GokGokalp authored May 18, 2018
2 parents 4fa5576 + 02f3e50 commit 0cd7928
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 6 deletions.
26 changes: 26 additions & 0 deletions NLog.Web.AspNetCore.Targets.Gelf.1.1.0.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>NLog.Web.AspNetCore.Targets.Gelf</id>
<version>1.1.0</version>
<title>NLog.Web.AspNetCore.Targets.Gelf</title>
<authors>Gökhan Gökalp</authors>
<owners>Gökhan Gökalp</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<licenseUrl>https://github.com/GokGokalp/NLog.Web.AspNetCore.Targets.Gelf/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/GokGokalp/NLog.Web.AspNetCore.Targets.Gelf</projectUrl>
<description>Gelf4NLog is an NLog target implementation to push log messages to GrayLog2.</description>
<copyright>Copyright 2018</copyright>
<tags>gelf graylog2 nlog</tags>
<dependencies>
<group targetFramework=".NETStandard2.0">
<dependency id="Newtonsoft.Json" version="10.0.2" exclude="Build,Analyzers" />
<dependency id="System.ComponentModel.Annotations" version="4.3.0" exclude="Build,Analyzers" />
<dependency id="NLog.Extensions.Logging" version="1.0.2" />
</group>
</dependencies>
</metadata>
<files>
<file src="lib\netstandard2.0\NLog.Web.AspNetCore.Targets.Gelf.dll" target="lib\netstandard2.0\NLog.Web.AspNetCore.Targets.Gelf.dll" />
</files>
</package>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog.Web.AspNetCore" Version="4.4.1" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.5.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NLog.Web.AspNetCore.Targets.Gelf\NLog.Web.AspNetCore.Targets.Gelf.csproj" />
Expand Down
38 changes: 38 additions & 0 deletions NLog.Web.AspNetCore.Targets.Gelf.Tests/GelfConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Xunit;

namespace NLog.Web.AspNetCore.Targets.Gelf.Tests
{
public class GelfConverterTests
{
[Fact]
public void ShouldGetGelfJsonAddMappedDiagnosticsLogicalContextData()
{
MappedDiagnosticsLogicalContext.Set("test", "value");

var logEvent = LogEventInfo.Create(LogLevel.Info, "loggerName", null, "message");

var converter = new GelfConverter();

// Act
var gelfJson = converter.GetGelfJson(logEvent, "facility");

Assert.Equal("value", gelfJson.Value<string>("_test"));
}

[Fact]
public void ShouldGetGelfJsonDiscardMappedDiagnosticsLogicalContextDataIfPresentInLogEventInfo()
{
MappedDiagnosticsLogicalContext.Set("test", "value");

var logEvent = LogEventInfo.Create(LogLevel.Info, "loggerName", null, "message");
logEvent.Properties.Add("test", "anotherValue");

var converter = new GelfConverter();

// Act
var gelfJson = converter.GetGelfJson(logEvent, "facility");

Assert.Equal("anotherValue", gelfJson.Value<string>("_test"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
<EmbeddedResource Include="..\NLog.Web.AspNetCore.Targets.Gelf.Tests\Resources\LongMessage.txt">
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-rc6" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.0.2" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.3.0" />
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions NLog.Web.AspNetCore.Targets.Gelf/Target/GelfConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -66,6 +67,16 @@ public JObject GetGelfJson(LogEventInfo logEventInfo, string facility)
//Add any other interesting data to LogEventInfo properties
logEventInfo.Properties.Add("LoggerName", logEventInfo.LoggerName);

// adding MappedDiagnosticsLogicalContext data
MappedDiagnosticsLogicalContext.GetNames()
.Select(n => (Name: n, Value: MappedDiagnosticsLogicalContext.GetObject(n)))
.ToList()
.ForEach(t =>
{
if (!logEventInfo.Properties.ContainsKey(t.Name))
logEventInfo.Properties.Add(t.Name, t.Value);
});

//We will persist them "Additional Fields" according to Gelf spec
foreach (var property in logEventInfo.Properties)
{
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# NLog.Web.AspNetCore.Targets.Gelf
Gelf4NLog is an [NLog] target implementation to push log messages to [GrayLog2]. It implements the [Gelf] specification and communicates with GrayLog server via UDP.

[![NuGet version](https://badge.fury.io/nu/NLog.Web.AspNetCore.Targets.Gelf.svg)](https://badge.fury.io/nu/NLog.Web.AspNetCore.Targets.Gelf)

## History
Code forked from https://github.com/2020Legal/NLog.Targets.Gelf which is a fork from https://github.com/akurdyukov/Gelf4NLog who forked the origonal code from https://github.com/seymen/Gelf4NLog

Expand Down Expand Up @@ -32,7 +34,12 @@ Here is a sample nlog.config configuration file for graylog:
</extensions>
<targets>
<target xsi:type="File" name="debugFile" filename="C:\@Logs\${shortdate}-${level}-${applicationName}.txt" layout="${longdate}|${level:upperCase=true}|${logger}|${aspnet-Request-Method}|url: ${aspnet-Request-Url}${aspnet-Request-QueryString}|${message}" concurrentWrites="false" />
<target xsi:type="Gelf" name="graylog" endpoint="udp://192.168.99.100:12201" facility="console-runner" SendLastFormatParameter="true" />
<target xsi:type="Gelf" name="graylog" endpoint="udp://192.168.99.100:12201" facility="console-runner" SendLastFormatParameter="true">

<!-- Optional parameters -->
<parameter name="param1" layout="${longdate}"/>
<parameter name="param2" layout="${callsite}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="debugFile, graylog" />
Expand Down Expand Up @@ -71,4 +78,4 @@ will log Publisher and ReleaseDate as separate fields in Graylog

[NLog]: http://nlog-project.org/
[GrayLog2]: http://graylog2.org/
[Gelf]: http://graylog2.org/about/gelf
[Gelf]: http://graylog2.org/about/gelf
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ services:
- some-elasticsearch:elasticsearch
ports:
- "9000:9000"
- "12201/udp:12201/udp"
- "1514/udp:1514/udp"
- "12201:12201/udp"
- "1514:1514/udp"

0 comments on commit 0cd7928

Please sign in to comment.