diff --git a/README.md b/README.md index 2e0bf71..c2a5123 100644 --- a/README.md +++ b/README.md @@ -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 @@ -13,6 +13,10 @@ dotnet add package NLog.Targets.Seq Then, add the target and rules entries to your NLog configuration: ```xml + + + + @@ -22,6 +26,7 @@ Then, add the target and rules entries to your NLog configuration: + ``` The `BufferingWrapper` ensures that writes to Seq do not block the application. @@ -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. diff --git a/appveyor.yml b/appveyor.yml index c6cf943..f1356e6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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 @@ -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)$/ diff --git a/global.json b/global.json index 6e34824..dfa39aa 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "5.0.401" + "version": "6.0.408" } } \ No newline at end of file diff --git a/sample/Example/Example.csproj b/sample/Example/Example.csproj index b645af2..05036d6 100644 --- a/sample/Example/Example.csproj +++ b/sample/Example/Example.csproj @@ -1,7 +1,7 @@ - net46 + net472;net6.0 true Example Exe @@ -21,9 +21,4 @@ - - - - - diff --git a/sample/Example/Properties/AssemblyInfo.cs b/sample/Example/Properties/AssemblyInfo.cs deleted file mode 100644 index a5739cb..0000000 --- a/sample/Example/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Sample")] -[assembly: AssemblyTrademark("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("34bbd428-8297-484e-b771-0b72c172c264")] diff --git a/src/NLog.Targets.Seq/CompactJsonLayout.cs b/src/NLog.Targets.Seq/CompactJsonLayout.cs index 89e0a5b..ebfc229 100644 --- a/src/NLog.Targets.Seq/CompactJsonLayout.cs +++ b/src/NLog.Targets.Seq/CompactJsonLayout.cs @@ -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 @@ -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(ResolveService)), encode: false); + Attributes.Add(renderingsAttribute); } else { Attributes.Add(_messageAttribute); } - IncludeAllProperties = true; - IncludeMdc = true; - IncludeMdlc = true; + IncludeEventProperties = true; + IncludeScopeProperties = true; SuppressSpaces = true; } } diff --git a/src/NLog.Targets.Seq/NLog.Targets.Seq.csproj b/src/NLog.Targets.Seq/NLog.Targets.Seq.csproj index 98f2fb0..8aad972 100644 --- a/src/NLog.Targets.Seq/NLog.Targets.Seq.csproj +++ b/src/NLog.Targets.Seq/NLog.Targets.Seq.csproj @@ -3,8 +3,8 @@ An NLog target that writes structured log events to Seq Datalust;Contributors - 2.1.0 - net4.5;net4.6.2;netstandard2.0;net5.0 + 3.0.0 + net45;net462;netstandard2.0;net5.0;net6.0 true true ../../asset/nlog-targets-seq.snk @@ -17,6 +17,7 @@ https://github.com/datalust/nlog-targets-seq.git git false + 8.0 @@ -30,10 +31,10 @@ - + - + diff --git a/src/NLog.Targets.Seq/RenderingsLayout.cs b/src/NLog.Targets.Seq/RenderingsLayout.cs index 1eb162d..d13777d 100644 --- a/src/NLog.Targets.Seq/RenderingsLayout.cs +++ b/src/NLog.Targets.Seq/RenderingsLayout.cs @@ -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 _jsonConverter; + + public RenderingsLayout(Lazy jsonConverter) + { + _jsonConverter = jsonConverter; + } protected override void RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target) { @@ -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; @@ -94,6 +97,5 @@ StringBuilder RenderLogEvent(LogEventInfo logEvent, StringBuilder preallocated = output?.Append("]"); } } - } } diff --git a/src/NLog.Targets.Seq/SeqPropertyItem.cs b/src/NLog.Targets.Seq/SeqPropertyItem.cs index bee0495..297b6c6 100644 --- a/src/NLog.Targets.Seq/SeqPropertyItem.cs +++ b/src/NLog.Targets.Seq/SeqPropertyItem.cs @@ -23,14 +23,6 @@ namespace NLog.Targets.Seq [NLogConfigurationItem] public sealed class SeqPropertyItem { - /// - /// Initialize parameter defaults. - /// - public SeqPropertyItem() - { - As = "string"; - } - /// /// The name of the property. /// @@ -43,14 +35,23 @@ public SeqPropertyItem() [RequiredParameter] public Layout Value { get; set; } + /// + /// Gets or sets whether value should be handled as string-value. + /// + /// + /// Matches + /// + public bool AsString { get; set; } = true; + /// /// Either "string", which is the default, or "number", which /// will cause values of this type to be converted to numbers for /// storage. /// - [RequiredParameter] - public string As { get; set; } - - internal bool IsNumber => As == "number"; + public string As + { + get => AsString ? "string" : "number"; + set => AsString = value != "number"; + } } } \ No newline at end of file diff --git a/src/NLog.Targets.Seq/SeqTarget.cs b/src/NLog.Targets.Seq/SeqTarget.cs index 459dc52..f670faa 100644 --- a/src/NLog.Targets.Seq/SeqTarget.cs +++ b/src/NLog.Targets.Seq/SeqTarget.cs @@ -104,7 +104,6 @@ public SeqTarget() { Properties = new List(); MaxRecursionLimit = 0; // Default behavior for Serilog - OptimizeBufferReuse = true; JsonPayloadMaxLength = 128 * 1024; } @@ -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); } diff --git a/test/NLog.Targets.Seq.Tests/NLog.Targets.Seq.Tests.csproj b/test/NLog.Targets.Seq.Tests/NLog.Targets.Seq.Tests.csproj index e3441e4..2fc9a89 100644 --- a/test/NLog.Targets.Seq.Tests/NLog.Targets.Seq.Tests.csproj +++ b/test/NLog.Targets.Seq.Tests/NLog.Targets.Seq.Tests.csproj @@ -1,18 +1,13 @@  - net462;net5.0 - NLog.Targets.Seq.Tests + net472;net6.0 ../../asset/nlog-targets-seq.snk true true - NLog.Targets.Seq.Tests - true - false - false - false True + 8.0 @@ -20,14 +15,15 @@ - - - - + + + + - - - + + + diff --git a/test/NLog.Targets.Seq.Tests/Properties/AssemblyInfo.cs b/test/NLog.Targets.Seq.Tests/Properties/AssemblyInfo.cs deleted file mode 100644 index 2f38d3f..0000000 --- a/test/NLog.Targets.Seq.Tests/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Superpower.Tests")] -[assembly: AssemblyTrademark("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("cd473266-4aed-4207-89fd-0b185239f1c7")]