From b303dd543522b0be651588067ddcec7770d13a22 Mon Sep 17 00:00:00 2001 From: David Noakes Date: Tue, 23 Jul 2024 14:29:04 +0200 Subject: [PATCH 1/3] Added support for pushing content of serilog Tags property via exceptionless client --- .../ExceptionlessClientExtensions.cs | 19 +++++++++++++++++++ .../Sinks/Exceptionless/ExceptionlessSink.cs | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessClientExtensions.cs b/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessClientExtensions.cs index 5e7d735..1c3095a 100644 --- a/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessClientExtensions.cs +++ b/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessClientExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using Exceptionless; using Exceptionless.Logging; using Serilog.Core; @@ -36,6 +37,24 @@ internal static string GetSource(this LogEvent log) { return null; } + internal static string[] GetTags(this LogEvent log) + { + if (log.Properties.TryGetValue("Tags", out LogEventPropertyValue value)) + { + var propertyCollection = value.FlattenProperties() as List; + if (propertyCollection == null) return Array.Empty(); + + List tags = new List(); + foreach (var item in propertyCollection) + { + tags.Add(item.ToString()); + } + + return tags.ToArray(); + } + return Array.Empty(); + } + internal static LogLevel GetLevel(this LogEventLevel log) { switch (log) diff --git a/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessSink.cs b/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessSink.cs index f4b043a..a9afc41 100644 --- a/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessSink.cs +++ b/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessSink.cs @@ -108,7 +108,7 @@ public void Emit(LogEvent logEvent) { if (logEvent.Level.GetLevel() < minLogLevel) return; - var builder = _client.CreateFromLogEvent(logEvent).AddTags(_defaultTags); + var builder = _client.CreateFromLogEvent(logEvent).AddTags(_defaultTags).AddTags(logEvent.GetTags()); if (_includeProperties) { foreach (var prop in logEvent.Properties) From c68cc0c3bdbb4b8d40ed3a6e4fd4f40e66ae43bb Mon Sep 17 00:00:00 2001 From: David Noakes Date: Wed, 24 Jul 2024 08:20:24 +0200 Subject: [PATCH 2/3] Moved getTags extension to LogEventPropertyValue and moved to the existing iteration through properties --- .../ExceptionlessClientExtensions.cs | 22 ++++++++----------- .../Sinks/Exceptionless/ExceptionlessSink.cs | 5 ++++- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessClientExtensions.cs b/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessClientExtensions.cs index 1c3095a..141ba2a 100644 --- a/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessClientExtensions.cs +++ b/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessClientExtensions.cs @@ -37,22 +37,18 @@ internal static string GetSource(this LogEvent log) { return null; } - internal static string[] GetTags(this LogEvent log) + internal static string[] GetTags(this LogEventPropertyValue value) { - if (log.Properties.TryGetValue("Tags", out LogEventPropertyValue value)) - { - var propertyCollection = value.FlattenProperties() as List; - if (propertyCollection == null) return Array.Empty(); - - List tags = new List(); - foreach (var item in propertyCollection) - { - tags.Add(item.ToString()); - } + var propertyCollection = value.FlattenProperties() as List; + if (propertyCollection == null) return Array.Empty(); - return tags.ToArray(); + List tags = new List(); + foreach (var item in propertyCollection) + { + tags.Add(item.ToString()); } - return Array.Empty(); + + return tags.ToArray(); } internal static LogLevel GetLevel(this LogEventLevel log) diff --git a/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessSink.cs b/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessSink.cs index a9afc41..21dc1c0 100644 --- a/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessSink.cs +++ b/src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessSink.cs @@ -108,7 +108,7 @@ public void Emit(LogEvent logEvent) { if (logEvent.Level.GetLevel() < minLogLevel) return; - var builder = _client.CreateFromLogEvent(logEvent).AddTags(_defaultTags).AddTags(logEvent.GetTags()); + var builder = _client.CreateFromLogEvent(logEvent).AddTags(_defaultTags); if (_includeProperties) { foreach (var prop in logEvent.Properties) @@ -139,6 +139,9 @@ public void Emit(LogEvent logEvent) { if (!String.IsNullOrWhiteSpace(emailAddress) || !String.IsNullOrWhiteSpace(description)) builder.SetUserDescription(emailAddress, description); break; + case "Tags": + builder.AddTags(prop.Value.GetTags()); + break; default: builder.SetProperty(prop.Key, prop.Value.FlattenProperties()); break; From e0994dc904a686decde743de6eb7b5523e0f8695 Mon Sep 17 00:00:00 2001 From: David Noakes Date: Wed, 24 Jul 2024 08:35:49 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 7c98b60..2e27082 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,19 @@ Log.Logger = new LoggerConfiguration() .CreateLogger(); ``` +To get tags to populate on the exceptionless UI, add a `Tags` string enumerable to any log. + +```Example with Serilog: +Serilog: Log.ForContext("Tags", new List() { "Tag1", "Tag2"}).Information("Seri info"); +``` + +```Example with ILogger +using (var scope = _logger.BeginScope(new Dictionary { ["Tags"] = new string[] { "Tag1", "Tag2" }})) +{ +_logger.Log(logLevel, eventId, state, exception, formatter); +} +``` + * [Documentation](https://github.com/serilog/serilog/wiki) Copyright © 2023 Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html).