I have a pretty standard .net core 2 WebApi project. I want to add an attribute on a method that will kick off when the action is called.
In the pseudo code below whenever someone posts to the foo
action, I'd like to log the body of the POST. To that end I add the [LogBody]
attribute. But I don't know how to actually kick off a method in the attribute.
class SomeController: Controller {
[HttpPost]
[LogBody]
public void foo([FromBody] SomeObj obj) {
return View(obj);
}
}
class LogBodyAttribute: Attribute {
void LogIt() {
string methodName = getMethodName();
string body = new StreamReader(Request.Body, Encoding.UTF8).ReadToEnd();
SaveData(methodName, body);
}
}
P.S. I know I can do this with PostSharp, but I'd rather not.