Skip to content

Commit

Permalink
Merge pull request #73 from datalust/dev
Browse files Browse the repository at this point in the history
3.1.0 Release
  • Loading branch information
nblumhardt authored Jan 10, 2024
2 parents ef72208 + d7d67ed commit 463de94
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 97 deletions.
3 changes: 2 additions & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"sdk": {
"version": "6.0.408"
"version": "6.0.100",
"rollForward": "latestMinor"
}
}
10 changes: 5 additions & 5 deletions nlog-targets-seq.sln
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29209.62
# Visual Studio Version 17
VisualStudioVersion = 17.5.33627.172
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AC295EEA-D319-4146-97E0-B978DF6F2557}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "global", "global", "{66B005E9-A083-41E8-BD89-4D6E753CD8BF}"
ProjectSection(SolutionItems) = preProject
.gitattributes = .gitattributes
.gitignore = .gitignore
appveyor.yml = appveyor.yml
Build.ps1 = Build.ps1
global.json = global.json
LICENSE = LICENSE
README.md = README.md
.gitattributes = .gitattributes
.gitignore = .gitignore
global.json = global.json
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2ED926D3-7AC8-4BFD-A16B-74D942602968}"
Expand Down
26 changes: 10 additions & 16 deletions src/NLog.Targets.Seq/CompactJsonLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,30 @@
// limitations under the License.

using System;
using NLog.Config;
using NLog.Layouts;

namespace NLog.Targets.Seq
{
[ThreadAgnostic]
class CompactJsonLayout : JsonLayout
{
readonly JsonAttribute
_timestampAttribute = new JsonAttribute("@t", new SimpleLayout("${date:o}")),
_timestampAttribute = new JsonAttribute("@t", new SimpleLayout("${date:format=o}")),
_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}"));
_messageAttribute = new JsonAttribute("@m", new FormattedMessageLayout()),
_messageTemplateAttribute = new JsonAttribute("@mt", new SimpleLayout("${onhasproperties:${message:raw=true}}"));

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

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

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

IncludeEventProperties = true;
IncludeScopeProperties = true;
Expand Down
53 changes: 53 additions & 0 deletions src/NLog.Targets.Seq/FormattedMessageLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Seq Target for NLog - Copyright 2014-2017 Datalust and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

namespace NLog.Targets.Seq
{
[ThreadAgnostic]
class FormattedMessageLayout : Layout
{
protected override void RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target)
{
target.Append(GetFormattedMessage(logEvent));
}

protected override string GetFormattedMessage(LogEventInfo logEvent)
{
if (HasMessageTemplateSyntax(logEvent))
{
return string.Empty; // Message Template Syntax, no need to include formatted message
}

return logEvent.FormattedMessage;
}

bool HasMessageTemplateSyntax(LogEventInfo logEvent)
{
if (!logEvent.HasProperties)
return false;

if (logEvent.Message?.IndexOf("{0", System.StringComparison.Ordinal) >= 0)
{
var mtp = logEvent.MessageTemplateParameters;
return !mtp.IsPositional;
}

return true;
}
}
}
8 changes: 4 additions & 4 deletions src/NLog.Targets.Seq/NLog.Targets.Seq.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>An NLog target that writes structured log events to Seq</Description>
<Authors>Datalust;Contributors</Authors>
<VersionPrefix>3.0.0</VersionPrefix>
<TargetFrameworks>net45;net462;netstandard2.0;net5.0;net6.0</TargetFrameworks>
<VersionPrefix>3.1.0</VersionPrefix>
<TargetFrameworks>net45;net462;netstandard2.0;net6.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyOriginatorKeyFile>../../asset/nlog-targets-seq.snk</AssemblyOriginatorKeyFile>
Expand All @@ -31,7 +31,7 @@
</ItemGroup>

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

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' or '$(TargetFramework)' == 'net462' ">
Expand Down
55 changes: 30 additions & 25 deletions src/NLog.Targets.Seq/RenderingsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
// 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
{
[ThreadAgnostic]
class RenderingsLayout : Layout
{
readonly Lazy<IJsonConverter> _jsonConverter;
Expand All @@ -31,15 +32,13 @@ public RenderingsLayout(Lazy<IJsonConverter> jsonConverter)

protected override void RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target)
{
var result = RenderLogEvent(logEvent, target);
if (result == null)
target.Append("null");
RenderLogEvent(logEvent, target);
}

protected override string GetFormattedMessage(LogEventInfo logEvent)
{
var result = RenderLogEvent(logEvent);
return result?.ToString() ?? "null";
return result?.ToString() ?? "";
}

StringBuilder RenderLogEvent(LogEventInfo logEvent, StringBuilder preallocated = null)
Expand All @@ -49,38 +48,31 @@ StringBuilder RenderLogEvent(LogEventInfo logEvent, StringBuilder preallocated =

try
{
if (!logEvent.HasProperties)
return null;

var nextDelimiter = "";
var mtp = logEvent.MessageTemplateParameters;
if (mtp.IsPositional || mtp.Count == 0)
return null;

foreach (var parameter in mtp)
{
if (parameter.Format == null) continue;

if (string.IsNullOrEmpty(parameter.Format)) continue;

if (!logEvent.Properties.TryGetValue(parameter.Name, out var value)) continue;

if (output == null)
{
output = preallocated ?? new StringBuilder();
output.Append("[");
output.Append('[');
}

var space = new StringWriter();

if (logEvent.Properties != null &&
logEvent.Properties.TryGetValue(parameter.Name, out var value))
{
if (parameter.CaptureType == CaptureType.Normal)
{
var formatString = string.Concat("{0:", parameter.Format, "}");
space.Write(formatString, value);
}
else
{
space.Write(value);
}
}
string formattedValue = FormatToString(parameter, value);

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

return output;
Expand All @@ -94,7 +86,20 @@ StringBuilder RenderLogEvent(LogEventInfo logEvent, StringBuilder preallocated =
}
finally
{
output?.Append("]");
output?.Append(']');
}
}

private static string FormatToString(MessageTemplateParameter parameter, object value)
{
if (parameter.CaptureType == CaptureType.Normal)
{
var formatString = string.Concat("{0:", parameter.Format, "}");
return string.Format(formatString, value);
}
else
{
return Convert.ToString(value);
}
}
}
Expand Down
Loading

0 comments on commit 463de94

Please sign in to comment.