Skip to content

Commit

Permalink
Added Diagnostic Listener Logging Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll committed Jun 9, 2018
1 parent a463c6f commit 9897fb3
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 25 deletions.
7 changes: 4 additions & 3 deletions Common.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
<PropertyGroup>
<RSG_Build_Version>2.0.0</RSG_Build_Version>
<RSG_Builders_Version>3.0.0</RSG_Builders_Version>
<Microsoft_Extensions_Logging_Version>2.0.0</Microsoft_Extensions_Logging_Version>
<Microsoft_Extensions_DependencyModel_Version>2.0.0</Microsoft_Extensions_DependencyModel_Version>
<Microsoft_Extensions_Logging_Version>2.1.0</Microsoft_Extensions_Logging_Version>
<Microsoft_Extensions_DependencyModel_Version>2.1.0</Microsoft_Extensions_DependencyModel_Version>
<Microsoft_Extensions_DiagnosticAdapter_Version>2.1.0</Microsoft_Extensions_DiagnosticAdapter_Version>
<System_Diagnostics_DiagnosticSource_Version>4.5.0</System_Diagnostics_DiagnosticSource_Version>
<SourceLink_Version>2.8.1</SourceLink_Version>
<Microsoft_NET_Test_Sdk_Version>15.7.2</Microsoft_NET_Test_Sdk_Version>
Expand All @@ -18,7 +19,7 @@
<FakeItEasy_Version>4.6.0</FakeItEasy_Version>
<Autofac_FakeItEasy_Version>5.0.0</Autofac_FakeItEasy_Version>
<Bogus_Version>22.1.2</Bogus_Version>
<RSG_Testing_Version>0.5.0</RSG_Testing_Version>
<RSG_Testing_Version>0.7.1</RSG_Testing_Version>
<XunitXml_TestLogger_Version>2.0.0</XunitXml_TestLogger_Version>
</PropertyGroup>
</Project>
37 changes: 37 additions & 0 deletions src/Conventions/DiagnosticListenerLoggingAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using Microsoft.Extensions.DiagnosticAdapter;
using Microsoft.Extensions.Logging;

namespace Rocket.Surgery.Conventions
{
public class DiagnosticListenerLoggingAdapter
{
private readonly ILogger _logger;

public DiagnosticListenerLoggingAdapter(ILogger logger)
{
_logger = logger;
}

[DiagnosticName("Log.Other")]
public void LogOther(LogLevel logLevel, EventId eventId, Exception exception, string message) => _logger.Log(logLevel, eventId, exception, message);

[DiagnosticName("Log.Trace")]
public void LogTrace(EventId eventId, Exception exception, string message) => _logger.LogTrace(eventId, exception, message);

[DiagnosticName("Log.Debug")]
public void LogDebug(EventId eventId, Exception exception, string message) => _logger.LogDebug(eventId, exception, message);

[DiagnosticName("Log.Information")]
public void LogInformation(EventId eventId, Exception exception, string message) => _logger.LogInformation(eventId, exception, message);

[DiagnosticName("Log.Warning")]
public void LogWarning(EventId eventId, Exception exception, string message) => _logger.LogWarning(eventId, exception, message);

[DiagnosticName("Log.Error")]
public void LogError(EventId eventId, Exception exception, string message) => _logger.LogError(eventId, exception, message);

[DiagnosticName("Log.Critical")]
public void LogCritical(EventId eventId, Exception exception, string message) => _logger.LogCritical(eventId, exception, message);
}
}
3 changes: 2 additions & 1 deletion src/Conventions/DiagnosticLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Rocket.Surgery.Conventions
public class DiagnosticLogger : ILogger
{
private readonly DiagnosticSource _diagnosticSource;
private static readonly IReadOnlyDictionary<LogLevel, string> Names =
internal static readonly IReadOnlyDictionary<LogLevel, string> Names =
Enum.GetValues(typeof(LogLevel))
.Cast<LogLevel>()
.ToDictionary(x => x, x => $"Log.{x}");
Expand All @@ -28,6 +28,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
{
_diagnosticSource.Write(GetName(logLevel), new
{
logLevel,
eventId,
state = (object)state,
exception,
Expand Down
5 changes: 4 additions & 1 deletion src/Conventions/Reflection/AssemblyCandidateResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ private Dependency CreateDependency(Assembly library, ISet<string> referenceAsse
private DependencyClassification ComputeClassification(string dependency)
{
// Prevents issues with looking at system assemblies
if (dependency.StartsWith("System.") || dependency.StartsWith("mscorlib") || dependency.StartsWith("Microsoft."))
if (dependency.StartsWith("System.") ||
dependency.StartsWith("mscorlib") ||
dependency.StartsWith("Microsoft.") ||
dependency.StartsWith("DynamicProxyGenAssembly"))
{
return DependencyClassification.NotCandidate;
}
Expand Down
1 change: 1 addition & 0 deletions src/Conventions/Rocket.Surgery.Conventions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(Microsoft_Extensions_Logging_Version)" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="$(Microsoft_Extensions_DependencyModel_Version)" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="$(System_Diagnostics_DiagnosticSource_Version)" />
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="$(Microsoft_Extensions_DiagnosticAdapter_Version)" />
</ItemGroup>
</Project>
40 changes: 20 additions & 20 deletions test/Conventions.Tests/ConventionComposerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void RegisterShouldCallConvention()

composer.Register(context, new [] { typeof(ITestConvention), typeof(TestConventionDelegate) });

A.CallTo(() => ((ITestConvention)convention).Register(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => ((IServiceConvention)convention).Register(A<IServiceConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => ((ITestConvention)convention).Register(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => ((IServiceConvention)convention).Register(A<IServiceConventionContext>._)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand All @@ -57,9 +57,9 @@ public void RegisterShouldCallBothConvention()

composer.Register(context, new [] { typeof(IServiceConvention), typeof(ServiceConventionDelegate), typeof(ITestConvention), typeof(TestConventionDelegate) });

A.CallTo(() => ((ITestConvention)convention).Register(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => ((IServiceConvention)convention).Register(A<IServiceConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => convention2.Register(A<IServiceConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => ((ITestConvention)convention).Register(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => ((IServiceConvention)convention).Register(A<IServiceConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => convention2.Register(A<IServiceConventionContext>._)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand All @@ -79,8 +79,8 @@ public void RegisterShouldCallBothDelegates()

composer.Register(context, typeof(IServiceConvention), typeof(ServiceConventionDelegate), typeof(ITestConvention), typeof(TestConventionDelegate));

A.CallTo(() => @delegate(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => delegate2(A<IServiceConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => @delegate(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => delegate2(A<IServiceConventionContext>._)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand Down Expand Up @@ -108,10 +108,10 @@ public void RegisterShouldCallAllTheThings()

composer.Register(context, typeof(IServiceConvention), typeof(ServiceConventionDelegate), typeof(ITestConvention), typeof(TestConventionDelegate));

A.CallTo(() => convention.Register(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => convention2.Register(A<IServiceConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => @delegate(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => delegate2(A<IServiceConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => convention.Register(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => convention2.Register(A<IServiceConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => @delegate(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => delegate2(A<IServiceConventionContext>._)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand All @@ -137,9 +137,9 @@ public void RegisterShouldCallConventions()

composer.Register(context, new[] { typeof(ITestConvention), typeof(TestConventionDelegate) });

A.CallTo(() => convention1.Register(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => convention2.Register(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => convention3.Register(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => convention1.Register(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => convention2.Register(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => convention3.Register(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand All @@ -158,7 +158,7 @@ public void RegisterShouldCallDelegate()

composer.Register(context, new[] { typeof(ITestConvention), typeof(TestConventionDelegate) });

A.CallTo(() => @delegate.Invoke(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => @delegate.Invoke(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand All @@ -183,9 +183,9 @@ public void RegisterShouldCallDelegates()

composer.Register(context, new[] { typeof(ITestConvention), typeof(TestConventionDelegate) });

A.CallTo(() => @delegate1.Invoke(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => @delegate2.Invoke(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => @delegate3.Invoke(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => @delegate1.Invoke(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => @delegate2.Invoke(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => @delegate3.Invoke(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
}

[Fact]
Expand All @@ -208,8 +208,8 @@ public void RegisterShouldCallConventionsAndDelegates()

composer.Register(context, new[] { typeof(ITestConvention), typeof(TestConventionDelegate) });

A.CallTo(() => convention.Register(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => @delegate.Invoke(A<ITestConventionContext>._)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => convention.Register(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
A.CallTo(() => @delegate.Invoke(A<ITestConventionContext>._)).MustHaveHappenedOnceExactly();
}
}
}
1 change: 1 addition & 0 deletions test/Conventions.Tests/ConventionScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using FakeItEasy;
using FluentAssertions;
using Microsoft.Extensions.Logging;
Expand Down
111 changes: 111 additions & 0 deletions test/Conventions.Tests/DiagnosticListenerLoggingAdapterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Diagnostics;
using FakeItEasy;
using Microsoft.Extensions.Logging;
using Rocket.Surgery.Extensions.Testing;
using Xunit;
using Xunit.Abstractions;

namespace Rocket.Surgery.Conventions.Tests
{
public class DiagnosticListenerLoggingAdapterTests : AutoTestBase
{
public DiagnosticListenerLoggingAdapterTests(ITestOutputHelper outputHelper) : base(outputHelper){}

[Fact]
public void LogDebug()
{
var adapter = AutoFake.Resolve<DiagnosticListenerLoggingAdapter>();
var source = new DiagnosticListener("Test");
using (source.SubscribeWithAdapter(adapter))
{
var logger = new DiagnosticLogger(source);

logger.LogDebug("test");

A.CallTo(() => Logger.Log(LogLevel.Debug, A<EventId>._, A<object>._, A<Exception>._, A<Func<object, Exception, string>>._))
.MustHaveHappened();
}
}

[Fact]
public void LogTrace()
{
var adapter = AutoFake.Resolve<DiagnosticListenerLoggingAdapter>();
var source = new DiagnosticListener("Test");
using (source.SubscribeWithAdapter(adapter))
{
var logger = new DiagnosticLogger(source);

logger.LogTrace("test");

A.CallTo(() => Logger.Log(LogLevel.Trace, A<EventId>._, A<object>._, A<Exception>._, A<Func<object, Exception, string>>._))
.MustHaveHappened();
}
}

[Fact]
public void LogInformation()
{
var adapter = AutoFake.Resolve<DiagnosticListenerLoggingAdapter>();
var source = new DiagnosticListener("Test");
using (source.SubscribeWithAdapter(adapter))
{
var logger = new DiagnosticLogger(source);

logger.LogInformation("test");

A.CallTo(() => Logger.Log(LogLevel.Information, A<EventId>._, A<object>._, A<Exception>._, A<Func<object, Exception, string>>._))
.MustHaveHappened();
}
}

[Fact]
public void LogCritical()
{
var adapter = AutoFake.Resolve<DiagnosticListenerLoggingAdapter>();
var source = new DiagnosticListener("Test");
using (source.SubscribeWithAdapter(adapter))
{
var logger = new DiagnosticLogger(source);

logger.LogCritical("test");

A.CallTo(() => Logger.Log(LogLevel.Critical, A<EventId>._, A<object>._, A<Exception>._, A<Func<object, Exception, string>>._))
.MustHaveHappened();
}
}

[Fact]
public void LogError()
{
var adapter = AutoFake.Resolve<DiagnosticListenerLoggingAdapter>();
var source = new DiagnosticListener("Test");
using (source.SubscribeWithAdapter(adapter))
{
var logger = new DiagnosticLogger(source);

logger.LogError("test");

A.CallTo(() => Logger.Log(LogLevel.Error, A<EventId>._, A<object>._, A<Exception>._, A<Func<object, Exception, string>>._))
.MustHaveHappened();
}
}

[Fact]
public void LogWarning()
{
var adapter = AutoFake.Resolve<DiagnosticListenerLoggingAdapter>();
var source = new DiagnosticListener("Test");
using (source.SubscribeWithAdapter(adapter))
{
var logger = new DiagnosticLogger(source);

logger.LogWarning("test");

A.CallTo(() => Logger.Log(LogLevel.Warning, A<EventId>._, A<object>._, A<Exception>._, A<Func<object, Exception, string>>._))
.MustHaveHappened();
}
}
}
}
Loading

0 comments on commit 9897fb3

Please sign in to comment.