From 0a57aef666c16c87a79a749f9e8caf2d5e5e8f1a Mon Sep 17 00:00:00 2001 From: Victor Usoltsev Date: Wed, 14 Mar 2018 22:31:06 +1300 Subject: [PATCH] Adds data parameter. Adds Multilogger. Updates packages. --- ...LoggingAbstractor.AppInsights.Tests.csproj | 6 +- .../AppInsightsLogger.cs | 19 +++- .../IAppInsightsLogger.cs | 12 +++ .../Vima.LoggingAbstractor.AppInsights.csproj | 4 +- .../Vima.LoggingAbstractor.Core.Tests.csproj | 4 +- .../Extensions/LoggingParameterExtensions.cs | 32 ++++++- .../IMultiLogger.cs | 9 ++ .../MultiLogger.cs | 92 +++++++++++++++++++ .../Parameters/LoggingDataParameter.cs | 35 +++++++ .../Parameters/LoggingParameterType.cs | 7 +- .../Vima.LoggingAbstractor.Core.csproj | 1 + ...Vima.LoggingAbstractor.Raygun.Tests.csproj | 4 +- .../IRaygunLogger.cs | 11 +++ .../RaygunLogger.cs | 22 ++++- .../Vima.LoggingAbstractor.Raygun.csproj | 2 +- ...Vima.LoggingAbstractor.Sentry.Tests.csproj | 4 +- .../ISentryLogger.cs | 11 +++ .../SentryLogger.cs | 12 ++- .../Vima.LoggingAbstractor.Sentry.csproj | 4 +- 19 files changed, 262 insertions(+), 29 deletions(-) create mode 100644 Source/Vima.LoggingAbstractor.AppInsights/IAppInsightsLogger.cs create mode 100644 Source/Vima.LoggingAbstractor.Core/IMultiLogger.cs create mode 100644 Source/Vima.LoggingAbstractor.Core/MultiLogger.cs create mode 100644 Source/Vima.LoggingAbstractor.Core/Parameters/LoggingDataParameter.cs create mode 100644 Source/Vima.LoggingAbstractor.Raygun/IRaygunLogger.cs create mode 100644 Source/Vima.LoggingAbstractor.Sentry/ISentryLogger.cs diff --git a/Source/Vima.LoggingAbstractor.AppInsights.Tests/Vima.LoggingAbstractor.AppInsights.Tests.csproj b/Source/Vima.LoggingAbstractor.AppInsights.Tests/Vima.LoggingAbstractor.AppInsights.Tests.csproj index 789f75e..756ce98 100644 --- a/Source/Vima.LoggingAbstractor.AppInsights.Tests/Vima.LoggingAbstractor.AppInsights.Tests.csproj +++ b/Source/Vima.LoggingAbstractor.AppInsights.Tests/Vima.LoggingAbstractor.AppInsights.Tests.csproj @@ -13,12 +13,12 @@ - - + + - + diff --git a/Source/Vima.LoggingAbstractor.AppInsights/AppInsightsLogger.cs b/Source/Vima.LoggingAbstractor.AppInsights/AppInsightsLogger.cs index 014bd80..d976d1d 100644 --- a/Source/Vima.LoggingAbstractor.AppInsights/AppInsightsLogger.cs +++ b/Source/Vima.LoggingAbstractor.AppInsights/AppInsightsLogger.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.DataContracts; using Vima.LoggingAbstractor.Core; @@ -11,7 +12,7 @@ namespace Vima.LoggingAbstractor.AppInsights /// /// Represents an instance of an Application Insights logger. /// - public class AppInsightsLogger : LoggerBase + public class AppInsightsLogger : LoggerBase, IAppInsightsLogger { private readonly TelemetryClient _telemetryClient; @@ -40,7 +41,7 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn } var traceTelemetry = new TraceTelemetry(message); - AddTagsToProperties(traceTelemetry, parameters); + AddParametersToProperties(traceTelemetry, parameters); _telemetryClient.Track(traceTelemetry); } @@ -58,16 +59,24 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev } var exceptionTelemetry = new ExceptionTelemetry(exception); - AddTagsToProperties(exceptionTelemetry, parameters); + AddParametersToProperties(exceptionTelemetry, parameters); _telemetryClient.Track(exceptionTelemetry); } - private static void AddTagsToProperties(ISupportProperties telemetry, IEnumerable parameters) + private static void AddParametersToProperties(ISupportProperties telemetry, IEnumerable parameters) { - foreach (string tag in parameters.ExtractTags()) + IEnumerable loggingParameters = parameters.ToList(); + + foreach (string tag in loggingParameters.ExtractTags()) { telemetry.Properties.Add(tag, tag); } + + var dataCount = 0; + foreach (string data in loggingParameters.ExtractData()) + { + telemetry.Properties.Add($"Data #{dataCount++}", data); + } } } } diff --git a/Source/Vima.LoggingAbstractor.AppInsights/IAppInsightsLogger.cs b/Source/Vima.LoggingAbstractor.AppInsights/IAppInsightsLogger.cs new file mode 100644 index 0000000..8dd3970 --- /dev/null +++ b/Source/Vima.LoggingAbstractor.AppInsights/IAppInsightsLogger.cs @@ -0,0 +1,12 @@ +using Vima.LoggingAbstractor.Core; + +namespace Vima.LoggingAbstractor.AppInsights +{ + /// + /// Represents an instance of an Application Insights logger. + /// + /// + public interface IAppInsightsLogger : ILogger + { + } +} \ No newline at end of file diff --git a/Source/Vima.LoggingAbstractor.AppInsights/Vima.LoggingAbstractor.AppInsights.csproj b/Source/Vima.LoggingAbstractor.AppInsights/Vima.LoggingAbstractor.AppInsights.csproj index 232efb1..8ef7232 100644 --- a/Source/Vima.LoggingAbstractor.AppInsights/Vima.LoggingAbstractor.AppInsights.csproj +++ b/Source/Vima.LoggingAbstractor.AppInsights/Vima.LoggingAbstractor.AppInsights.csproj @@ -20,7 +20,7 @@ - net47;net46;net45;net40;netstandard1.3;netstandard2.0 + net47;net46;net45;netstandard1.3;netstandard2.0 Vima.LoggingAbstractor.AppInsights Vima.LoggingAbstractor.AppInsights bin\$(Configuration)\$(TargetFramework)\Vima.LoggingAbstractor.AppInsights.xml @@ -30,7 +30,7 @@ - + diff --git a/Source/Vima.LoggingAbstractor.Core.Tests/Vima.LoggingAbstractor.Core.Tests.csproj b/Source/Vima.LoggingAbstractor.Core.Tests/Vima.LoggingAbstractor.Core.Tests.csproj index dc64d60..d72cd9d 100644 --- a/Source/Vima.LoggingAbstractor.Core.Tests/Vima.LoggingAbstractor.Core.Tests.csproj +++ b/Source/Vima.LoggingAbstractor.Core.Tests/Vima.LoggingAbstractor.Core.Tests.csproj @@ -13,11 +13,11 @@ - + - + diff --git a/Source/Vima.LoggingAbstractor.Core/Extensions/LoggingParameterExtensions.cs b/Source/Vima.LoggingAbstractor.Core/Extensions/LoggingParameterExtensions.cs index 9bd2604..4467e1e 100644 --- a/Source/Vima.LoggingAbstractor.Core/Extensions/LoggingParameterExtensions.cs +++ b/Source/Vima.LoggingAbstractor.Core/Extensions/LoggingParameterExtensions.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using Newtonsoft.Json; using Vima.LoggingAbstractor.Core.Parameters; namespace Vima.LoggingAbstractor.Core.Extensions @@ -13,7 +14,7 @@ public static class LoggingParameterExtensions /// Extracts the tags. /// /// The logging parameters. - /// Tags + /// Tag values public static IEnumerable ExtractTags(this IEnumerable parameters) { List loggingParameters = parameters @@ -36,5 +37,34 @@ public static IEnumerable ExtractTags(this IEnumerable + /// Extracts the data values. + /// + /// The logging parameters. + /// Data values + public static IEnumerable ExtractData(this IEnumerable parameters) + { + List loggingParameters = parameters + .Where(x => x.LoggingParameterType == LoggingParameterType.Data) + .ToList(); + + if (!loggingParameters.Any()) + { + return new List(); + } + + List result = new List(); + foreach (var loggingParameter in loggingParameters) + { + if (loggingParameter is ILoggingParameter data) + { + string value = JsonConvert.SerializeObject(data.Value); + result.Add(value); + } + } + + return result; + } } } diff --git a/Source/Vima.LoggingAbstractor.Core/IMultiLogger.cs b/Source/Vima.LoggingAbstractor.Core/IMultiLogger.cs new file mode 100644 index 0000000..459bf91 --- /dev/null +++ b/Source/Vima.LoggingAbstractor.Core/IMultiLogger.cs @@ -0,0 +1,9 @@ +namespace Vima.LoggingAbstractor.Core +{ + /// + /// Responsible for combining multiple loggers at the same time. + /// + public interface IMultiLogger : ILogger + { + } +} \ No newline at end of file diff --git a/Source/Vima.LoggingAbstractor.Core/MultiLogger.cs b/Source/Vima.LoggingAbstractor.Core/MultiLogger.cs new file mode 100644 index 0000000..7815014 --- /dev/null +++ b/Source/Vima.LoggingAbstractor.Core/MultiLogger.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Vima.LoggingAbstractor.Core.Parameters; + +namespace Vima.LoggingAbstractor.Core +{ + /// + /// Responsible for combining multiple loggers at the same time. + /// + public class MultiLogger : IMultiLogger + { + private readonly IEnumerable _loggers; + + /// + /// Initializes a new instance of the class. + /// + /// The loggers used to trace events. + protected MultiLogger(IEnumerable loggers) + { + _loggers = loggers ?? throw new ArgumentNullException(nameof(loggers)); + } + + /// + /// Traces the message. + /// + /// The message to be logged. + public void TraceMessage(string message) + { + TraceMessage(message, LoggingLevel.Verbose); + } + + /// + /// Traces the message. + /// + /// The message to be logged. + /// The logging level. + public void TraceMessage(string message, LoggingLevel loggingLevel) + { + TraceMessage(message, loggingLevel, Enumerable.Empty()); + } + + /// + /// Traces the message. + /// + /// The message to be logged. + /// The logging level. + /// The logging parameters. + public void TraceMessage(string message, LoggingLevel loggingLevel, IEnumerable parameters) + { + IEnumerable loggingParameters = parameters.ToList(); + foreach (var logger in _loggers) + { + logger.TraceMessage(message, loggingLevel, loggingParameters); + } + } + + /// + /// Traces the exception. + /// + /// The exception to be logged. + public void TraceException(Exception exception) + { + TraceException(exception, LoggingLevel.Critical); + } + + /// + /// Traces the exception. + /// + /// The exception to be logged. + /// The logging level. + public void TraceException(Exception exception, LoggingLevel loggingLevel) + { + TraceException(exception, loggingLevel, Enumerable.Empty()); + } + + /// + /// Traces the exception. + /// + /// The exception to be logged. + /// The logging level. + /// The logging parameters. + public void TraceException(Exception exception, LoggingLevel loggingLevel, IEnumerable parameters) + { + IEnumerable loggingParameters = parameters.ToList(); + foreach (var logger in _loggers) + { + logger.TraceException(exception, loggingLevel, loggingParameters); + } + } + } +} \ No newline at end of file diff --git a/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingDataParameter.cs b/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingDataParameter.cs new file mode 100644 index 0000000..2e9d819 --- /dev/null +++ b/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingDataParameter.cs @@ -0,0 +1,35 @@ +using System; + +namespace Vima.LoggingAbstractor.Core.Parameters +{ + /// + /// Represents logging data parameter. + /// + public class LoggingDataParameter : ILoggingParameter + { + /// + /// Initializes a new instance of the class. + /// + /// The data. + public LoggingDataParameter(object data) + { + Value = data ?? throw new ArgumentNullException(nameof(data)); + } + + /// + /// Gets the parameter's value. + /// + /// + /// The parameter's value. + /// + public object Value { get; } + + /// + /// Gets the type of the logging parameter. + /// + /// + /// The type of the logging parameter. + /// + public LoggingParameterType LoggingParameterType => LoggingParameterType.Data; + } +} \ No newline at end of file diff --git a/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingParameterType.cs b/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingParameterType.cs index 82c732b..4fb4d9b 100644 --- a/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingParameterType.cs +++ b/Source/Vima.LoggingAbstractor.Core/Parameters/LoggingParameterType.cs @@ -8,6 +8,11 @@ public enum LoggingParameterType /// /// The tags parameter. /// - Tags + Tags, + + /// + /// The data parameter + /// + Data } } \ No newline at end of file diff --git a/Source/Vima.LoggingAbstractor.Core/Vima.LoggingAbstractor.Core.csproj b/Source/Vima.LoggingAbstractor.Core/Vima.LoggingAbstractor.Core.csproj index 6dfd651..a501c19 100644 --- a/Source/Vima.LoggingAbstractor.Core/Vima.LoggingAbstractor.Core.csproj +++ b/Source/Vima.LoggingAbstractor.Core/Vima.LoggingAbstractor.Core.csproj @@ -30,6 +30,7 @@ + diff --git a/Source/Vima.LoggingAbstractor.Raygun.Tests/Vima.LoggingAbstractor.Raygun.Tests.csproj b/Source/Vima.LoggingAbstractor.Raygun.Tests/Vima.LoggingAbstractor.Raygun.Tests.csproj index 64b54ff..e3d11bf 100644 --- a/Source/Vima.LoggingAbstractor.Raygun.Tests/Vima.LoggingAbstractor.Raygun.Tests.csproj +++ b/Source/Vima.LoggingAbstractor.Raygun.Tests/Vima.LoggingAbstractor.Raygun.Tests.csproj @@ -13,11 +13,11 @@ - + - + diff --git a/Source/Vima.LoggingAbstractor.Raygun/IRaygunLogger.cs b/Source/Vima.LoggingAbstractor.Raygun/IRaygunLogger.cs new file mode 100644 index 0000000..f4ff6ba --- /dev/null +++ b/Source/Vima.LoggingAbstractor.Raygun/IRaygunLogger.cs @@ -0,0 +1,11 @@ +using Vima.LoggingAbstractor.Core; + +namespace Vima.LoggingAbstractor.Raygun +{ + /// + /// Represents an instance of a Raygun logger. + /// + public interface IRaygunLogger : ILogger + { + } +} \ No newline at end of file diff --git a/Source/Vima.LoggingAbstractor.Raygun/RaygunLogger.cs b/Source/Vima.LoggingAbstractor.Raygun/RaygunLogger.cs index cefa2ee..5c26794 100644 --- a/Source/Vima.LoggingAbstractor.Raygun/RaygunLogger.cs +++ b/Source/Vima.LoggingAbstractor.Raygun/RaygunLogger.cs @@ -11,7 +11,7 @@ namespace Vima.LoggingAbstractor.Raygun /// /// Represents an instance of a Raygun logger. /// - public class RaygunLogger : LoggerBase + public class RaygunLogger : LoggerBase, IRaygunLogger { private readonly RaygunClient _raygunClient; @@ -39,8 +39,10 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn return; } + IEnumerable loggingParameters = parameters.ToList(); var messageException = new RaygunMessageException(message); - _raygunClient.Send(messageException, parameters.ExtractTags().ToList()); + var data = ExtractDataValues(loggingParameters); + _raygunClient.Send(messageException, loggingParameters.ExtractTags().ToList(), data); } /// @@ -56,7 +58,21 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev return; } - _raygunClient.Send(exception, parameters.ExtractTags().ToList()); + IEnumerable loggingParameters = parameters.ToList(); + var data = ExtractDataValues(loggingParameters); + _raygunClient.Send(exception, loggingParameters.ExtractTags().ToList(), data); + } + + private Dictionary ExtractDataValues(IEnumerable parameters) + { + var dataCount = 0; + var dataDictionary = new Dictionary(); + foreach (string data in parameters.ExtractData()) + { + dataDictionary.Add($"Data #{dataCount++}", data); + } + + return dataDictionary; } } } diff --git a/Source/Vima.LoggingAbstractor.Raygun/Vima.LoggingAbstractor.Raygun.csproj b/Source/Vima.LoggingAbstractor.Raygun/Vima.LoggingAbstractor.Raygun.csproj index 894f369..9b626f9 100644 --- a/Source/Vima.LoggingAbstractor.Raygun/Vima.LoggingAbstractor.Raygun.csproj +++ b/Source/Vima.LoggingAbstractor.Raygun/Vima.LoggingAbstractor.Raygun.csproj @@ -31,7 +31,7 @@ - + diff --git a/Source/Vima.LoggingAbstractor.Sentry.Tests/Vima.LoggingAbstractor.Sentry.Tests.csproj b/Source/Vima.LoggingAbstractor.Sentry.Tests/Vima.LoggingAbstractor.Sentry.Tests.csproj index ba096f6..6250070 100644 --- a/Source/Vima.LoggingAbstractor.Sentry.Tests/Vima.LoggingAbstractor.Sentry.Tests.csproj +++ b/Source/Vima.LoggingAbstractor.Sentry.Tests/Vima.LoggingAbstractor.Sentry.Tests.csproj @@ -13,11 +13,11 @@ - + - + diff --git a/Source/Vima.LoggingAbstractor.Sentry/ISentryLogger.cs b/Source/Vima.LoggingAbstractor.Sentry/ISentryLogger.cs new file mode 100644 index 0000000..a8eb548 --- /dev/null +++ b/Source/Vima.LoggingAbstractor.Sentry/ISentryLogger.cs @@ -0,0 +1,11 @@ +using Vima.LoggingAbstractor.Core; + +namespace Vima.LoggingAbstractor.Sentry +{ + /// + /// Represents an instance of a Sentry logger. + /// + public interface ISentryLogger : ILogger + { + } +} \ No newline at end of file diff --git a/Source/Vima.LoggingAbstractor.Sentry/SentryLogger.cs b/Source/Vima.LoggingAbstractor.Sentry/SentryLogger.cs index 5a2c7ae..603887d 100644 --- a/Source/Vima.LoggingAbstractor.Sentry/SentryLogger.cs +++ b/Source/Vima.LoggingAbstractor.Sentry/SentryLogger.cs @@ -12,7 +12,7 @@ namespace Vima.LoggingAbstractor.Sentry /// /// Represents an instance of a Sentry logger. /// - public class SentryLogger : LoggerBase + public class SentryLogger : LoggerBase, ISentryLogger { private readonly RavenClient _ravenClient; @@ -40,8 +40,9 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn return; } - Dictionary tags = GenerateTags(parameters); - _ravenClient.Capture(new SentryEvent(message) { Tags = tags }); + IEnumerable loggingParameters = parameters.ToList(); + Dictionary tags = GenerateTags(loggingParameters); + _ravenClient.Capture(new SentryEvent(message) { Tags = tags, Extra = loggingParameters.ExtractData() }); } /// @@ -57,8 +58,9 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev return; } - Dictionary tags = GenerateTags(parameters); - _ravenClient.Capture(new SentryEvent(exception) { Tags = tags }); + IEnumerable loggingParameters = parameters.ToList(); + Dictionary tags = GenerateTags(loggingParameters); + _ravenClient.Capture(new SentryEvent(exception) { Tags = tags, Extra = loggingParameters.ExtractData() }); } private static Dictionary GenerateTags(IEnumerable parameters) diff --git a/Source/Vima.LoggingAbstractor.Sentry/Vima.LoggingAbstractor.Sentry.csproj b/Source/Vima.LoggingAbstractor.Sentry/Vima.LoggingAbstractor.Sentry.csproj index 4353653..6fdab52 100644 --- a/Source/Vima.LoggingAbstractor.Sentry/Vima.LoggingAbstractor.Sentry.csproj +++ b/Source/Vima.LoggingAbstractor.Sentry/Vima.LoggingAbstractor.Sentry.csproj @@ -20,7 +20,7 @@ - net47;net46;net45;net40;net35 + net47;net46;net45;net40;net35;netstandard2.0 Vima.LoggingAbstractor.Sentry Vima.LoggingAbstractor.Sentry bin\$(Configuration)\$(TargetFramework)\Vima.LoggingAbstractor.Sentry.xml @@ -30,7 +30,7 @@ - +