I have the following code:
[LayoutRenderer("http-request")] public class NLogHttpRequestLayoutRenderer : AspNetRequestPostedBody { protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) { base.DoAppend(builder, logEvent); var body = builder.ToString(); // after getting the type of the action's request model do serialization deserialization things ... } This is my nLog renderer. It will render every request body into the log system. But some bodies contain sensitive data like emails or bank cards. I want to mask the data. To do that, I need to understand what is the type of the action request. Considering, I have the following action:
[HttpPost] [Route("api/v1/payment/pay")] [MaskRequestMethod(typeof(PaymentRequest))] public Task<BankCardActionResponse> Pay([FromBody] PaymentRequest request) { if (request == null) throw new HttpResponseException(HttpStatusCode.BadRequest); return _paymentService.PayAsync(SsoHelper.Ctn, request); } The question is, how can I get into the renderer the MethodInfo of the action if I have the HttpContext. Because if I get the MethodInfo I can retrieve the attribute [MaskRequestMethod(typeof(PaymentRequest))] and get the Type from the attribute. Having that Type, I can deserialize JSON body, mask it according to programmed in advance rules and serialize it again. That was the short explanation why do I need it at all.
So, the question: if I have the HttpContext can I get the MethodInfo of the action that is going to be executed?
HttpContextAccessor.HttpContext?.GetRouteData()?.Values?["action"]?.ToString(). See also: github.com/NLog/NLog/wiki/AspNet-MVC-Action-Layout-Renderer