@@ -54,26 +54,24 @@ public int Factorial(int n)
54
54
55
55
public class CalculatorErrorHandler : IErrorHandler
56
56
{
57
+ private ILogger _logger ;
57
58
// Provide a fault. The Message fault parameter can be replaced, or set to
58
59
// null to suppress reporting a fault.
60
+ public CalculatorErrorHandler ( ILogger < CalculatorErrorHandler > logger )
61
+ {
62
+ _logger = logger ;
63
+ }
59
64
60
65
public void ProvideFault ( Exception error , MessageVersion version , ref Message fault )
61
66
{
62
67
}
63
68
64
69
// HandleError. Log an error, then allow the error to be handled as usual.
65
70
// Return true if the error is considered as already handled
66
-
67
71
public bool HandleError ( Exception error )
68
72
{
69
- using ( TextWriter tw = File . AppendText ( @"c:\logs\error.txt" ) )
70
- {
71
- if ( error != null )
72
- {
73
- tw . WriteLine ( "Exception: " + error . GetType ( ) . Name + " - " + error . Message ) ;
74
- }
75
- tw . Close ( ) ;
76
- }
73
+ _logger . LogInformation ( "Exception: " + error . GetType ( ) . Name + " - " + error . Message ) ;
74
+
77
75
return true ;
78
76
}
79
77
}
@@ -91,18 +89,19 @@ public ErrorBehaviorAttribute(Type errorHandlerType)
91
89
void IServiceBehavior . Validate ( ServiceDescription description , ServiceHostBase serviceHostBase )
92
90
{
93
91
}
94
-
92
+
95
93
void IServiceBehavior . AddBindingParameters ( ServiceDescription description , ServiceHostBase serviceHostBase , System . Collections . ObjectModel . Collection < ServiceEndpoint > endpoints , BindingParameterCollection parameters )
96
94
{
97
95
}
98
96
99
97
void IServiceBehavior . ApplyDispatchBehavior ( ServiceDescription description , ServiceHostBase serviceHostBase )
100
98
{
101
99
IErrorHandler errorHandler ;
100
+ var serviceProvider = description . Behaviors . Find < ServiceProviderServiceBehavior > ( ) . ServiceProvider ;
102
101
103
102
try
104
103
{
105
- errorHandler = ( IErrorHandler ) Activator . CreateInstance ( ErrorHandlerType ) ;
104
+ errorHandler = ( IErrorHandler ) ActivatorUtilities . CreateInstance ( serviceProvider , ErrorHandlerType ) ;
106
105
}
107
106
catch ( MissingMethodException e )
108
107
{
0 commit comments