Skip to content

Instantly share code, notes, and snippets.

@cbanner
Last active December 29, 2018 13:57
Show Gist options
  • Save cbanner/38e53b6afb811a2c9b89e89ea5550b0b to your computer and use it in GitHub Desktop.
Save cbanner/38e53b6afb811a2c9b89e89ea5550b0b to your computer and use it in GitHub Desktop.
Part 2: Pseudo code demonstrating how to isolate logging components through decoration.
public class LoggingAuthenticationProcedure : IAuthenticationProcedure
{
private readonly ILogger logger;
private readonly IAuthenticationProcedure innerProcedure;
public LoggingAuthenticationProcedure(ILogger logger, IAuthenticationProcedure procedure)
{
this.logger = logger;
this.innerProcedure = procedure;
}
public AuthenticationResult Authenticate(HttpRequest request)
{
AuthenticationResult result;
try
{
this.logger.Trace("Beginning signature authentication procedure.")
result = this.innerProcedure.Authenticate(request);
if(result == AuthenticationResult.Successful)
{
this.logger.Information("Authentication successful.")
}
}
catch(Exception ex)
{
this.logger.Error(ex);
throw;
}
finally
{
this.logger.Trace("Completed signature authentication procedure.");
}
return result;
}
}
public class SignatureAuthenticationProcedure : IAuthenticationProcedure
{
public AuthenticationResult Authenticate(HttpRequest request)
{
AuthenticationResult result = AuthenticationResult.Failed;
if(IsTimestampValid(request) && IsSignatureValid(request))
{
result = AuthenticationResult.Success;
}
return result;
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment