简体   繁体   中英

How to enable JSON.NET serialization/deserialization tracing in asp.net owin api project?

I followed the JSON.NET blog - enable tracing .

It only shows how to enable tracing when you are using one of their object to manually deserialize. However, in asp.net OWIN, the serialization/deserialization is done by the framework and I wanted to know if there is any way to hook the tracing to asp.net OWIN stack for JSON.NET?

Any help is appreciated.

you can enable tracing like this

public class SimpleTracer : ITraceWriter
{
    public void Trace(HttpRequestMessage request, string category, TraceLevel level, 
        Action<TraceRecord> traceAction)
    {
        TraceRecord rec = new TraceRecord(request, category, level);
        traceAction(rec);
        WriteTrace(rec);
    }

     //here you can write logic for json serialization/deserialization    
    protected void WriteTrace(TraceRecord rec)
    {
        var message = string.Format("{0};{1};{2}", 
            rec.Operator, rec.Operation, rec.Message);
        System.Diagnostics.Trace.WriteLine(message, rec.Category);
    }
}

plug in your tracing

 config.Services.Replace(typeof(ITraceWriter), new SimpleTracer());

Based on : Tracing in ASP.NET Web API 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM