Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Changing BuildScopeImpl to BuildScope<TState>
Browse files Browse the repository at this point in the history
Closes #366
  • Loading branch information
lodejard authored and BrennanConroy committed Mar 31, 2016
1 parent 6be93eb commit 654a863
Show file tree
Hide file tree
Showing 19 changed files with 37 additions and 47 deletions.
2 changes: 1 addition & 1 deletion samples/SampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void Execute(string[] args)
_logger.LogWarning("Unexpected warning", ex);
}

using (_logger.BeginScopeImpl("Main"))
using (_logger.BeginScope("Main"))
{

_logger.LogInformation("Waiting for user input");
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Extensions.Logging.Abstractions/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public interface ILogger
/// </summary>
/// <param name="state">The identifier for the scope.</param>
/// <returns>An IDisposable that ends the logical operation scope on dispose.</returns>
IDisposable BeginScopeImpl(object state);
IDisposable BeginScope<TState>(TState state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public static IDisposable BeginScope(
throw new ArgumentNullException(nameof(messageFormat));
}

return logger.BeginScopeImpl(new FormattedLogValues(messageFormat, args));
return logger.BeginScope(new FormattedLogValues(messageFormat, args));
}

//------------------------------------------HELPERS------------------------------------------//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static Func<ILogger, IDisposable> DefineScope(string formatString)
{
var logValues = new LogValues(new LogValuesFormatter(formatString));

return logger => logger.BeginScopeImpl(logValues);
return logger => logger.BeginScope(logValues);
}

/// <summary>
Expand All @@ -35,7 +35,7 @@ public static Func<ILogger, T1, IDisposable> DefineScope<T1>(string formatString
{
var formatter = new LogValuesFormatter(formatString);

return (logger, arg1) => logger.BeginScopeImpl(new LogValues<T1>(formatter, arg1));
return (logger, arg1) => logger.BeginScope(new LogValues<T1>(formatter, arg1));
}

/// <summary>
Expand All @@ -49,7 +49,7 @@ public static Func<ILogger, T1, T2, IDisposable> DefineScope<T1, T2>(string form
{
var formatter = new LogValuesFormatter(formatString);

return (logger, arg1, arg2) => logger.BeginScopeImpl(new LogValues<T1, T2>(formatter, arg1, arg2));
return (logger, arg1, arg2) => logger.BeginScope(new LogValues<T1, T2>(formatter, arg1, arg2));
}

/// <summary>
Expand All @@ -64,7 +64,7 @@ public static Func<ILogger, T1, T2, T3, IDisposable> DefineScope<T1, T2, T3>(str
{
var formatter = new LogValuesFormatter(formatString);

return (logger, arg1, arg2, arg3) => logger.BeginScopeImpl(new LogValues<T1, T2, T3>(formatter, arg1, arg2, arg3));
return (logger, arg1, arg2, arg3) => logger.BeginScope(new LogValues<T1, T2, T3>(formatter, arg1, arg2, arg3));
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Extensions.Logging.Abstractions/LoggerOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public Logger(ILoggerFactory factory)
_logger = factory.CreateLogger(TypeNameHelper.GetTypeDisplayName(typeof(T)));
}

IDisposable ILogger.BeginScopeImpl(object state)
IDisposable ILogger.BeginScope<TState>(TState state)
{
return _logger.BeginScopeImpl(state);
return _logger.BeginScope(state);
}

bool ILogger.IsEnabled(LogLevel logLevel)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Extensions.Logging.Console/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public bool IsEnabled(LogLevel logLevel)
return Filter(Name, logLevel);
}

public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
if (state == null)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.Extensions.Logging.Debug/DebugLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public DebugLogger(string name, Func<string, LogLevel, bool> filter)


/// <inheritdoc />
public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
return new NoopDisposable();
return NoopDisposable.Instance;
}

/// <inheritdoc />
Expand Down Expand Up @@ -82,6 +82,8 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

private class NoopDisposable : IDisposable
{
public static NoopDisposable Instance = new NoopDisposable();

public void Dispose()
{
}
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.Extensions.Logging.EventLog/EventLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public EventLogLogger(string name, EventLogSettings settings)
public IEventLog EventLog { get; }

/// <inheritdoc />
public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
return new NoopDisposable();
return NoopDisposable.Instance;
}

/// <inheritdoc />
Expand Down Expand Up @@ -172,6 +172,8 @@ private EventLogEntryType GetEventLogEntryType(LogLevel level)

private class NoopDisposable : IDisposable
{
public static NoopDisposable Instance = new NoopDisposable();

public void Dispose()
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Extensions.Logging.Testing/NullLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class NullLogger : ILogger
{
public static readonly NullLogger Instance = new NullLogger();

public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
return NullDisposable.Instance;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Extensions.Logging.Testing/NullLoggerOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class NullLogger<T> : ILogger<T>
{
public static readonly NullLogger<T> Instance = new NullLogger<T>();

public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
return NullDisposable.Instance;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Extensions.Logging.Testing/TestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public TestLogger(string name, TestSink sink, bool enabled)

public string Name { get; set; }

public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
_scope = state;

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Extensions.Logging.Testing/TestLoggerOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public TestLogger(TestLoggerFactory factory)
_logger = factory.CreateLogger<T>();
}

public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
return _logger.BeginScopeImpl(state);
return _logger.BeginScope(state);
}

public bool IsEnabled(LogLevel logLevel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static TraceEventType GetEventType(LogLevel logLevel)
}
}

public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
return new TraceSourceScope(state);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.Extensions.Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public bool IsEnabled(LogLevel logLevel)
return false;
}

public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
if (_loggers == null)
{
Expand All @@ -109,7 +109,7 @@ public IDisposable BeginScopeImpl(object state)

if (_loggers.Length == 1)
{
return _loggers[0].BeginScopeImpl(state);
return _loggers[0].BeginScope(state);
}

var loggers = _loggers;
Expand All @@ -120,7 +120,7 @@ public IDisposable BeginScopeImpl(object state)
{
try
{
var disposable = loggers[index].BeginScopeImpl(state);
var disposable = loggers[index].BeginScope(state);
scope.SetDisposable(index, disposable);
}
catch (Exception ex)
Expand Down
6 changes: 3 additions & 3 deletions test/Microsoft.Extensions.Logging.Test/ConsoleLoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ public void CallingBeginScopeOnLogger_AlwaysReturnsNewDisposableInstance()
var sink = t.Item2;

// Act
var disposable1 = logger.BeginScopeImpl("Scope1");
var disposable2 = logger.BeginScopeImpl("Scope2");
var disposable1 = logger.BeginScope("Scope1");
var disposable2 = logger.BeginScope("Scope2");

// Assert
Assert.NotNull(disposable1);
Expand All @@ -633,7 +633,7 @@ public void CallingBeginScopeOnLogger_ReturnsNonNullableInstance()
var sink = t.Item2;

// Act
var disposable = logger.BeginScopeImpl("Scope1");
var disposable = logger.BeginScope("Scope1");

// Assert
Assert.NotNull(disposable);
Expand Down
18 changes: 1 addition & 17 deletions test/Microsoft.Extensions.Logging.Test/DebugLoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,14 @@ namespace Microsoft.Extensions.Logging
{
public class DebugLoggerTest
{
[Fact]
public void CallingBeginScopeOnLogger_AlwaysReturnsNewDisposableInstance()
{
// Arrange
var logger = new DebugLogger("Test");

// Act
var disposable1 = logger.BeginScopeImpl("Scope1");
var disposable2 = logger.BeginScopeImpl("Scope2");

// Assert
Assert.NotNull(disposable1);
Assert.NotNull(disposable2);
Assert.NotSame(disposable1, disposable2);
}

[Fact]
public void CallingBeginScopeOnLogger_ReturnsNonNullableInstance()
{
// Arrange
var logger = new DebugLogger("Test");

// Act
var disposable = logger.BeginScopeImpl("Scope1");
var disposable = logger.BeginScope("Scope1");

// Assert
Assert.NotNull(disposable);
Expand Down
2 changes: 1 addition & 1 deletion test/Microsoft.Extensions.Logging.Test/LoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public CustomLogger(string name, ThrowExceptionAt throwExceptionAt, List<string>
_store = store;
}

public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
if (_throwExceptionAt == ThrowExceptionAt.BeginScope)
{
Expand Down
6 changes: 4 additions & 2 deletions test/Microsoft.Extensions.Logging.Test/TestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public TestLogger(string name, TestSink sink, Func<LogLevel, bool> filter)

public string Name { get; set; }

public IDisposable BeginScopeImpl(object state)
public IDisposable BeginScope<TState>(TState state)
{
_scope = state;

Expand All @@ -36,7 +36,7 @@ public IDisposable BeginScopeImpl(object state)
Scope = state,
});

return new NoopDisposable();
return NoopDisposable.Instance;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
Expand Down Expand Up @@ -65,6 +65,8 @@ public bool IsEnabled(LogLevel logLevel)

private class NoopDisposable : IDisposable
{
public static NoopDisposable Instance = new NoopDisposable();

public void Dispose()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void BeginScope_CanDispose()
var logger = NullLogger.Instance;

// Act & Assert
using (logger.BeginScopeImpl(null))
using (logger.BeginScope("48656c6c6f20576f726c64"))
{
}
}
Expand Down

0 comments on commit 654a863

Please sign in to comment.