Skip to content

Commit 25f1706

Browse files
committed
add ILogger
1 parent 00bdb94 commit 25f1706

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

Extensibility/ErrorHandling/ErrorLogging/Service/CalculatorService.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,24 @@ public int Factorial(int n)
5454

5555
public class CalculatorErrorHandler : IErrorHandler
5656
{
57+
private ILogger _logger;
5758
// Provide a fault. The Message fault parameter can be replaced, or set to
5859
// null to suppress reporting a fault.
60+
public CalculatorErrorHandler(ILogger<CalculatorErrorHandler> logger)
61+
{
62+
_logger = logger;
63+
}
5964

6065
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
6166
{
6267
}
6368

6469
// HandleError. Log an error, then allow the error to be handled as usual.
6570
// Return true if the error is considered as already handled
66-
6771
public bool HandleError(Exception error)
6872
{
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+
7775
return true;
7876
}
7977
}
@@ -91,18 +89,19 @@ public ErrorBehaviorAttribute(Type errorHandlerType)
9189
void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
9290
{
9391
}
94-
92+
9593
void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
9694
{
9795
}
9896

9997
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
10098
{
10199
IErrorHandler errorHandler;
100+
var serviceProvider = description.Behaviors.Find<ServiceProviderServiceBehavior>().ServiceProvider;
102101

103102
try
104103
{
105-
errorHandler = (IErrorHandler)Activator.CreateInstance(ErrorHandlerType);
104+
errorHandler = (IErrorHandler)ActivatorUtilities.CreateInstance(serviceProvider, ErrorHandlerType);
106105
}
107106
catch (MissingMethodException e)
108107
{

Extensibility/ErrorHandling/ErrorLogging/Service/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
//Enable CoreWCF Services, with metadata (WSDL) support
77
builder.Services.AddServiceModelServices()
88
.AddServiceModelMetadata()
9+
.AddSingleton<IServiceBehavior, ServiceProviderServiceBehavior>()
910
.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();
1011

12+
//Add logging
13+
builder.Logging.ClearProviders();
14+
builder.Logging.AddConsole();
15+
1116
var app = builder.Build();
1217

1318
app.UseServiceModel(builder =>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.ObjectModel;
5+
using CoreWCF.Channels;
6+
7+
//this class is used to get ServiceProvider reference as ServiceBehavior for DI
8+
internal class ServiceProviderServiceBehavior : IServiceBehavior
9+
{
10+
public ServiceProviderServiceBehavior(IServiceProvider serviceProvider)
11+
{
12+
ServiceProvider = serviceProvider;
13+
}
14+
15+
public IServiceProvider ServiceProvider { get; }
16+
17+
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
18+
{
19+
}
20+
21+
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
22+
{
23+
24+
}
25+
26+
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
27+
{
28+
}
29+
}

0 commit comments

Comments
 (0)