Skip to content

Commit 8f2a7c4

Browse files
committed
NLogLoggerProvider - Added constructor with isolated NLog LogFactory
1 parent 7f99b71 commit 8f2a7c4

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/NLog.Extensions.Logging/Logging/NLogLoggerProvider.cs

+20-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ namespace NLog.Extensions.Logging
1515
#endif
1616
public class NLogLoggerProvider : Microsoft.Extensions.Logging.ILoggerProvider
1717
{
18+
private NLogBeginScopeParser _beginScopeParser;
19+
1820
/// <summary>
1921
/// NLog options
2022
/// </summary>
2123
public NLogProviderOptions Options { get; set; }
22-
private NLogBeginScopeParser _beginScopeParser;
24+
25+
/// <summary>
26+
/// NLog Factory
27+
/// </summary>
28+
public LogFactory LogFactory { get; }
2329

2430
/// <summary>
2531
/// New provider with default options, see <see cref="Options"/>
@@ -34,7 +40,18 @@ public NLogLoggerProvider()
3440
/// </summary>
3541
/// <param name="options"></param>
3642
public NLogLoggerProvider(NLogProviderOptions options)
43+
:this(options, null)
44+
{
45+
}
46+
47+
/// <summary>
48+
/// New provider with options
49+
/// </summary>
50+
/// <param name="options"></param>
51+
/// <param name="logFactory">Optional isolated NLog LogFactory</param>
52+
public NLogLoggerProvider(NLogProviderOptions options, LogFactory logFactory)
3753
{
54+
LogFactory = logFactory ?? LogManager.LogFactory;
3855
Options = options ?? NLogProviderOptions.Default;
3956
_beginScopeParser = new NLogBeginScopeParser(options);
4057
RegisterHiddenAssembliesForCallSite();
@@ -50,7 +67,7 @@ public Microsoft.Extensions.Logging.ILogger CreateLogger(string name)
5067
var beginScopeParser = ((Options?.CaptureMessageProperties ?? true) && (Options?.IncludeScopes ?? true))
5168
? (_beginScopeParser ?? System.Threading.Interlocked.CompareExchange(ref _beginScopeParser, new NLogBeginScopeParser(Options), null))
5269
: null;
53-
return new NLogLogger(LogManager.GetLogger(name), Options, beginScopeParser);
70+
return new NLogLogger(LogFactory.GetLogger(name), Options, beginScopeParser);
5471
}
5572

5673
/// <summary>
@@ -70,7 +87,7 @@ protected virtual void Dispose(bool disposing)
7087
{
7188
if (disposing)
7289
{
73-
LogManager.Flush();
90+
LogFactory.Flush();
7491
}
7592
}
7693

0 commit comments

Comments
 (0)