// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Web.Http.Controllers;
namespace System.Web.Http.ExceptionHandling
{
/// Creates exception services to call logging and handling from catch blocks.
public static class ExceptionServices
{
/// Gets an exception logger that calls all registered logger services.
/// The configuration.
/// A composite logger.
public static IExceptionLogger GetLogger(HttpConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
ServicesContainer services = configuration.Services;
Contract.Assert(services != null);
return GetLogger(services);
}
/// Gets an exception logger that calls all registered logger services.
/// The services container.
/// A composite logger.
public static IExceptionLogger GetLogger(ServicesContainer services)
{
if (services == null)
{
throw new ArgumentNullException("services");
}
Lazy exceptionServicesLogger = services.ExceptionServicesLogger;
Contract.Assert(exceptionServicesLogger != null);
return exceptionServicesLogger.Value;
}
internal static IExceptionLogger CreateLogger(ServicesContainer services)
{
Contract.Assert(services != null);
IEnumerable loggers = services.GetExceptionLoggers();
Contract.Assert(loggers != null);
return new CompositeExceptionLogger(loggers);
}
///
/// Gets an exception handler that calls the registered handler service, if any, and ensures exceptions do not
/// accidentally propagate to the host.
///
/// The configuration.
///
/// An exception handler that calls any registered handler and ensures exceptions do not accidentally propagate
/// to the host.
///
public static IExceptionHandler GetHandler(HttpConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
ServicesContainer services = configuration.Services;
Contract.Assert(services != null);
return GetHandler(services);
}
///
/// Gets an exception handler that calls the registered handler service, if any, and ensures exceptions do not
/// accidentally propagate to the host.
///
/// The services container.
///
/// An exception handler that calls any registered handler and ensures exceptions do not accidentally propagate
/// to the host.
///
public static IExceptionHandler GetHandler(ServicesContainer services)
{
if (services == null)
{
throw new ArgumentNullException("services");
}
Lazy exceptionServicesHandler = services.ExceptionServicesHandler;
Contract.Assert(exceptionServicesHandler != null);
return exceptionServicesHandler.Value;
}
internal static IExceptionHandler CreateHandler(ServicesContainer services)
{
Contract.Assert(services != null);
IExceptionHandler innerHandler = services.GetExceptionHandler() ?? new EmptyExceptionHandler();
return new LastChanceExceptionHandler(innerHandler);
}
}
}