From 8a13a80b720bdfa375548bbd8ebcd7265deea634 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 20 Dec 2022 11:29:02 +0100 Subject: [PATCH] dev: pass sessionid as parameter --- .../Enrichers/SessionIdEnricher.cs | 8 +++++++- .../EnrichersConfigurationExtensions.cs | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/nightly.serilog.xamarin/nightly.serilog.xamarin/Enrichers/SessionIdEnricher.cs b/nightly.serilog.xamarin/nightly.serilog.xamarin/Enrichers/SessionIdEnricher.cs index 67b4b6e..10701fd 100644 --- a/nightly.serilog.xamarin/nightly.serilog.xamarin/Enrichers/SessionIdEnricher.cs +++ b/nightly.serilog.xamarin/nightly.serilog.xamarin/Enrichers/SessionIdEnricher.cs @@ -7,10 +7,16 @@ namespace nightly.serilog.xamarin.Enrichers public class SessionIdEnricher : ILogEventEnricher { private LogEventProperty _cachedProperty; + private readonly string _sessionId; + + public SessionIdEnricher(string sessionId = null) + { + this._sessionId = sessionId ?? Guid.NewGuid().ToString("N"); + } public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { - this._cachedProperty ??= propertyFactory.CreateProperty("SessionId",Guid.NewGuid().ToString("N")); + this._cachedProperty ??= propertyFactory.CreateProperty("SessionId",this._sessionId); logEvent.AddPropertyIfAbsent(this._cachedProperty); } } diff --git a/nightly.serilog.xamarin/nightly.serilog.xamarin/EnrichersConfigurationExtensions.cs b/nightly.serilog.xamarin/nightly.serilog.xamarin/EnrichersConfigurationExtensions.cs index bbd09f7..4a51b46 100644 --- a/nightly.serilog.xamarin/nightly.serilog.xamarin/EnrichersConfigurationExtensions.cs +++ b/nightly.serilog.xamarin/nightly.serilog.xamarin/EnrichersConfigurationExtensions.cs @@ -42,10 +42,10 @@ public static LoggerConfiguration WithDeviceId(this LoggerEnrichmentConfiguratio /// /// /// - public static LoggerConfiguration WithSessionId(this LoggerEnrichmentConfiguration enrichment) + public static LoggerConfiguration WithSessionId(this LoggerEnrichmentConfiguration enrichment, string sessionId = null) { if (enrichment == null) throw new ArgumentNullException(nameof(enrichment)); - return enrichment.With(); + return enrichment.With(new SessionIdEnricher(sessionId)); } ///