forked from elastic/apm-agent-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integration with .Net Core logging infrastructure
- Loading branch information
Vadim Hatsura
committed
Jul 11, 2019
1 parent
f9a9a8b
commit bde79cc
Showing
11 changed files
with
231 additions
and
7 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
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
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Warning" | ||
"Default": "Warning", | ||
"Elastic.Apm": "Error" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"ElasticApm": { | ||
"LogLevel": "Error", | ||
"ServerUrls": "http://localhost:8200" | ||
} | ||
} |
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
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,39 @@ | ||
using System; | ||
using Elastic.Apm.AspNetCore.Extensions; | ||
using Elastic.Apm.Logging; | ||
using Microsoft.Extensions.Logging; | ||
using LogLevel = Elastic.Apm.Logging.LogLevel; | ||
|
||
namespace Elastic.Apm.AspNetCore | ||
{ | ||
internal class AspNetCoreLogger : IApmLogger | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
public AspNetCoreLogger(ILoggerFactory loggerFactory) | ||
{ | ||
_logger = loggerFactory?.CreateLogger("Elastic.Apm") ?? throw new ArgumentNullException(nameof(loggerFactory)); | ||
Level = _logger.GetMinLogLevel(); | ||
} | ||
|
||
public LogLevel Level { get; } | ||
|
||
public void Log<TState>(LogLevel level, TState state, Exception e, Func<TState, Exception, string> formatter) => | ||
_logger.Log(Convert(level), new EventId(), state, e, formatter); | ||
|
||
private static Microsoft.Extensions.Logging.LogLevel Convert(LogLevel logLevel) | ||
{ | ||
switch (logLevel) | ||
{ | ||
case LogLevel.Trace: return Microsoft.Extensions.Logging.LogLevel.Trace; | ||
case LogLevel.Debug: return Microsoft.Extensions.Logging.LogLevel.Debug; | ||
case LogLevel.Information: return Microsoft.Extensions.Logging.LogLevel.Information; | ||
case LogLevel.Warning: return Microsoft.Extensions.Logging.LogLevel.Warning; | ||
case LogLevel.Error: return Microsoft.Extensions.Logging.LogLevel.Error; | ||
case LogLevel.Critical: return Microsoft.Extensions.Logging.LogLevel.Critical; | ||
case LogLevel.None: | ||
default: return Microsoft.Extensions.Logging.LogLevel.None; | ||
} | ||
} | ||
} | ||
} |
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
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,26 @@ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using LogLevel = Elastic.Apm.Logging.LogLevel; | ||
|
||
namespace Elastic.Apm.AspNetCore.Extensions | ||
{ | ||
internal static class LoggerExtensions | ||
{ | ||
internal static LogLevel GetMinLogLevel(this ILogger logger) | ||
{ | ||
if (logger == null) throw new ArgumentNullException(nameof(logger)); | ||
|
||
if (logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Trace)) return LogLevel.Trace; | ||
|
||
if (logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug)) return LogLevel.Debug; | ||
|
||
if (logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Information)) return LogLevel.Information; | ||
|
||
if (logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning)) return LogLevel.Warning; | ||
|
||
if (logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Error)) return LogLevel.Error; | ||
|
||
return logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Critical) ? LogLevel.Critical : LogLevel.None; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
test/Elastic.Apm.AspNetCore.Tests/ApmMiddlewareExtensionTest.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,33 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace Elastic.Apm.AspNetCore.Tests | ||
{ | ||
/// <summary> | ||
/// Tests the <see cref="Elastic.Apm.AspNetCore.ApmMiddlewareExtension"/> type. | ||
/// </summary> | ||
public class ApmMiddlewareExtensionTest | ||
{ | ||
[Fact] | ||
public void UseElasticApmShouldUseAspNetLoggerWhenLoggingIsConfigured() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddLogging(); | ||
|
||
var logger = services.BuildServiceProvider().GetApmLogger(); | ||
|
||
Assert.IsType<AspNetCoreLogger>(logger); | ||
} | ||
|
||
[Fact] | ||
public void UseElasticApmShouldUseConsoleLoggerInstanceWhenLoggingIsNotConfigured() | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
var logger = services.BuildServiceProvider().GetApmLogger(); | ||
|
||
Assert.IsType<Logging.ConsoleLogger>(logger); | ||
Assert.Same(Logging.ConsoleLogger.Instance, logger); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
test/Elastic.Apm.AspNetCore.Tests/AspNetCoreLoggerTests.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,48 @@ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Elastic.Apm.AspNetCore.Tests | ||
{ | ||
/// <summary> | ||
/// Tests the <see cref="Elastic.Apm.AspNetCore.AspNetCoreLogger"/> type. | ||
/// </summary> | ||
public class AspNetCoreLoggerTests | ||
{ | ||
[Fact] | ||
public void AspNetCoreLoggerShouldThrowExceptionWhenLoggerFactoryIsNull() | ||
=> Assert.Throws<ArgumentNullException>(() => new AspNetCoreLogger(null)); | ||
|
||
[Fact] | ||
public void AspNetCoreLoggerShouldGetLoggerFromFactoryWithProperCategoryName() | ||
{ | ||
var loggerFactoryMock = new Mock<ILoggerFactory>(); | ||
loggerFactoryMock.Setup(x => x.CreateLogger(It.IsAny<string>())) | ||
.Returns(() => Mock.Of<ILogger>()); | ||
|
||
// ReSharper disable UnusedVariable | ||
var logger = new AspNetCoreLogger(loggerFactoryMock.Object); | ||
// ReSharper restore UnusedVariable | ||
|
||
loggerFactoryMock.Verify(x => x.CreateLogger(It.Is<string>(s => s.Equals("Elastic.Apm"))), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public void AspNetCoreLoggerShouldCalculateMinLogLevelOnCreation() | ||
{ | ||
var loggerFactoryMock = new Mock<ILoggerFactory>(); | ||
var loggerMock = new Mock<ILogger>(); | ||
loggerFactoryMock.Setup(x => x.CreateLogger(It.IsAny<string>())) | ||
.Returns(() => loggerMock.Object); | ||
|
||
var logger = new AspNetCoreLogger(loggerFactoryMock.Object); | ||
|
||
loggerMock.Verify(x => x.IsEnabled(It.Is<LogLevel>(l => l == LogLevel.Trace)), Times.Once); | ||
// ReSharper disable UnusedVariable | ||
var level = logger.Level; | ||
// ReSharper restore UnusedVariable | ||
loggerMock.Verify(x => x.IsEnabled(It.Is<LogLevel>(l => l == LogLevel.Trace)), Times.Once); | ||
} | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
test/Elastic.Apm.AspNetCore.Tests/LoggerExtensionsTests.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,42 @@ | ||
using System.Collections.Generic; | ||
using Elastic.Apm.AspNetCore.Extensions; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
using LogLevel = Elastic.Apm.Logging.LogLevel; | ||
|
||
namespace Elastic.Apm.AspNetCore.Tests | ||
{ | ||
/// <summary> | ||
/// Tests the <see cref="Elastic.Apm.AspNetCore.Extensions.LoggerExtensions"/> type. | ||
/// </summary> | ||
public class LoggerExtensionsTests | ||
{ | ||
[Theory] | ||
[InlineData(Microsoft.Extensions.Logging.LogLevel.Trace, LogLevel.Trace)] | ||
[InlineData(Microsoft.Extensions.Logging.LogLevel.Debug, LogLevel.Debug)] | ||
[InlineData(Microsoft.Extensions.Logging.LogLevel.Information, LogLevel.Information)] | ||
[InlineData(Microsoft.Extensions.Logging.LogLevel.Warning, LogLevel.Warning)] | ||
[InlineData(Microsoft.Extensions.Logging.LogLevel.Error, LogLevel.Error)] | ||
[InlineData(Microsoft.Extensions.Logging.LogLevel.Critical, LogLevel.Critical)] | ||
[InlineData(Microsoft.Extensions.Logging.LogLevel.None, LogLevel.None)] | ||
public void GetMinLogLevelTest(Microsoft.Extensions.Logging.LogLevel level, LogLevel expected) | ||
{ | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(new[]{new KeyValuePair<string, string>("Logging:LogLevel:Default", level.ToString()), }) | ||
.Build(); | ||
|
||
var serviceProvider = new ServiceCollection() | ||
.AddLogging(builder => builder.AddConfiguration(configuration.GetSection("Logging")).AddConsole()) | ||
.AddOptions() | ||
.BuildServiceProvider(); | ||
|
||
var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger<LoggerExtensionsTests>(); | ||
|
||
var minLogLevel = logger.GetMinLogLevel(); | ||
|
||
Assert.Equal(expected, minLogLevel); | ||
} | ||
} | ||
} |