-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CallSite should recognize all Microsoft.Extensions.Logging.ILogger
- Loading branch information
Showing
6 changed files
with
142 additions
and
43 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
|
||
namespace NLog.Extensions.Logging.Tests | ||
{ | ||
public class CustomLoggerCallSiteTest : NLogTestBase | ||
{ | ||
[Fact] | ||
public void TestCallSite() | ||
{ | ||
ConfigureServiceProvider<CustomLoggerCallSiteTestRunner>((s) => s.AddSingleton(typeof(ILogger<>), typeof(SameAssemblyLogger<>))); | ||
var runner = GetRunner<CustomLoggerCallSiteTestRunner>(); | ||
|
||
var target = new NLog.Targets.MemoryTarget() { Layout = "${callsite}|${message}" }; | ||
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target); | ||
runner.SayHello(); | ||
Assert.Single(target.Logs); | ||
Assert.Contains("SayHello", target.Logs[0]); | ||
Assert.Contains("stuff", target.Logs[0]); | ||
} | ||
|
||
public class SameAssemblyLogger<T> : ILogger<T> | ||
{ | ||
private readonly Microsoft.Extensions.Logging.ILogger _logger; | ||
|
||
public SameAssemblyLogger(ILoggerFactory loggerFactory) | ||
{ | ||
_logger = loggerFactory.CreateLogger<T>(); | ||
} | ||
|
||
public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception exception, | ||
Func<TState, Exception, string> formatter) | ||
{ | ||
string Formatter(TState innserState, Exception innerException) | ||
{ | ||
// additional logic for all providers goes here | ||
var message = formatter(innserState, innerException) ?? string.Empty; | ||
return message + " additional stuff in here"; | ||
} | ||
|
||
_logger.Log(logLevel, eventId, state, exception, Formatter); | ||
} | ||
|
||
public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) | ||
{ | ||
return _logger.IsEnabled(logLevel); | ||
} | ||
|
||
public IDisposable BeginScope<TState>(TState state) | ||
{ | ||
return _logger.BeginScope(state); | ||
} | ||
} | ||
|
||
public class CustomLoggerCallSiteTestRunner | ||
{ | ||
private readonly ILogger<CustomLoggerCallSiteTestRunner> _logger; | ||
|
||
public CustomLoggerCallSiteTestRunner(ILogger<CustomLoggerCallSiteTestRunner> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public void SayHello() | ||
{ | ||
_logger.LogInformation("Hello"); | ||
} | ||
} | ||
|
||
} | ||
} |
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 System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace NLog.Extensions.Logging.Tests | ||
{ | ||
public class NLogTestBase | ||
{ | ||
IServiceProvider _serviceProvider; | ||
|
||
protected IServiceProvider ConfigureServiceProvider<T>(Action<ServiceCollection> configureServices = null, NLogProviderOptions options = null) where T : class | ||
{ | ||
if (_serviceProvider == null) | ||
{ | ||
var services = new ServiceCollection(); | ||
|
||
services.AddTransient<T>(); | ||
services.AddSingleton<ILoggerFactory, LoggerFactory>(); | ||
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); | ||
configureServices?.Invoke(services); | ||
|
||
_serviceProvider = services.BuildServiceProvider(); | ||
|
||
var loggerFactory = _serviceProvider.GetRequiredService<ILoggerFactory>(); | ||
loggerFactory.AddNLog(options ?? new NLogProviderOptions() { CaptureMessageTemplates = true, CaptureMessageProperties = true }); | ||
} | ||
return _serviceProvider; | ||
} | ||
|
||
protected T GetRunner<T>(NLogProviderOptions options = null) where T : class | ||
{ | ||
// Start program | ||
var runner = ConfigureServiceProvider<T>(null, options).GetRequiredService<T>(); | ||
return runner; | ||
} | ||
} | ||
} |
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,14 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
[assembly: Xunit.CollectionBehavior(DisableTestParallelization = true)] |