forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionServices.cs
More file actions
102 lines (89 loc) · 4.07 KB
/
ExceptionServices.cs
File metadata and controls
102 lines (89 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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
{
/// <summary>Creates exception services to call logging and handling from catch blocks.</summary>
public static class ExceptionServices
{
/// <summary>Gets an exception logger that calls all registered logger services.</summary>
/// <param name="configuration">The configuration.</param>
/// <returns>A composite logger.</returns>
public static IExceptionLogger GetLogger(HttpConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
ServicesContainer services = configuration.Services;
Contract.Assert(services != null);
return GetLogger(services);
}
/// <summary>Gets an exception logger that calls all registered logger services.</summary>
/// <param name="services">The services container.</param>
/// <returns>A composite logger.</returns>
public static IExceptionLogger GetLogger(ServicesContainer services)
{
if (services == null)
{
throw new ArgumentNullException("services");
}
Lazy<IExceptionLogger> exceptionServicesLogger = services.ExceptionServicesLogger;
Contract.Assert(exceptionServicesLogger != null);
return exceptionServicesLogger.Value;
}
internal static IExceptionLogger CreateLogger(ServicesContainer services)
{
Contract.Assert(services != null);
IEnumerable<IExceptionLogger> loggers = services.GetExceptionLoggers();
Contract.Assert(loggers != null);
return new CompositeExceptionLogger(loggers);
}
/// <summary>
/// Gets an exception handler that calls the registered handler service, if any, and ensures exceptions do not
/// accidentally propagate to the host.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <returns>
/// An exception handler that calls any registered handler and ensures exceptions do not accidentally propagate
/// to the host.
/// </returns>
public static IExceptionHandler GetHandler(HttpConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
ServicesContainer services = configuration.Services;
Contract.Assert(services != null);
return GetHandler(services);
}
/// <summary>
/// Gets an exception handler that calls the registered handler service, if any, and ensures exceptions do not
/// accidentally propagate to the host.
/// </summary>
/// <param name="services">The services container.</param>
/// <returns>
/// An exception handler that calls any registered handler and ensures exceptions do not accidentally propagate
/// to the host.
/// </returns>
public static IExceptionHandler GetHandler(ServicesContainer services)
{
if (services == null)
{
throw new ArgumentNullException("services");
}
Lazy<IExceptionHandler> 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);
}
}
}