From 5f0e1d9ddbfc4c48cd2a5f9afa3a63762125588f Mon Sep 17 00:00:00 2001 From: Victor Usoltsev Date: Wed, 5 Dec 2018 23:04:32 +1300 Subject: [PATCH] Adds IdentityProvider into settings. --- .../AppInsightsAbstractLogger.cs | 4 ++-- .../Vima.LoggingAbstractor.Core/AbstractLoggerBase.cs | 11 +++++++++-- .../AbstractLoggerSettings.cs | 11 ++++++++++- .../Parameters/LoggingIdentityParameter.cs | 9 +++++++++ .../RaygunAbstractLogger.cs | 4 ++-- .../SentryAbstractLogger.cs | 4 ++-- 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Source/Vima.LoggingAbstractor.AppInsights/AppInsightsAbstractLogger.cs b/Source/Vima.LoggingAbstractor.AppInsights/AppInsightsAbstractLogger.cs index 2e23aa4..f647df9 100644 --- a/Source/Vima.LoggingAbstractor.AppInsights/AppInsightsAbstractLogger.cs +++ b/Source/Vima.LoggingAbstractor.AppInsights/AppInsightsAbstractLogger.cs @@ -54,7 +54,7 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn return; } - var allParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList(); + var allParameters = GetGlobalAndLocalLoggingParameters(parameters); var traceTelemetry = new TraceTelemetry(message) { SeverityLevel = LoggingLevelMapper.ConvertLoggingLevelToSeverityLevel(loggingLevel) @@ -77,7 +77,7 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev return; } - var allParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList(); + var allParameters = GetGlobalAndLocalLoggingParameters(parameters); var exceptionTelemetry = new ExceptionTelemetry(exception) { SeverityLevel = LoggingLevelMapper.ConvertLoggingLevelToSeverityLevel(loggingLevel) diff --git a/Source/Vima.LoggingAbstractor.Core/AbstractLoggerBase.cs b/Source/Vima.LoggingAbstractor.Core/AbstractLoggerBase.cs index a20e594..205a8e4 100644 --- a/Source/Vima.LoggingAbstractor.Core/AbstractLoggerBase.cs +++ b/Source/Vima.LoggingAbstractor.Core/AbstractLoggerBase.cs @@ -95,9 +95,16 @@ protected bool ShouldBeTraced(LoggingLevel loggingLevel) /// /// The logging parameters that are explicitly specified by the user on logging call. /// All parameters to be logged. - protected IEnumerable GetGlobalAndLocalLoggingParameters(IEnumerable localParameters) + protected IList GetGlobalAndLocalLoggingParameters(IEnumerable localParameters) { - var globalAndLocalParameters = localParameters.Concat(_settings.GlobalLoggingParameters); + var globalAndLocalParameters = localParameters.Concat(_settings.GlobalLoggingParameters).ToList(); + + var globalIdentityParameter = _settings.IdentityProvider?.Invoke(); + if (globalIdentityParameter != null) + { + globalAndLocalParameters.Add(new LoggingIdentityParameter(globalIdentityParameter)); + } + return globalAndLocalParameters; } } diff --git a/Source/Vima.LoggingAbstractor.Core/AbstractLoggerSettings.cs b/Source/Vima.LoggingAbstractor.Core/AbstractLoggerSettings.cs index 041aec5..09860b9 100644 --- a/Source/Vima.LoggingAbstractor.Core/AbstractLoggerSettings.cs +++ b/Source/Vima.LoggingAbstractor.Core/AbstractLoggerSettings.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Vima.LoggingAbstractor.Core.Parameters; namespace Vima.LoggingAbstractor.Core @@ -29,5 +30,13 @@ public IEnumerable GlobalLoggingParameters get => _globalLoggingParameters ?? (_globalLoggingParameters = new List()); set => _globalLoggingParameters = value; } + + /// + /// Gets or sets the identity provider. + /// + /// + /// The identity provider. + /// + public Func IdentityProvider { get; set; } } } \ No newline at end of file diff --git a/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingIdentityParameter.cs b/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingIdentityParameter.cs index 1277be9..128fd31 100644 --- a/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingIdentityParameter.cs +++ b/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingIdentityParameter.cs @@ -22,6 +22,15 @@ public LoggingIdentityParameter(string identity, string name = null) Value = new IdentityParameter(identity, name); } + /// + /// Initializes a new instance of the class. + /// + /// The identity parameter. + public LoggingIdentityParameter(IdentityParameter identityParameter) + { + Value = identityParameter ?? throw new ArgumentNullException(nameof(identityParameter)); + } + /// /// Gets the parameter's value. /// diff --git a/Source/Vima.LoggingAbstractor.Raygun/RaygunAbstractLogger.cs b/Source/Vima.LoggingAbstractor.Raygun/RaygunAbstractLogger.cs index 6294bbc..ff3a9c4 100644 --- a/Source/Vima.LoggingAbstractor.Raygun/RaygunAbstractLogger.cs +++ b/Source/Vima.LoggingAbstractor.Raygun/RaygunAbstractLogger.cs @@ -53,7 +53,7 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn return; } - IEnumerable loggingParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList(); + var loggingParameters = GetGlobalAndLocalLoggingParameters(parameters); SetIdentityParameters(loggingParameters); var messageException = new RaygunMessageException(message); var data = ExtractDataValues(loggingParameters); @@ -74,7 +74,7 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev return; } - IEnumerable loggingParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList(); + var loggingParameters = GetGlobalAndLocalLoggingParameters(parameters); SetIdentityParameters(loggingParameters); var data = ExtractDataValues(loggingParameters); var tags = ExtractTags(loggingParameters, loggingLevel); diff --git a/Source/Vima.LoggingAbstractor.Sentry/SentryAbstractLogger.cs b/Source/Vima.LoggingAbstractor.Sentry/SentryAbstractLogger.cs index ded50d4..94b9c8b 100644 --- a/Source/Vima.LoggingAbstractor.Sentry/SentryAbstractLogger.cs +++ b/Source/Vima.LoggingAbstractor.Sentry/SentryAbstractLogger.cs @@ -53,7 +53,7 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn return; } - IEnumerable loggingParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList(); + var loggingParameters = GetGlobalAndLocalLoggingParameters(parameters); Dictionary tags = GenerateTags(loggingParameters); _ravenClient.Capture(new SentryEvent(message) { @@ -76,7 +76,7 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev return; } - IEnumerable loggingParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList(); + var loggingParameters = GetGlobalAndLocalLoggingParameters(parameters); Dictionary tags = GenerateTags(loggingParameters); _ravenClient.Capture(new SentryEvent(exception) {