Skip to content

Commit

Permalink
Adds data parameter. Adds Multilogger. Updates packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
bernarden committed Mar 14, 2018
1 parent 175402e commit 0a57aef
Show file tree
Hide file tree
Showing 19 changed files with 262 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.1.2" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.5.0" />
<PackageReference Include="Moq" Version="4.8.1" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.5.1" />
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

Expand Down
19 changes: 14 additions & 5 deletions Source/Vima.LoggingAbstractor.AppInsights/AppInsightsLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Vima.LoggingAbstractor.Core;
Expand All @@ -11,7 +12,7 @@ namespace Vima.LoggingAbstractor.AppInsights
/// <summary>
/// Represents an instance of an Application Insights logger.
/// </summary>
public class AppInsightsLogger : LoggerBase
public class AppInsightsLogger : LoggerBase, IAppInsightsLogger
{
private readonly TelemetryClient _telemetryClient;

Expand Down Expand Up @@ -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);
}

Expand All @@ -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<ILoggingParameter> parameters)
private static void AddParametersToProperties(ISupportProperties telemetry, IEnumerable<ILoggingParameter> parameters)
{
foreach (string tag in parameters.ExtractTags())
IEnumerable<ILoggingParameter> 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);
}
}
}
}
12 changes: 12 additions & 0 deletions Source/Vima.LoggingAbstractor.AppInsights/IAppInsightsLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Vima.LoggingAbstractor.Core;

namespace Vima.LoggingAbstractor.AppInsights
{
/// <summary>
/// Represents an instance of an Application Insights logger.
/// </summary>
/// <seealso cref="Vima.LoggingAbstractor.Core.ILogger" />
public interface IAppInsightsLogger : ILogger
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks>net47;net46;net45;net40;netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net47;net46;net45;netstandard1.3;netstandard2.0</TargetFrameworks>
<AssemblyName>Vima.LoggingAbstractor.AppInsights</AssemblyName>
<RootNamespace>Vima.LoggingAbstractor.AppInsights</RootNamespace>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\Vima.LoggingAbstractor.AppInsights.xml</DocumentationFile>
Expand All @@ -30,7 +30,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.4.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.5.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="All" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.1.2" />
<PackageReference Include="Moq" Version="4.8.1" />
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Vima.LoggingAbstractor.Core.Parameters;

namespace Vima.LoggingAbstractor.Core.Extensions
Expand All @@ -13,7 +14,7 @@ public static class LoggingParameterExtensions
/// Extracts the tags.
/// </summary>
/// <param name="parameters">The logging parameters.</param>
/// <returns>Tags</returns>
/// <returns>Tag values</returns>
public static IEnumerable<string> ExtractTags(this IEnumerable<ILoggingParameter> parameters)
{
List<ILoggingParameter> loggingParameters = parameters
Expand All @@ -36,5 +37,34 @@ public static IEnumerable<string> ExtractTags(this IEnumerable<ILoggingParameter

return result.Distinct();
}

/// <summary>
/// Extracts the data values.
/// </summary>
/// <param name="parameters">The logging parameters.</param>
/// <returns>Data values</returns>
public static IEnumerable<string> ExtractData(this IEnumerable<ILoggingParameter> parameters)
{
List<ILoggingParameter> loggingParameters = parameters
.Where(x => x.LoggingParameterType == LoggingParameterType.Data)
.ToList();

if (!loggingParameters.Any())
{
return new List<string>();
}

List<string> result = new List<string>();
foreach (var loggingParameter in loggingParameters)
{
if (loggingParameter is ILoggingParameter<object> data)
{
string value = JsonConvert.SerializeObject(data.Value);
result.Add(value);
}
}

return result;
}
}
}
9 changes: 9 additions & 0 deletions Source/Vima.LoggingAbstractor.Core/IMultiLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Vima.LoggingAbstractor.Core
{
/// <summary>
/// Responsible for combining multiple loggers at the same time.
/// </summary>
public interface IMultiLogger : ILogger
{
}
}
92 changes: 92 additions & 0 deletions Source/Vima.LoggingAbstractor.Core/MultiLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Vima.LoggingAbstractor.Core.Parameters;

namespace Vima.LoggingAbstractor.Core
{
/// <summary>
/// Responsible for combining multiple loggers at the same time.
/// </summary>
public class MultiLogger : IMultiLogger
{
private readonly IEnumerable<ILogger> _loggers;

/// <summary>
/// Initializes a new instance of the <see cref="MultiLogger"/> class.
/// </summary>
/// <param name="loggers">The loggers used to trace events.</param>
protected MultiLogger(IEnumerable<ILogger> loggers)
{
_loggers = loggers ?? throw new ArgumentNullException(nameof(loggers));
}

/// <summary>
/// Traces the message.
/// </summary>
/// <param name="message">The message to be logged.</param>
public void TraceMessage(string message)
{
TraceMessage(message, LoggingLevel.Verbose);
}

/// <summary>
/// Traces the message.
/// </summary>
/// <param name="message">The message to be logged.</param>
/// <param name="loggingLevel">The logging level.</param>
public void TraceMessage(string message, LoggingLevel loggingLevel)
{
TraceMessage(message, loggingLevel, Enumerable.Empty<ILoggingParameter>());
}

/// <summary>
/// Traces the message.
/// </summary>
/// <param name="message">The message to be logged.</param>
/// <param name="loggingLevel">The logging level.</param>
/// <param name="parameters">The logging parameters.</param>
public void TraceMessage(string message, LoggingLevel loggingLevel, IEnumerable<ILoggingParameter> parameters)
{
IEnumerable<ILoggingParameter> loggingParameters = parameters.ToList();
foreach (var logger in _loggers)
{
logger.TraceMessage(message, loggingLevel, loggingParameters);
}
}

/// <summary>
/// Traces the exception.
/// </summary>
/// <param name="exception">The exception to be logged.</param>
public void TraceException(Exception exception)
{
TraceException(exception, LoggingLevel.Critical);
}

/// <summary>
/// Traces the exception.
/// </summary>
/// <param name="exception">The exception to be logged.</param>
/// <param name="loggingLevel">The logging level.</param>
public void TraceException(Exception exception, LoggingLevel loggingLevel)
{
TraceException(exception, loggingLevel, Enumerable.Empty<ILoggingParameter>());
}

/// <summary>
/// Traces the exception.
/// </summary>
/// <param name="exception">The exception to be logged.</param>
/// <param name="loggingLevel">The logging level.</param>
/// <param name="parameters">The logging parameters.</param>
public void TraceException(Exception exception, LoggingLevel loggingLevel, IEnumerable<ILoggingParameter> parameters)
{
IEnumerable<ILoggingParameter> loggingParameters = parameters.ToList();
foreach (var logger in _loggers)
{
logger.TraceException(exception, loggingLevel, loggingParameters);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;

namespace Vima.LoggingAbstractor.Core.Parameters
{
/// <summary>
/// Represents logging data parameter.
/// </summary>
public class LoggingDataParameter : ILoggingParameter<object>
{
/// <summary>
/// Initializes a new instance of the <see cref="LoggingDataParameter"/> class.
/// </summary>
/// <param name="data">The data.</param>
public LoggingDataParameter(object data)
{
Value = data ?? throw new ArgumentNullException(nameof(data));
}

/// <summary>
/// Gets the parameter's value.
/// </summary>
/// <value>
/// The parameter's value.
/// </value>
public object Value { get; }

/// <summary>
/// Gets the type of the logging parameter.
/// </summary>
/// <value>
/// The type of the logging parameter.
/// </value>
public LoggingParameterType LoggingParameterType => LoggingParameterType.Data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public enum LoggingParameterType
/// <summary>
/// The tags parameter.
/// </summary>
Tags
Tags,

/// <summary>
/// The data parameter
/// </summary>
Data
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="All" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.1.2" />
<PackageReference Include="Moq" Version="4.8.1" />
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions Source/Vima.LoggingAbstractor.Raygun/IRaygunLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Vima.LoggingAbstractor.Core;

namespace Vima.LoggingAbstractor.Raygun
{
/// <summary>
/// Represents an instance of a Raygun logger.
/// </summary>
public interface IRaygunLogger : ILogger
{
}
}
Loading

0 comments on commit 0a57aef

Please sign in to comment.