![itrace writer itrace writer](https://i.pinimg.com/originals/bb/1a/8c/bb1a8c5e9105e5f899e9dfa42f2c2e34.jpg)
Since in some cases it would be null, we’d only go into our per-request tracing path when it isn’t. The core activity happens inside the Trace method – into which Web API will pass the current instance of the HttpRequestMessage. Return string.Format("", record.Level, message) ĭespite a deceiving complexity, the trace writer is really simple. If (record.Exception != null & !string.IsNullOrWhiteSpace(().Message)) Message.Append(" ").Append(record.Message) If (!string.IsNullOrWhiteSpace(record.Message)) Message.Append(" ").Append(record.Operator).Append(" ").Append(record.Operation) If (!string.IsNullOrWhiteSpace(record.Operator)) Message.Append(" ").Append(record.Category) If (!string.IsNullOrWhiteSpace(record.Category)) Private static string Log(TraceRecord record) _innerWriter.Trace(request, category, level, traceAction) (perRequestTrace as List).Add(Log(record)) Var record = new TraceRecord(request, category, level) If (!("perRequestTrace", out perRequestTrace)) If (traceQueryString != null & Boolean.TryParse(traceQueryString, out shouldTrace) & shouldTrace) Var traceQueryString = request.GetQueryString("trace") Public void Trace(HttpRequestMessage request, string category, TraceLevel level, Action traceAction) Public PerRequestTraceWriter(ITraceWriter innerWriter) Public PerRequestTraceWriter() : this(null) Private readonly ITraceWriter _innerWriter Public class PerRequestTraceWriter : ITraceWriter Let’s create an implementation of an ITraceWriter, which, instead of logging everything to some centralized target, would instead write all of the trace information to the current Request.Properties dictionary.Īt the end of the execution pipeline, with the help of a custom MessageHandler, we could pick up this information and attach to the outgoing HttpResponseMessage. Writing a custom per-request ITraceWriter This allows us to use it as a per-request storage – and the Properties dictionary on it is perfect just for that purpose. While Web API doesn’t use any magical context object like we are used to in ASP.NET with HttpContext.Current, it uses the next best thing – a “god-like” HttpRequestMessage, which is not static but an instance of which is created for each HTTP request and then passed around between almost all important activities in the processing pipeline.
![itrace writer itrace writer](https://edshelf.com/wp-content/uploads/screenshot-itrace-1.jpg)
Request.Properties for per-request storage But what if you wanted all the trace information produced while the given request was being processed, to be returned together with the server response? However, any existing instance of an ITraceWriter would log all of the information aside – into a log file, trace, database. This will log all important events in the pipeline – such as selection of the controller, action, parameter binding and so on – all of which are extremely important in debugging all kinds of issues. Web API allows you to plug in extensive logging mechanism through the ITraceWriter service.