Friday 15 August 2014

asp.net web api - How to log which action method is executed in a controller in webapi -



asp.net web api - How to log which action method is executed in a controller in webapi -

in webapi, there anyway log name of action method controller gets called or executed using action filter. using routedata property shown below, action value not contain value. there way can action name in filter.

public class logactionfilter : actionfilterattribute { public override void onactionexecuted(httpactionexecutedcontext actionexecutedcontext) { log(actionexecutedcontext.actioncontext.requestcontext.routedata); base.onactionexecuted(actionexecutedcontext); } private void log(system.web.http.routing.ihttproutedata httproutedata) { var controllername = httproutedata.values["controller"]; var actionname = httproutedata.values["action"]; var message = string.format("controller:{0}, action:{1}", controllername, actionname); debug.writeline(message, "action filter log"); } }

you can find action name in actionexecutedcontext.actioncontext.actiondescriptor.actionname property (string).

you can cast actiondescriptor reflectedhttpactiondescriptor , obtain instance of methodinfo called, if need more info string name.

var reflectedactiondescriptor = actionexecutedcontext.actioncontext.actiondescriptor reflectedhttpactiondescriptor; //inspect reflectedactiondescriptor.methodinfo here

asp.net-web-api

No comments:

Post a Comment