Skip to content

Commit

Permalink
Merge pull request #48 from datalust/dev
Browse files Browse the repository at this point in the history
1.2.0 Release
  • Loading branch information
nblumhardt authored Apr 29, 2020
2 parents 3349f4e + c1445d1 commit 6102581
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 26 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

An NLog target that writes events to [Seq](https://getseq.net). The target takes full advantage of the structured logging support in **NLog 4.5** to provide hassle-free filtering, searching and analysis.

Projects using earlier NLog versions require the [_Seq.Client.NLog_ package](https://nuget.org/packages/seq.client.nlog) instead.

### Getting started

After installing NLog, install the _NLog.Targets.Seq_ package from NuGet:

```
Install-Package NLog.Targets.Seq
dotnet add package NLog.Targets.Seq
```

Then, add the target and rules entries to your NLog configuration:

```xml
<targets>
<target name="seq" xsi:type="BufferingWrapper" bufferSize="1000" flushTimeout="2000">
<target name="seq" xsi:type="BufferingWrapper" bufferSize="1000"
flushTimeout="2000" slidingTimeout="false">
<target xsi:type="Seq" serverUrl="http://localhost:5341" apiKey="" />
</target>
</targets>
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2017
image: Visual Studio 2019
configuration: Release
install:
- ps: mkdir -Force ".\build\" | Out-Null
Expand Down
Binary file modified asset/nlog-events-in-seq.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions nlog-targets-seq.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.15
# Visual Studio Version 16
VisualStudioVersion = 16.0.29209.62
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AC295EEA-D319-4146-97E0-B978DF6F2557}"
EndProject
Expand All @@ -17,6 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2ED926D3-7
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "asset", "asset", "{69238E6E-26AB-494B-8CBD-65F8C1F0696A}"
ProjectSection(SolutionItems) = preProject
asset\nlog-events-in-seq.png = asset\nlog-events-in-seq.png
asset\nlog-targets-seq.png = asset\nlog-targets-seq.png
asset\nlog-targets-seq.snk = asset\nlog-targets-seq.snk
asset\nlog-targets-seq.svg = asset\nlog-targets-seq.svg
Expand Down
5 changes: 3 additions & 2 deletions sample/Example/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<target name="seq"
xsi:type="BufferingWrapper"
bufferSize="1000"
flushTimeout="2000">
flushTimeout="2000"
slidingTimeout="false">

<target xsi:type="Seq" serverUrl="http://localhost:5341" apiKey="">
<property name="ThreadId" value="${threadid}" as="number" />
Expand All @@ -27,4 +28,4 @@
<logger name="*" minlevel="Info" writeTo="seq" />
<logger name="*" minlevel="Info" writeTo="console" />
</rules>
</nlog>
</nlog>
10 changes: 6 additions & 4 deletions src/NLog.Targets.Seq/NLog.Targets.Seq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
<PropertyGroup>
<Description>An NLog target that writes structured log events to Seq</Description>
<Authors>Datalust;Contributors</Authors>
<VersionPrefix>1.1.0</VersionPrefix>
<VersionPrefix>1.2.0</VersionPrefix>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>NLog.Targets.Seq</AssemblyName>
<AssemblyOriginatorKeyFile>../../asset/nlog-targets-seq.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageId>NLog.Targets.Seq</PackageId>
<PackageTags>seq;nlog</PackageTags>
<PackageIconUrl>https://raw.githubusercontent.com/datalust/nlog-targets-seq/dev/asset/nlog-targets-seq.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/datalust/nlog-targets-seq</PackageProjectUrl>
<PackageLicenseUrl>https://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/datalust/nlog-targets-seq.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE" Link="LICENSE" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NLog" Version="4.5.11" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/NLog.Targets.Seq/RenderingsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace NLog.Targets.Seq
{
[ThreadSafe]
class RenderingsLayout : Layout
{
IJsonConverter _jsonConverter;
Expand Down
55 changes: 43 additions & 12 deletions src/NLog.Targets.Seq/SeqTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public sealed class SeqTarget : Target
/// </summary>
public JsonLayout TextClefLayout { get; } = new CompactJsonLayout(false);

/// <summary>
/// Maximum size allowed for JSON payload sent to Seq-Server. Discards logevents that are larger than limit.
/// </summary>
public int JsonPayloadMaxLength { get; set; }

/// <summary>
/// Initializes the target.
/// </summary>
Expand All @@ -49,6 +54,7 @@ public SeqTarget()
Properties = new List<SeqPropertyItem>();
MaxRecursionLimit = 0; // Default behavior for Serilog
OptimizeBufferReuse = true;
JsonPayloadMaxLength = 128 * 1024;
}

/// <summary>
Expand Down Expand Up @@ -131,13 +137,10 @@ protected override void Write(IList<AsyncLogEventInfo> logEvents)
try
{
PostBatch(logEvents);

for (int i = 0; i < logEvents.Count; ++i)
logEvents[i].Continuation(null);
}
catch (Exception ex)
{
InternalLogger.Error(ex, "Seq(Name={0}): Failed sending LogEvents", Name);
InternalLogger.Error(ex, "Seq(Name={0}): Failed sending LogEvents. Uri={1}", Name, _webRequestUri);
if (LogManager.ThrowExceptions)
throw;

Expand All @@ -156,11 +159,10 @@ protected override void Write(AsyncLogEventInfo logEvent)
try
{
PostBatch(new[] { logEvent });
logEvent.Continuation(null);
}
catch (Exception ex)
{
InternalLogger.Error(ex, "Seq(Name={0}): Failed sending LogEvent", Name);
InternalLogger.Error(ex, "Seq(Name={0}): Failed sending LogEvents. Uri={1}", Name, _webRequestUri);
if (LogManager.ThrowExceptions)
throw;

Expand All @@ -181,13 +183,33 @@ void PostBatch(IList<AsyncLogEventInfo> logEvents)
if (!string.IsNullOrWhiteSpace(_headerApiKey))
request.Headers.Add(ApiKeyHeaderName, _headerApiKey);

using (var requestStream = request.GetRequestStream())
using (var payload = new StreamWriter(requestStream))
List<AsyncLogEventInfo> extraBatch = null;
int totalPayload = 0;
using (var payload = new StreamWriter(request.GetRequestStream()))
{
for (int i = 0; i < logEvents.Count; ++i)
{
var evt = logEvents[i];
RenderCompactJsonLine(evt.LogEvent, payload);
var evt = logEvents[i].LogEvent;
var json = RenderCompactJsonLine(evt);

if (JsonPayloadMaxLength > 0)
{
if (json.Length > JsonPayloadMaxLength)
{
InternalLogger.Warn("Seq(Name={0}): Event JSON representation exceeds the char limit: {1} > {2}", Name, json.Length, JsonPayloadMaxLength);
continue;
}
if (totalPayload + json.Length > JsonPayloadMaxLength)
{
extraBatch = new List<AsyncLogEventInfo>(logEvents.Count - i);
for (; i < logEvents.Count; ++i)
extraBatch.Add(logEvents[i]);
break;
}
}

totalPayload += json.Length;
payload.WriteLine(json);
}
}

Expand All @@ -206,13 +228,22 @@ void PostBatch(IList<AsyncLogEventInfo> logEvents)
}
}
}

var completedCount = logEvents.Count - (extraBatch?.Count ?? 0);
for (int i = 0; i < completedCount; ++i)
logEvents[i].Continuation(null);

if (extraBatch != null)
{
PostBatch(extraBatch);
}
}

internal void RenderCompactJsonLine(LogEventInfo evt, TextWriter output)
internal string RenderCompactJsonLine(LogEventInfo evt)
{
bool hasProperties = evt.HasProperties && evt.Properties.Count > 0;
var json = RenderLogEvent(hasProperties ? TemplatedClefLayout : TextClefLayout, evt);
output.WriteLine(json);
return json;
}

internal void TestInitialize()
Expand Down
3 changes: 2 additions & 1 deletion test/NLog.Targets.Seq.Tests/SeqTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ static void ToCompactJson(LogEventInfo evt, TextWriter output, IEnumerable<SeqPr

target.TestInitialize();

target.RenderCompactJsonLine(evt, output);
var json = target.RenderCompactJsonLine(evt);
output.WriteLine(json);
}

JObject AssertValidJson(Action<ILogger> act, IEnumerable<SeqPropertyItem> properties = null, int? maxRecursionLimit = null)
Expand Down

0 comments on commit 6102581

Please sign in to comment.