5

I'm searching for a feature to enable loggin/tracing in ASP.NET MVC 4 Web Api. In WCF you can activate logging and WCF writes that it is doing. Then, I can open the dump and search for errors.

I do not want to trace my web api but log that the framework is doing. If i trace/log the web api with message handlers I get no information if an error occures before my implementation.

user437899
  • 8,879
  • 13
  • 51
  • 71

2 Answers2

7

See http://www.asp.net/web-api/overview/testing-and-debugging/tracing-in-aspnet-web-api

If you provide an ITraceWriter implementation (which can be as simple as calling System.Diagnostics.Trace), the web API framework will trace what it does.

  • Mike
Mike Wasson
  • 6,572
  • 2
  • 24
  • 20
4

I'd recommend two things.

1) To get full detail of on thrown exceptions, I'd set the error policy to always. This should return more detail exception messages from your code.

  GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

I think this only works in RC though.

2) Then I would also suggest wiring up a message handler that you can use to extra request/ response info. The message handlers will run when new requests come in and just before the final responses return. There are tons of examples of this online. WebAPIContrib has one in it's GitHub repo you can take a look at as an example

3) If you want to monitor other server events, you may want to consider using asp.net health monitoring, as long as you're hosting in asp.net of course. It'll allow you to do various audits and monitor server errors.

cecilphillip
  • 11,446
  • 4
  • 36
  • 40
  • Hi, I do NOT want to use message handlers because they won't work in this case. I need a log of that the framework is doing. For example, the message handlers will not be calles if a error occures while the server-start. – user437899 Jun 21 '12 at 13:58
  • Server errors wouldn't be specific to web api then. have you looked at health monitoring? I can add it with some links to my answer – cecilphillip Jun 21 '12 at 14:19
  • 4
    You should *never* use **IncludeErrorDetailPolicy.Always** in production environment this is a Information Diclosure vulnerability: exception contain details which can be used to find a way to hack you system (see https://www.owasp.org/index.php/Information_Leak_(information_disclosure)) – Isantipov Mar 15 '13 at 14:52