-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #267 from DFE-Digital/logging-request-response
Log request/response with payloads
- Loading branch information
Showing
44 changed files
with
522 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace GetIntoTeachingApi.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)] | ||
public class LogRequestsAttribute : Attribute, IActionFilter | ||
{ | ||
private readonly ILogger<LogRequestsAttribute> _logger; | ||
|
||
public LogRequestsAttribute() | ||
{ | ||
var factory = LoggerFactory.Create(builder => builder.AddConsole()); | ||
_logger = factory.CreateLogger<LogRequestsAttribute>(); | ||
} | ||
|
||
public LogRequestsAttribute(ILogger<LogRequestsAttribute> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public void OnActionExecuting(ActionExecutingContext context) | ||
{ | ||
var descriptor = (ControllerActionDescriptor)context.ActionDescriptor; | ||
|
||
var messages = context.ActionArguments.Select(a => LogMessageForObject(a.Value)); | ||
var message = LogMessage("Request", descriptor, messages); | ||
_logger.LogInformation(message); | ||
} | ||
|
||
public void OnActionExecuted(ActionExecutedContext context) | ||
{ | ||
if (context.Result is ObjectResult result) | ||
{ | ||
var descriptor = (ControllerActionDescriptor)context.ActionDescriptor; | ||
var messages = new List<string> { LogMessageForObject(result.Value) }; | ||
var message = LogMessage("Response", descriptor, messages); | ||
_logger.LogInformation(message); | ||
} | ||
} | ||
|
||
private static string LogMessage(string type, ControllerActionDescriptor descriptor, IEnumerable<string> messages) | ||
{ | ||
var actionName = descriptor.ActionName; | ||
var controllerName = descriptor.ControllerName; | ||
var payload = string.Join("\n", messages.Where(m => !string.IsNullOrEmpty(m))); | ||
|
||
return $"{type} {controllerName}:{actionName} - {payload}"; | ||
} | ||
|
||
private static string LogMessageForObject(object obj) | ||
{ | ||
var type = obj.GetType().GetGenericArguments().FirstOrDefault(); | ||
|
||
if (type == null) | ||
{ | ||
type = obj.GetType(); | ||
} | ||
|
||
if (type.GetCustomAttribute<LoggableAttribute>() == null) | ||
{ | ||
return null; | ||
} | ||
|
||
return JsonConvert.SerializeObject( | ||
obj, | ||
Formatting.None, | ||
new JsonSerializerSettings | ||
{ | ||
ContractResolver = new FilterSensitiveDataContractResolver(), | ||
}); | ||
} | ||
|
||
private class FilterSensitiveDataContractResolver : DefaultContractResolver | ||
{ | ||
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) | ||
{ | ||
var property = base.CreateProperty(member, memberSerialization); | ||
|
||
property.ShouldSerialize = instance => | ||
instance.GetType().GetCustomAttribute<LoggableAttribute>() != null && | ||
member.GetCustomAttribute<SensitiveDataAttribute>() == null && | ||
member.GetCustomAttribute<System.Text.Json.Serialization.JsonIgnoreAttribute>() == null; | ||
|
||
return property; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace GetIntoTeachingApi.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] | ||
public class LoggableAttribute : Attribute | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace GetIntoTeachingApi.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] | ||
public class SensitiveDataAttribute : Attribute | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.