From d8ef42492aa828b758b7fa692a44bfc1e89335f5 Mon Sep 17 00:00:00 2001 From: Oleg Deribas Date: Thu, 14 Jul 2016 00:06:14 +0200 Subject: [PATCH] Fix exception in Log4NetTracingInterceptor default constructor (#1237) * Make it possible to instantiate Log4NetTracingInterceptor without configuration file. * Mention ServiceClientTracing.IsEnabled in README file. --- .../Log4NetTracingInterceptor.cs | 29 +++++++++++++------ .../README.md | 3 +- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/client/Microsoft.Rest.ClientRuntime.Log4Net/Log4NetTracingInterceptor.cs b/src/client/Microsoft.Rest.ClientRuntime.Log4Net/Log4NetTracingInterceptor.cs index 2882fca12b8dd..5cb49f6eb60a7 100644 --- a/src/client/Microsoft.Rest.ClientRuntime.Log4Net/Log4NetTracingInterceptor.cs +++ b/src/client/Microsoft.Rest.ClientRuntime.Log4Net/Log4NetTracingInterceptor.cs @@ -17,28 +17,39 @@ public class Log4NetTracingInterceptor : IServiceClientTracingInterceptor { private ILog _logger; + /// + /// Initializes a new instance of the class with log4net logger. + /// + /// log4net logger. + public Log4NetTracingInterceptor(ILog logger) + { + _logger = logger; + } + /// /// Initializes a new instance of the class with configuration file. /// /// The configuration file absolute path. public Log4NetTracingInterceptor(string filePath) + : this(LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)) { - _logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); - - if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) - { - log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(filePath)); - } - else + if (!string.IsNullOrEmpty(filePath)) { - throw new FileNotFoundException(filePath); + if (File.Exists(filePath)) + { + log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(filePath)); + } + else + { + throw new FileNotFoundException(filePath); + } } } /// /// Initializes a new instance of the class without configuration file. /// - public Log4NetTracingInterceptor() : this(null) + public Log4NetTracingInterceptor() : this(string.Empty) { } diff --git a/src/client/Microsoft.Rest.ClientRuntime.Log4Net/README.md b/src/client/Microsoft.Rest.ClientRuntime.Log4Net/README.md index c6c1f8a1b2d43..b7183550d27bb 100644 --- a/src/client/Microsoft.Rest.ClientRuntime.Log4Net/README.md +++ b/src/client/Microsoft.Rest.ClientRuntime.Log4Net/README.md @@ -35,7 +35,8 @@ Using Log4Net for AutoRest Generated Clients: ``` B) Passing the config file name to ```Log4NetTracingInterceptor``` constructor. -3- Last step is to register the logger into the ServiceClientTracing by having this line called at the start of the application: +3- Last step is to register the logger into the ServiceClientTracing and enable tracing by having these lines called at the start of the application: ```csharp ServiceClientTracing.AddTracingInterceptor(new Log4NetTracingInterceptor()); + ServiceClientTracing.IsEnabled = true; ``` \ No newline at end of file