Skip to content

Commit

Permalink
Issue patterns-group#92 - Fixed
Browse files Browse the repository at this point in the history
* extracted ILoggingConfig from LoggingConfig
* switched to interface usage everywhere
* modified LoggingModule logic to allow config source to be unregistered /
  config section to be missing; either case results in a default instance
  of LoggingConfig to be registered
  • Loading branch information
jbatte47 committed Aug 17, 2013
1 parent 4675e7a commit 8328d55
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/Patterns.Autofac/Logging/LoggingModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ protected override void Load(ContainerBuilder builder)
{
try
{
var configSource = context.Resolve<IConfigurationSource>();
return configSource.GetSection<LoggingConfig>(LoggingConfig.SectionName);
var configSource = context.ResolveOptional<IConfigurationSource>();
var config = configSource != null ? configSource.GetSection<LoggingConfig>(LoggingConfig.SectionName) : null;
return config ?? new LoggingConfig();
}
catch (ComponentNotRegisteredException registrationError)
{
throw ErrorBuilder.BuildContainerException(registrationError, ConfigurationResources.MissingConfigSourceErrorHint);
}
});
}).As<ILoggingConfig>();
builder.RegisterType<LoggingInterceptor>();
}

Expand Down
38 changes: 38 additions & 0 deletions src/Patterns/Logging/ILoggingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#region FreeBSD

// Copyright (c) 2013, John Batte
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion

namespace Patterns.Logging
{
/// <summary>
/// Defines configuration options for the Patterns.Logging namespace.
/// </summary>
public interface ILoggingConfig
{
/// <summary>
/// Gets or sets a value indicating whether the logging interceptor should trap exceptions
/// (as opposed to allowing them to bubble up).
/// </summary>
/// <value>
/// <c>true</c> if the logging interceptor should trap exceptions; otherwise, <c>false</c>.
/// </value>
bool TrapExceptions { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Patterns/Logging/LoggingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Patterns.Logging
/// <summary>
/// Defines configuration options for the Patterns.Logging namespace.
/// </summary>
public class LoggingConfig : ConfigurationSection
public class LoggingConfig : ConfigurationSection, ILoggingConfig
{
/// <summary>
/// The default section name.
Expand Down
4 changes: 2 additions & 2 deletions src/Patterns/Logging/LoggingInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class LoggingInterceptor : IInterceptor
private const string _argumentListFormat = "({0})";
private const string _argumentListSeparator = ",";
private const string _stringDisplayFormat = @"""{0}""";
private readonly LoggingConfig _config;
private readonly ILoggingConfig _config;
private readonly Func<Type, ILog> _logFactory;

private static readonly FuncStrategies<Type, object, object> _displayStrategies
Expand All @@ -58,7 +58,7 @@ public class LoggingInterceptor : IInterceptor
/// </summary>
/// <param name="config">The config.</param>
/// <param name="logFactory">The log factory.</param>
public LoggingInterceptor(LoggingConfig config, Func<Type, ILog> logFactory)
public LoggingInterceptor(ILoggingConfig config, Func<Type, ILog> logFactory)
{
_config = config;
_logFactory = logFactory;
Expand Down
1 change: 1 addition & 0 deletions src/Patterns/Patterns.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="Configuration\IConfigurationSource.cs" />
<Compile Include="ExceptionHandling\ExceptionState.cs" />
<Compile Include="ExceptionHandling\Try.cs" />
<Compile Include="Logging\ILoggingConfig.cs" />
<Compile Include="Logging\LoggingConfig.cs" />
<Compile Include="Logging\LoggingInterceptor.cs" />
<Compile Include="Logging\LoggingResources.Designer.cs">
Expand Down

0 comments on commit 8328d55

Please sign in to comment.