Skip to content

Commit

Permalink
Adds LoggerIdentityParameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
bernarden committed Dec 5, 2018
1 parent 297af77 commit 74eb3eb
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public void ShouldTraceExceptionWithTags()
var appInsightsLogger = CreateAppInsightsAbstractLogger(out var telemetryClient);
var exception = new Exception("Test-" + DateTime.UtcNow.ToString("s"));

LoggingTagsParameter loggingTagsParameter = new LoggingTagsParameter(new[] { "tag", "tag2" });
var loggingParameters = new List<ILoggingParameter> { loggingTagsParameter };
var loggingTagsParameter = new LoggingTagsParameter(new[] { "tag", "tag2" });
var loggingIdentityParameter = new LoggingIdentityParameter("identity", "name");
var loggingParameters = new List<ILoggingParameter> { loggingTagsParameter, loggingIdentityParameter };

// Act
appInsightsLogger.TraceException(exception, LoggingLevel.Critical, loggingParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Vima.LoggingAbstractor.Core;
using Vima.LoggingAbstractor.Core.Extensions;
Expand Down Expand Up @@ -53,11 +54,12 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn
return;
}

var allParameters = GetGlobalAndLocalLoggingParameters(parameters);
var allParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList();
var traceTelemetry = new TraceTelemetry(message)
{
SeverityLevel = LoggingLevelMapper.ConvertLoggingLevelToSeverityLevel(loggingLevel)
};
SetIdentityParameter(traceTelemetry, allParameters);
AddParametersToProperties(traceTelemetry, allParameters);
_telemetryClient.Track(traceTelemetry);
}
Expand All @@ -75,15 +77,27 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev
return;
}

var allParameters = GetGlobalAndLocalLoggingParameters(parameters);
var allParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList();
var exceptionTelemetry = new ExceptionTelemetry(exception)
{
SeverityLevel = LoggingLevelMapper.ConvertLoggingLevelToSeverityLevel(loggingLevel)
};
SetIdentityParameter(exceptionTelemetry, allParameters);
AddParametersToProperties(exceptionTelemetry, allParameters);
_telemetryClient.Track(exceptionTelemetry);
}

private static void SetIdentityParameter(ITelemetry exceptionTelemetry, IEnumerable<ILoggingParameter> loggingParameters)
{
var identity = loggingParameters.ExtractIdentity();
if (identity == null || string.IsNullOrEmpty(identity.Identity))
{
return;
}

exceptionTelemetry.Context.User.Id = identity.Identity;
}

private static void AddParametersToProperties(ISupportProperties telemetry, IEnumerable<ILoggingParameter> parameters)
{
IEnumerable<ILoggingParameter> loggingParameters = parameters.ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Newtonsoft.Json;
using Vima.LoggingAbstractor.Core.Extensions;
using Vima.LoggingAbstractor.Core.Parameters;
using Xunit;
Expand All @@ -15,11 +16,8 @@ public sealed class ExtractTags
[Fact]
public void ShouldHandleNoParameters()
{
// Arrange
var loggingParameters = new List<ILoggingParameter> { new LoggingDataParameter(string.Empty) };

// Act
var tags = loggingParameters.ExtractTags().ToList();
var tags = new List<ILoggingParameter>().ExtractTags().ToList();

// Assert
tags.Should().BeEmpty();
Expand All @@ -28,8 +26,11 @@ public void ShouldHandleNoParameters()
[Fact]
public void ShouldHandleNoTagsParameters()
{
// Arrange
var loggingParameters = new List<ILoggingParameter> { new LoggingDataParameter("Data") };

// Act
var tags = new List<ILoggingParameter>().ExtractTags().ToList();
var tags = loggingParameters.ExtractTags().ToList();

// Assert
tags.Should().BeEmpty();
Expand All @@ -56,15 +57,154 @@ public void ShouldHandleMultipleTagsParameter()
// Arrange
var loggingTagsParameter1 = new LoggingTagsParameter(new List<Enum> { LoggingLevel.Critical });
var loggingTagsParameter2 = new LoggingTagsParameter(new List<Enum> { LoggingLevel.None });
var loggingParameters = new List<ILoggingParameter> { loggingTagsParameter1, loggingTagsParameter2 };

// Act
var tags = new List<ILoggingParameter> { loggingTagsParameter1, loggingTagsParameter2 }.ExtractTags().ToList();
var tags = loggingParameters.ExtractTags().ToList();

// Assert
tags.Count.Should().Be(2);
tags.Should().Contain(LoggingLevel.Critical.ToString("G"));
tags.Should().Contain(LoggingLevel.None.ToString("G"));
}
}

public sealed class ExtractData
{
[Fact]
public void ShouldHandleNoParameters()
{
// Act
var data = new List<ILoggingParameter>().ExtractData().ToList();

// Assert
data.Should().BeEmpty();
}

[Fact]
public void ShouldHandleNoDataParameters()
{
// Arrange
var loggingParameters = new[] { new LoggingTagsParameter(new List<string> { "CustomTag" }) };

// Act
var data = loggingParameters.ExtractData().ToList();

// Assert
data.Should().BeEmpty();
}

[Fact]
public void ShouldHandleOneDataParameter()
{
// Arrange
var dataValue = "Data";
var loggingParameters = new List<ILoggingParameter> { new LoggingDataParameter(dataValue) };

// Act
var data = loggingParameters.ExtractData().ToList();

// Assert
data.Count.Should().Be(1);
data.Should().Contain(JsonConvert.SerializeObject(dataValue));
}

[Fact]
public void ShouldHandleMultipleDataParameter()
{
// Arrange
var dataValue1 = "Data 1";
var dataValue2 = "Data 2";
var loggingParameters = new List<ILoggingParameter> { new LoggingDataParameter(dataValue1), new LoggingDataParameter(dataValue2) };

// Act
var data = loggingParameters.ExtractData().ToList();

// Assert
data.Count.Should().Be(2);
data.Should().Contain(JsonConvert.SerializeObject(dataValue1));
data.Should().Contain(JsonConvert.SerializeObject(dataValue2));
}
}

public sealed class ExtractIdentity
{
[Fact]
public void ShouldHandleNoParameters()
{
// Act
var identity = new List<ILoggingParameter>().ExtractIdentity();

// Assert
identity.Should().BeNull();
}

[Fact]
public void ShouldHandleNoIdentityParameters()
{
// Arrange
var loggingParameters = new[] { new LoggingTagsParameter(new List<string> { "CustomTag" }) };

// Act
var identity = loggingParameters.ExtractIdentity();

// Assert
identity.Should().BeNull();
}

[Fact]
public void ShouldHandleIdentityParameter()
{
// Arrange
var identityValue = "Id";
var identityName = "Name";
var loggingParameters = new List<ILoggingParameter> { new LoggingIdentityParameter(identityValue, identityName) };

// Act
var identity = loggingParameters.ExtractIdentity();

// Assert
identity.Should().NotBeNull();
identity.Identity.Should().Be(identityValue);
identity.Name.Should().Be(identityName);
}

[Fact]
public void ShouldHandleIdentityParameterWithoutName()
{
// Arrange
var identityValue = "Id";
var loggingParameters = new List<ILoggingParameter> { new LoggingIdentityParameter(identityValue) };

// Act
var identity = loggingParameters.ExtractIdentity();

// Assert
identity.Should().NotBeNull();
identity.Identity.Should().Be(identityValue);
identity.Name.Should().BeNull();
}

[Fact]
public void ShouldHandleMultipleIdentityParameter()
{
// Arrange
var identityValue = "Id";
var identityName = "Name";
var loggingParameters = new List<ILoggingParameter>
{
new LoggingIdentityParameter(identityValue, identityName),
new LoggingIdentityParameter($"{identityValue}-2", $"{identityName}-2")
};

// Act
var identity = loggingParameters.ExtractIdentity();

// Assert
identity.Should().NotBeNull();
identity.Identity.Should().Be(identityValue);
identity.Name.Should().Be(identityName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,23 @@ public static IEnumerable<string> ExtractData(this IEnumerable<ILoggingParameter

return result;
}

/// <summary>
/// Extracts the identity information.
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <returns>Identity value</returns>
public static IdentityParameter ExtractIdentity(this IEnumerable<ILoggingParameter> parameters)
{
ILoggingParameter loggingParameter = parameters
.FirstOrDefault(x => x.LoggingParameterType == LoggingParameterType.Identity);

if (loggingParameter != null && loggingParameter is ILoggingParameter<IdentityParameter> identity)
{
return identity.Value;
}

return null;
}
}
}
}
37 changes: 37 additions & 0 deletions Source/Vima.LoggingAbstractor.Core/Parameters/IdentityParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;

namespace Vima.LoggingAbstractor.Core.Parameters
{
/// <summary>
/// Represents identity parameter.
/// </summary>
public class IdentityParameter
{
/// <summary>
/// Initializes a new instance of the <see cref="IdentityParameter"/> class.
/// </summary>
/// <param name="identity">The identity.</param>
/// <param name="name">The name. Can be null.</param>
public IdentityParameter(string identity, string name)
{
Identity = identity ?? throw new ArgumentNullException(nameof(identity));
Name = name;
}

/// <summary>
/// Gets the identity.
/// </summary>
/// <value>
/// The identity.
/// </value>
public string Identity { get; }

/// <summary>
/// Gets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;

namespace Vima.LoggingAbstractor.Core.Parameters
{
/// <summary>
/// Represents logging identity parameter.
/// </summary>
public class LoggingIdentityParameter : ILoggingParameter<IdentityParameter>
{
/// <summary>
/// Initializes a new instance of the <see cref="LoggingIdentityParameter"/> class.
/// </summary>
/// <param name="identity">The identity.</param>
/// <param name="name">The name.</param>
public LoggingIdentityParameter(string identity, string name = null)
{
if (identity == null)
{
throw new ArgumentNullException(nameof(identity));
}

Value = new IdentityParameter(identity, name);
}

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

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

/// <summary>
/// The data parameter
/// The data parameter.
/// </summary>
Data
Data,

/// <summary>
/// The identity parameter.
/// </summary>
Identity
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public void ShouldTraceExceptionWithTags()
var raygunLogger = CreateRaygunAbstractLogger();
var exception = new Exception("Test-" + DateTime.UtcNow.ToString("s"));

LoggingTagsParameter loggingTagsParameter = new LoggingTagsParameter(new[] { "tag", "tag2" });
var loggingParameters = new List<ILoggingParameter> { loggingTagsParameter };
var loggingTagsParameter = new LoggingTagsParameter(new[] { "tag", "tag2" });
var loggingIdentityParameter = new LoggingIdentityParameter("identity", "name");
var loggingParameters = new List<ILoggingParameter> { loggingTagsParameter, loggingIdentityParameter };

// Act
raygunLogger.TraceException(exception, LoggingLevel.Critical, loggingParameters);
Expand Down
Loading

0 comments on commit 74eb3eb

Please sign in to comment.