Skip to content

Commit

Permalink
Merge pull request #7 from bernarden/develop
Browse files Browse the repository at this point in the history
Merges develop into master.
  • Loading branch information
bernarden authored Mar 14, 2018
2 parents c581641 + d3020b6 commit 5d6d992
Show file tree
Hide file tree
Showing 35 changed files with 1,968 additions and 46 deletions.
8 changes: 6 additions & 2 deletions Scripts/Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ properties {
@{"Framework" = "net45"; "TestingFramework" = "net-4.5"; "Utility" = "NUnit";},
@{"Framework" = "net451"; "TestingFramework" = "net-4.0"; "Utility" = "NUnit";},
@{"Framework" = "net452"; "TestingFramework" = "net-4.5"; "Utility" = "NUnit";},
@{"Framework" = "net46"; "TestingFramework" = "net46"; "Utility" = "DotnetTest";}
@{"Framework" = "net46"; "TestingFramework" = "net46"; "Utility" = "DotnetTest";},
@{"Framework" = "net47"; "TestingFramework" = "net47"; "Utility" = "DotnetTest";}
)

$ProjectsToPublish=@(
"$SourceDirectory\Vima.LoggingAbstractor.Core\Vima.LoggingAbstractor.Core.csproj"
"$SourceDirectory\Vima.LoggingAbstractor.Core\Vima.LoggingAbstractor.Core.csproj",
"$SourceDirectory\Vima.LoggingAbstractor.AppInsights\Vima.LoggingAbstractor.AppInsights.csproj",
"$SourceDirectory\Vima.LoggingAbstractor.Raygun\Vima.LoggingAbstractor.Raygun.csproj",
"$SourceDirectory\Vima.LoggingAbstractor.Sentry\Vima.LoggingAbstractor.Sentry.csproj"
)
}

Expand Down
12 changes: 12 additions & 0 deletions Source/Vima.LoggingAbstractor.AppInsights.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xunit;

namespace Vima.LoggingAbstractor.AppInsights.Tests
{
public class UnitTest1
{
[Fact]
public void Test1()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>Vima.LoggingAbstractor.AppInsights.Tests</AssemblyName>
<RootNamespace>Vima.LoggingAbstractor.AppInsights.Tests</RootNamespace>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\Vima.LoggingAbstractor.AppInsights.Tests.xml</DocumentationFile>
<CodeAnalysisRuleSet>..\ca-tests.ruleset</CodeAnalysisRuleSet>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.1.2" />
<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.6.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Vima.LoggingAbstractor.AppInsights\Vima.LoggingAbstractor.AppInsights.csproj" />
</ItemGroup>
</Project>
82 changes: 82 additions & 0 deletions Source/Vima.LoggingAbstractor.AppInsights/AppInsightsLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Vima.LoggingAbstractor.Core;
using Vima.LoggingAbstractor.Core.Extensions;
using Vima.LoggingAbstractor.Core.Parameters;

namespace Vima.LoggingAbstractor.AppInsights
{
/// <summary>
/// Represents an instance of an Application Insights logger.
/// </summary>
public class AppInsightsLogger : LoggerBase, IAppInsightsLogger
{
private readonly TelemetryClient _telemetryClient;

/// <summary>
/// Initializes a new instance of the <see cref="AppInsightsLogger"/> class.
/// </summary>
/// <param name="telemetryClient">The Application Insights client.</param>
/// <param name="minimalLoggingLevel">The minimal logging level.</param>
public AppInsightsLogger(TelemetryClient telemetryClient, LoggingLevel minimalLoggingLevel = LoggingLevel.Verbose)
: base(minimalLoggingLevel)
{
_telemetryClient = telemetryClient ?? throw new ArgumentNullException(nameof(telemetryClient));
}

/// <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 override void TraceMessage(string message, LoggingLevel loggingLevel, IEnumerable<ILoggingParameter> parameters)
{
if (!ShouldBeTraced(loggingLevel))
{
return;
}

var traceTelemetry = new TraceTelemetry(message);
AddParametersToProperties(traceTelemetry, parameters);
_telemetryClient.Track(traceTelemetry);
}

/// <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 override void TraceException(Exception exception, LoggingLevel loggingLevel, IEnumerable<ILoggingParameter> parameters)
{
if (!ShouldBeTraced(loggingLevel))
{
return;
}

var exceptionTelemetry = new ExceptionTelemetry(exception);
AddParametersToProperties(exceptionTelemetry, parameters);
_telemetryClient.Track(exceptionTelemetry);
}

private static void AddParametersToProperties(ISupportProperties telemetry, IEnumerable<ILoggingParameter> parameters)
{
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
@@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.1.0</Version>
<FileVersion>0.1.0</FileVersion>
<Authors>Victor Usoltsev</Authors>
<Description>Logging Abstractor is a library for .NET that allows you to swap out logging providers with ease.</Description>
<Copyright>Copyright © Victor Usoltsev 2018</Copyright>
<PackageLicenseUrl>https://github.com/bernarden/LoggingAbstractor/blob/master/LICENSE</PackageLicenseUrl>
<PackageIconUrl>https://raw.githubusercontent.com/bernarden/LoggingAbstractor/master/Resources/NugetIcon.png</PackageIconUrl>
<RepositoryUrl>https://github.com/bernarden/LoggingAbstractor</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>logging abstractor abstraction logger</PackageTags>
<PackageProjectUrl>https://github.com/bernarden/LoggingAbstractor</PackageProjectUrl>
<Company>Vima</Company>
<NeutralLanguage>en-US</NeutralLanguage>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Product>LoggingAbstractor.AppInsights</Product>
<PackageId>LoggingAbstractor.AppInsights</PackageId>
</PropertyGroup>

<PropertyGroup>
<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>
<CodeAnalysisRuleSet>..\ca.ruleset</CodeAnalysisRuleSet>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\Vima.LoggingAbstractor.Core\Vima.LoggingAbstractor.Core.csproj" />
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions Source/Vima.LoggingAbstractor.Core.Tests/LoggerBaseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Xunit;

namespace Vima.LoggingAbstractor.Core.Tests
{
public sealed class LoggerBaseTest
{
public sealed class ShouldBeTraced
{
[Fact]
public void ShouldReturnCorrectValueInAllCombinationsOfInputs()
{
var loggingLevels = Enum.GetValues(typeof(LoggingLevel)).Cast<LoggingLevel>().ToList();

foreach (var minimalLoggingLevel in loggingLevels)
{
foreach (var currentLoggingLevel in loggingLevels)
{
// Arrange
TestLoggerBase loggerBase = new TestLoggerBase(minimalLoggingLevel);
var expectedResult = ShouldClientLogTrace(minimalLoggingLevel, currentLoggingLevel);

// Act
var result = loggerBase.ShouldBeTraced(currentLoggingLevel);

// Assert
result.Should().Be(expectedResult, $"current logging level is '{currentLoggingLevel:G}' and minimal logging level is '{minimalLoggingLevel.ToString()}'");
}
}
}

private static bool ShouldClientLogTrace(LoggingLevel currentLoggingLevel, LoggingLevel minimumLoggingLevel)
{
Dictionary<LoggingLevel, List<LoggingLevel>> allowedLoggingLevelsForMinimumLoggingLevel =
new Dictionary<LoggingLevel, List<LoggingLevel>>
{
{ LoggingLevel.Verbose, new List<LoggingLevel> { LoggingLevel.Verbose } },
{ LoggingLevel.Information, new List<LoggingLevel> { LoggingLevel.Verbose, LoggingLevel.Information } },
{ LoggingLevel.Warning, new List<LoggingLevel> { LoggingLevel.Verbose, LoggingLevel.Information, LoggingLevel.Warning } },
{ LoggingLevel.Error, new List<LoggingLevel> { LoggingLevel.Verbose, LoggingLevel.Information, LoggingLevel.Warning, LoggingLevel.Error } },
{ LoggingLevel.Critical, new List<LoggingLevel> { LoggingLevel.Verbose, LoggingLevel.Information, LoggingLevel.Warning, LoggingLevel.Error, LoggingLevel.Critical } },
{ LoggingLevel.None, new List<LoggingLevel>() }
};

return allowedLoggingLevelsForMinimumLoggingLevel[minimumLoggingLevel].Contains(currentLoggingLevel);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Vima.LoggingAbstractor.Core.Extensions;
using Vima.LoggingAbstractor.Core.Parameters;
using Xunit;

namespace Vima.LoggingAbstractor.Core.Tests
{
public sealed class LoggingParameterExtensionsTest
{
public sealed class ExtractTags
{
[Fact(Skip = "Need other logging parameters to exist.")]
public void ShouldHandleNoParameters()
{
// Arrange
// TODO: Create different logging parameter and add here for tests.

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

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

[Fact]
public void ShouldHandleNoTagsParameters()
{
// Act
var tags = new List<ILoggingParameter>().ExtractTags().ToList();

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

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

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

// Assert
tags.Count.Should().Be(1);
tags.Should().Contain(tag);
}

[Fact]
public void ShouldHandleMultipleTagsParameter()
{
// Arrange
var loggingTagsParameter1 = new LoggingTagsParameter(new List<Enum> { LoggingLevel.Critical });
var loggingTagsParameter2 = new LoggingTagsParameter(new List<Enum> { LoggingLevel.None });

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

// Assert
tags.Count.Should().Be(2);
tags.Should().Contain(LoggingLevel.Critical.ToString("G"));
tags.Should().Contain(LoggingLevel.None.ToString("G"));
}
}
}
}
27 changes: 27 additions & 0 deletions Source/Vima.LoggingAbstractor.Core.Tests/TestLoggerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using Vima.LoggingAbstractor.Core.Parameters;

namespace Vima.LoggingAbstractor.Core.Tests
{
public class TestLoggerBase : LoggerBase
{
public TestLoggerBase(LoggingLevel minimalLoggingLevel = LoggingLevel.Verbose)
: base(minimalLoggingLevel)
{
}

public override void TraceMessage(string message, LoggingLevel loggingLevel, IEnumerable<ILoggingParameter> parameters)
{
}

public override void TraceException(Exception exception, LoggingLevel loggingLevel, IEnumerable<ILoggingParameter> parameters)
{
}

public new bool ShouldBeTraced(LoggingLevel loggingLevel)
{
return base.ShouldBeTraced(loggingLevel);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>Vima.LoggingAbstractor.Core.Tests</AssemblyName>
<RootNamespace>Vima.LoggingAbstractor.Core.Tests</RootNamespace>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\Vima.LoggingAbstractor.Core.Tests.xml</DocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CodeAnalysisRuleSet>..\ca-tests.ruleset</CodeAnalysisRuleSet>
<WarningsAsErrors />
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.1.2" />
<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.6.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Vima.LoggingAbstractor.Core\Vima.LoggingAbstractor.Core.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit 5d6d992

Please sign in to comment.