[英]How do you add a custom ITraceWriter to Newtonsoft JSON in ASP.NET Core Startup?
I want to add a custom trace writer to help with debugging JSON calls.我想添加一个自定义跟踪编写器来帮助调试 JSON 调用。 I have implemented an ITraceWriter
class but can find no documentation on how to add it to this call.我已经实现了一个ITraceWriter
类,但找不到有关如何将其添加到此调用的文档。 Any help would be appreciated.任何帮助,将不胜感激。
services.AddControllers().AddNewtonsoftJson()
And the object和对象
public class JSONTraceWriter : Newtonsoft.Json.Serialization.ITraceWriter
{
public TraceLevel LevelFilter
{
get
{
return TraceLevel.Error;
}
}
TraceLevel ITraceWriter.LevelFilter => TraceLevel.Error;
public void Trace(TraceLevel level, string message, Exception ex)
{
SiAuto.Main.LogMessage($"JSONTraceWriter: {message}");
SiAuto.Main.LogException("JSONTracerWriter", ex);
}
}
You can add a custom trace writer by setting JsonSerializerSettings.TraceWriter
in MvcNewtonsoftJsonOptions.SerializerSettings
when calling AddNewtonsoftJson(this IMvcCoreBuilder, Action<MvcNewtonsoftJsonOptions>)
:您可以通过在调用AddNewtonsoftJson(this IMvcCoreBuilder, Action<MvcNewtonsoftJsonOptions>)
时在MvcNewtonsoftJsonOptions.SerializerSettings
设置JsonSerializerSettings.TraceWriter
来添加自定义跟踪AddNewtonsoftJson(this IMvcCoreBuilder, Action<MvcNewtonsoftJsonOptions>)
:
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.TraceWriter = new JSONTraceWriter();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.