diff --git a/src/JwtAuthForWebAPI.SampleSite/App_Start/WebApiConfig.cs b/src/JwtAuthForWebAPI.SampleSite/App_Start/WebApiConfig.cs index 1e18ce4..8c42453 100644 --- a/src/JwtAuthForWebAPI.SampleSite/App_Start/WebApiConfig.cs +++ b/src/JwtAuthForWebAPI.SampleSite/App_Start/WebApiConfig.cs @@ -19,16 +19,16 @@ public static void Register(HttpConfiguration config) var tokenBuilder = new SecurityTokenBuilder(); var configReader = new ConfigurationReader(); - var jwtHandlerCert = new JwtAuthenticationMessageHandler + var jwtHandlerCert = new JwtAuthenticationMessageHandler(Logger.Instance) { AllowedAudience = configReader.AllowedAudience, AllowedAudiences = configReader.AllowedAudiences, Issuer = configReader.Issuer, SigningToken = tokenBuilder.CreateFromCertificate(configReader.SubjectCertificateName), PrincipalTransformer = new SamplePrincipalTransformer() - }; - - var jwtHandlerSharedKey = new JwtAuthenticationMessageHandler + }; + + var jwtHandlerSharedKey = new JwtAuthenticationMessageHandler(Logger.Instance) { AllowedAudience = configReader.AllowedAudience, Issuer = configReader.Issuer, diff --git a/src/JwtAuthForWebAPI.SampleSite/JwtAuthForWebAPI.SampleSite.csproj b/src/JwtAuthForWebAPI.SampleSite/JwtAuthForWebAPI.SampleSite.csproj index 438b3dd..6fb1247 100644 --- a/src/JwtAuthForWebAPI.SampleSite/JwtAuthForWebAPI.SampleSite.csproj +++ b/src/JwtAuthForWebAPI.SampleSite/JwtAuthForWebAPI.SampleSite.csproj @@ -155,6 +155,7 @@ Global.asax + diff --git a/src/JwtAuthForWebAPI.SampleSite/Logger.cs b/src/JwtAuthForWebAPI.SampleSite/Logger.cs new file mode 100644 index 0000000..641a584 --- /dev/null +++ b/src/JwtAuthForWebAPI.SampleSite/Logger.cs @@ -0,0 +1,44 @@ + + +using System; +using log4net; + +namespace JwtAuthForWebAPI.SampleSite +{ + public class Logger : ILogger + { + readonly ILog _logger = LogManager.GetLogger("JwtAuthForWebAPI"); + + private static Logger instance; + + private Logger() { } + + public static Logger Instance + { + get + { + if (instance == null) + { + instance = new Logger(); + } + return instance; + } + } + + public void DebugFormat(string message, params object[] paramList) + { + _logger.DebugFormat(message,paramList); + } + + public void WarnFormat(string message, Exception ex) + { + _logger.WarnFormat(message, ex); + } + + public void ErrorFormat(string message, Exception ex) + { + _logger.WarnFormat(message, ex); + } + } +} + diff --git a/src/JwtAuthForWebAPI/ILogger.cs b/src/JwtAuthForWebAPI/ILogger.cs new file mode 100644 index 0000000..17d0fbe --- /dev/null +++ b/src/JwtAuthForWebAPI/ILogger.cs @@ -0,0 +1,35 @@ + + +using System; +using System.Diagnostics; + +namespace JwtAuthForWebAPI +{ + public interface ILogger + { + void DebugFormat(string message, params object[] paramList); + + void WarnFormat(string message, Exception ex); + + void ErrorFormat(string message, Exception ex); + } + + public class DefaultLogger : ILogger + { + public void DebugFormat(string message, params object[] paramList) + { + Trace.TraceInformation(message,paramList); + } + + public void WarnFormat(string message, Exception ex) + { + Trace.TraceWarning(message, ex.Message); + } + + public void ErrorFormat(string message, Exception ex) + { + Trace.TraceError(message, ex.Message); + } + } +} + diff --git a/src/JwtAuthForWebAPI/JwtAuthForWebAPI.csproj b/src/JwtAuthForWebAPI/JwtAuthForWebAPI.csproj index e507e60..1152f5e 100644 --- a/src/JwtAuthForWebAPI/JwtAuthForWebAPI.csproj +++ b/src/JwtAuthForWebAPI/JwtAuthForWebAPI.csproj @@ -30,9 +30,6 @@ 4 - - ..\..\lib\log4net.2.0.2\lib\net40-full\log4net.dll - @@ -53,6 +50,7 @@ + diff --git a/src/JwtAuthForWebAPI/JwtAuthenticationMessageHandler.cs b/src/JwtAuthForWebAPI/JwtAuthenticationMessageHandler.cs index 69f1bdd..a44e3a6 100644 --- a/src/JwtAuthForWebAPI/JwtAuthenticationMessageHandler.cs +++ b/src/JwtAuthForWebAPI/JwtAuthenticationMessageHandler.cs @@ -10,7 +10,6 @@ using System.Threading; using System.Threading.Tasks; using System.Web; -using log4net; namespace JwtAuthForWebAPI { @@ -21,13 +20,18 @@ namespace JwtAuthForWebAPI /// public class JwtAuthenticationMessageHandler : DelegatingHandler { - private readonly ILog _logger = LogManager.GetLogger("JwtAuthForWebAPI"); + private readonly ILogger _logger = new DefaultLogger(); /// /// String representation of the Bearer scheme, used for JWTs. /// public const string BearerScheme = "Bearer"; + public JwtAuthenticationMessageHandler(ILogger logger) : base() + { + _logger = logger; + } + public JwtAuthenticationMessageHandler() { AllowedAudience = "http://www.example.com"; @@ -130,7 +134,7 @@ protected override Task SendAsync( if (string.IsNullOrEmpty(tokenString)) { - _logger.Debug("Token not found in authorization header or request cookie"); + _logger.DebugFormat("Token not found in authorization header or request cookie"); return BaseSendAsync(request, cancellationToken); }