Skip to content

Allow the provider to be embedded in other providers by making SerilogLoggerProvider public (WIP) #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@

namespace Serilog.Extensions.Logging
{
class SerilogLoggerProvider : ILoggerProvider, ILogEventEnricher
/// <summary>
/// An <see cref="ILoggerProvider"/> that pipes events through Serilog.
/// </summary>
public class SerilogLoggerProvider : ILoggerProvider, ILogEventEnricher
{
public const string OriginalFormatPropertyName = "{OriginalFormat}";
internal const string OriginalFormatPropertyName = "{OriginalFormat}";

// May be null; if it is, Log.Logger will be lazily used
readonly ILogger _logger;
readonly Action _dispose;

/// <summary>
/// Construct a <see cref="SerilogLoggerProvider"/>.
/// </summary>
/// <param name="logger">A Serilog logger to pipe events through; if null, the static <see cref="Log"/> class will be used.</param>
/// <param name="dispose">If true, the provided logger or static log class will be disposed/closed when the provider is disposed.</param>
public SerilogLoggerProvider(ILogger logger = null, bool dispose = false)
{
if (logger != null)
Expand All @@ -37,16 +45,19 @@ public SerilogLoggerProvider(ILogger logger = null, bool dispose = false)
}
}

/// <inheritdoc />
public FrameworkLogger CreateLogger(string name)
{
return new SerilogLogger(this, _logger, name);
}

/// <inheritdoc />
public IDisposable BeginScope<T>(T state)
{
return new SerilogLoggerScope(this, state);
}

/// <inheritdoc />
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
for (var scope = CurrentScope; scope != null; scope = scope.Parent)
Expand All @@ -58,7 +69,7 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
#if ASYNCLOCAL
readonly AsyncLocal<SerilogLoggerScope> _value = new AsyncLocal<SerilogLoggerScope>();

public SerilogLoggerScope CurrentScope
internal SerilogLoggerScope CurrentScope
{
get
{
Expand All @@ -72,7 +83,7 @@ public SerilogLoggerScope CurrentScope
#else
readonly string _currentScopeKey = nameof(SerilogLoggerScope) + "#" + Guid.NewGuid().ToString("n");

public SerilogLoggerScope CurrentScope
internal SerilogLoggerScope CurrentScope
{
get
{
Expand All @@ -86,6 +97,7 @@ public SerilogLoggerScope CurrentScope
}
#endif

/// <inheritdoc />
public void Dispose()
{
_dispose?.Invoke();
Expand Down