Skip to content

Commit

Permalink
Merge pull request datalust#66 from datalust/dev
Browse files Browse the repository at this point in the history
3.0.0 Release
  • Loading branch information
nblumhardt authored May 3, 2023
2 parents 90254cf + 637de7a commit ef72208
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 93 deletions.
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NLog.Targets.Seq [![NuGet Pre Release](https://img.shields.io/nuget/vpre/NLog.Targets.Seq.svg)](https://nuget.org/packages/NLog.Targets.Seq) [![Build status](https://ci.appveyor.com/api/projects/status/o22e6dq0mkftaggc?svg=true)](https://ci.appveyor.com/project/datalust/nlog-targets-seq) [![Join the chat at https://gitter.im/datalust/seq](https://img.shields.io/gitter/room/datalust/seq.svg)](https://gitter.im/datalust/seq)

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.
An NLog target that writes events to [Seq](https://datalust.co/seq). The target takes full advantage of the structured logging support in **NLog 4.5** to provide hassle-free filtering, searching and analysis.

### Getting started

Expand All @@ -13,6 +13,10 @@ dotnet add package NLog.Targets.Seq
Then, add the target and rules entries to your NLog configuration:

```xml
<nlog>
<extensions>
<add assembly="NLog.Targets.Seq"/>
</extensions>
<targets>
<target name="seq" xsi:type="BufferingWrapper" bufferSize="1000"
flushTimeout="2000" slidingTimeout="false">
Expand All @@ -22,6 +26,7 @@ Then, add the target and rules entries to your NLog configuration:
<rules>
<logger name="*" minlevel="Info" writeTo="seq" />
</rules>
</nlog>
```

The `BufferingWrapper` ensures that writes to Seq do not block the application.
Expand Down Expand Up @@ -65,6 +70,54 @@ The `target` declaration in _NLog.config_ can be expanded with additional proper

Any properties specified here will be attached to all outgoing events. You can see examples of `ThreadId` and `MachineName` in the screenshot above. The value can be any supported [layout renderer](https://github.com/NLog/NLog/wiki/Layout-Renderers).

### Configuration in appsettings.json

NLog.Extensions.Logging ver. 1.5.0 adds support for having [NLog configuration in appsettings.json](https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-configuration-with-appsettings.json)

```json
{
"NLog": {
"throwConfigExceptions": true,
"extensions": [
{ "assembly": "NLog.Targets.Seq" }
],
"targets": {
"seq": {
"type": "BufferingWrapper",
"bufferSize": 200,
"flushTimeout": 2000,
"slidingTimeout": false,
"target": {
"type": "Seq",
"serverUrl": "http://localhost:5341",
"apiKey": "",
"properties": [
{
"name": "Source",
"value": "${Logger}"
},
{
"name": "ThreadId",
"value": "${ThreadId}",
"as": "number"
},
{
"name": "MachineName",
"value": "${MachineName}"
}]
}
}
},
"rules": [
{
"logger": "*",
"minLevel": "Info",
"writeTo": "seq"
}]
}
}
```

### Acknowledgements

The target is based on the earlier [_Seq.Client.NLog_ project](https://github.com/datalust/seq-client), and benefits from many contributions accepted into that repository.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2019
image: Visual Studio 2022
install:
- ps: mkdir -Force ".\build\" | Out-Null
- ps: nuget update -Self
Expand All @@ -12,7 +12,7 @@ artifacts:
deploy:
- provider: NuGet
api_key:
secure: 71c2lLlQfEUp7lWA9Zo+BneSkHO5xuZVeIt+bqmqlzHg4g3NgVf0Buwp4pAhoGO5
secure: EWuj9Uh1xsnPDf7JZDzW4XiQJ4+If7OjsOmw/b3dvRUroNB+2CdqnXAWM2vkOPL2
skip_symbols: true
on:
branch: /^(main|dev)$/
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "5.0.401"
"version": "6.0.408"
}
}
7 changes: 1 addition & 6 deletions sample/Example/Example.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AssemblyName>Example</AssemblyName>
<OutputType>Exe</OutputType>
Expand All @@ -21,9 +21,4 @@
<ProjectReference Include="..\..\src\NLog.Targets.Seq\NLog.Targets.Seq.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

</Project>
19 changes: 0 additions & 19 deletions sample/Example/Properties/AssemblyInfo.cs

This file was deleted.

15 changes: 8 additions & 7 deletions src/NLog.Targets.Seq/CompactJsonLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using NLog.Layouts;

namespace NLog.Targets.Seq
Expand All @@ -23,28 +24,28 @@ readonly JsonAttribute
_levelAttribute = new JsonAttribute("@l", new SimpleLayout("${level}")),
_exceptionAttribute = new JsonAttribute("@x", new SimpleLayout("${exception:format=toString}")),
_messageAttribute = new JsonAttribute("@m", new SimpleLayout("${message}")),
_messageTemplateAttribute = new JsonAttribute("@mt", new SimpleLayout("${message:raw=true}")),
_renderingsAttribute = new JsonAttribute("@r", new RenderingsLayout(), encode: false);
_messageTemplateAttribute = new JsonAttribute("@mt", new SimpleLayout("${message:raw=true}"));

public CompactJsonLayout(bool usesTemplate)
{
Attributes.Add(_timestampAttribute);
Attributes.Add(_levelAttribute);
Attributes.Add(_exceptionAttribute);

if (usesTemplate)
{
Attributes.Add(_messageTemplateAttribute);
Attributes.Add(_renderingsAttribute);

var renderingsAttribute = new JsonAttribute("@r", new RenderingsLayout(new Lazy<IJsonConverter>(ResolveService<IJsonConverter>)), encode: false);
Attributes.Add(renderingsAttribute);
}
else
{
Attributes.Add(_messageAttribute);
}

IncludeAllProperties = true;
IncludeMdc = true;
IncludeMdlc = true;
IncludeEventProperties = true;
IncludeScopeProperties = true;
SuppressSpaces = true;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/NLog.Targets.Seq/NLog.Targets.Seq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<Description>An NLog target that writes structured log events to Seq</Description>
<Authors>Datalust;Contributors</Authors>
<VersionPrefix>2.1.0</VersionPrefix>
<TargetFrameworks>net4.5;net4.6.2;netstandard2.0;net5.0</TargetFrameworks>
<VersionPrefix>3.0.0</VersionPrefix>
<TargetFrameworks>net45;net462;netstandard2.0;net5.0;net6.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyOriginatorKeyFile>../../asset/nlog-targets-seq.snk</AssemblyOriginatorKeyFile>
Expand All @@ -17,6 +17,7 @@
<RepositoryUrl>https://github.com/datalust/nlog-targets-seq.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -30,10 +31,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NLog" Version="4.7.8" />
<PackageReference Include="NLog" Version="5.1.4" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'net5.0' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' or '$(TargetFramework)' == 'net462' ">
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
Expand Down
14 changes: 8 additions & 6 deletions src/NLog.Targets.Seq/RenderingsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.IO;
using System.Text;
using NLog.Config;
using NLog.Layouts;
using NLog.MessageTemplates;

namespace NLog.Targets.Seq
{
[ThreadSafe]
class RenderingsLayout : Layout
{
IJsonConverter _jsonConverter;
IJsonConverter JsonConverter => _jsonConverter ?? (_jsonConverter = ConfigurationItemFactory.Default.JsonConverter);
readonly Lazy<IJsonConverter> _jsonConverter;

public RenderingsLayout(Lazy<IJsonConverter> jsonConverter)
{
_jsonConverter = jsonConverter;
}

protected override void RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target)
{
Expand Down Expand Up @@ -77,7 +80,7 @@ StringBuilder RenderLogEvent(LogEventInfo logEvent, StringBuilder preallocated =

output.Append(nextDelimiter);
nextDelimiter = ",";
JsonConverter.SerializeObject(space.ToString(), output);
_jsonConverter.Value.SerializeObject(space.ToString(), output);
}

return output;
Expand All @@ -94,6 +97,5 @@ StringBuilder RenderLogEvent(LogEventInfo logEvent, StringBuilder preallocated =
output?.Append("]");
}
}

}
}
25 changes: 13 additions & 12 deletions src/NLog.Targets.Seq/SeqPropertyItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ namespace NLog.Targets.Seq
[NLogConfigurationItem]
public sealed class SeqPropertyItem
{
/// <summary>
/// Initialize parameter defaults.
/// </summary>
public SeqPropertyItem()
{
As = "string";
}

/// <summary>
/// The name of the property.
/// </summary>
Expand All @@ -43,14 +35,23 @@ public SeqPropertyItem()
[RequiredParameter]
public Layout Value { get; set; }

/// <summary>
/// Gets or sets whether value should be handled as string-value.
/// </summary>
/// <remarks>
/// Matches <see cref="NLog.Layouts.JsonAttribute.Encode"/>
/// </remarks>
public bool AsString { get; set; } = true;

/// <summary>
/// Either "string", which is the default, or "number", which
/// will cause values of this type to be converted to numbers for
/// storage.
/// </summary>
[RequiredParameter]
public string As { get; set; }

internal bool IsNumber => As == "number";
public string As
{
get => AsString ? "string" : "number";
set => AsString = value != "number";
}
}
}
3 changes: 1 addition & 2 deletions src/NLog.Targets.Seq/SeqTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public SeqTarget()
{
Properties = new List<SeqPropertyItem>();
MaxRecursionLimit = 0; // Default behavior for Serilog
OptimizeBufferReuse = true;
JsonPayloadMaxLength = 128 * 1024;
}

Expand All @@ -116,7 +115,7 @@ protected override void InitializeTarget()
{
foreach (var prop in Properties)
{
var attr = new JsonAttribute(prop.Name, prop.Value, !prop.IsNumber);
var attr = new JsonAttribute(prop.Name, prop.Value, prop.AsString);
TextClefLayout.Attributes.Add(attr);
TemplatedClefLayout.Attributes.Add(attr);
}
Expand Down
24 changes: 10 additions & 14 deletions test/NLog.Targets.Seq.Tests/NLog.Targets.Seq.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;net5.0</TargetFrameworks>
<AssemblyName>NLog.Targets.Seq.Tests</AssemblyName>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<AssemblyOriginatorKeyFile>../../asset/nlog-targets-seq.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageId>NLog.Targets.Seq.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<TreatSpecificWarningsAsErrors />
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\NLog.Targets.Seq\NLog.Targets.Seq.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="System" />

<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<!-- This works around occurrences of Error CS0656 : Missing compiler required member
'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create' where `dynamic` is used. -->
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

Expand Down
19 changes: 0 additions & 19 deletions test/NLog.Targets.Seq.Tests/Properties/AssemblyInfo.cs

This file was deleted.

0 comments on commit ef72208

Please sign in to comment.