ASP.NET Web Api 2 - Log every incoming request for specific actions -


i working asp.net web api 2. have written 2 filter attributes check every incoming request specific headers , if header keys not present return unauthorized response users.

this doing (one of filters):

public class headerfilterattribute : authorizationfilterattribute     {         public override void onauthorization(httpactioncontext actioncontext)         {             var req = actioncontext.request;               if (! req.headers.contains("api-key") || req.headers.getvalues("api-key") == null)             {                 actioncontext.response = req.createresponse(httpstatuscode.unauthorized);                 actioncontext.response.content = new stringcontent("token required", encoding.utf8, "text/html");             }         } } 

now if request contains valid header keys , have reached correct action method or endpoint, want log details request.

what right way create filter attribute scenario? method filter attribute override? or can achieve desired result without creating filter attribute. think not make sense override onauthorization()

make new filter extending actionfilterattribute. authorization filters run first logging filter logs accepted requests.

public class mylogfilterattribute : actionfilterattribute {     public override onactionexecuting(httpactioncontext actioncontext)     {         // log info          base.onactionexecuting(actioncontext);     } } 

add base controller class log actions.

[headerfilter] [mylogfilter] public abstract class baseapicontroller : apicontroller { }  public class myapicontroller : baseapicontroller { } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -