-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from bernarden/develop
Merges develop into master.
- Loading branch information
Showing
35 changed files
with
1,968 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
Source/Vima.LoggingAbstractor.AppInsights.Tests/UnitTest1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../Vima.LoggingAbstractor.AppInsights.Tests/Vima.LoggingAbstractor.AppInsights.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
82
Source/Vima.LoggingAbstractor.AppInsights/AppInsightsLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
Source/Vima.LoggingAbstractor.AppInsights/IAppInsightsLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
Source/Vima.LoggingAbstractor.AppInsights/Vima.LoggingAbstractor.AppInsights.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
Source/Vima.LoggingAbstractor.Core.Tests/LoggerBaseTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
Source/Vima.LoggingAbstractor.Core.Tests/LoggingParameterExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
Source/Vima.LoggingAbstractor.Core.Tests/TestLoggerBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Source/Vima.LoggingAbstractor.Core.Tests/Vima.LoggingAbstractor.Core.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.