简体   繁体   English

如何在 ASP.NET Core Startup 中向 Newtonsoft JSON 添加自定义 ITraceWriter?

[英]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.

相关问题 如何使用ASP.NET Core 2.0和newtonsoft.json - how to work with ASP.NET Core 2.0 and newtonsoft.json ASP.NET Core 3.1 - 在启动中配置自定义 JSON - ASP.NET Core 3.1 - Configure custom JSON in Startup 如何在 ASP.NET Core 中创建自定义 AuthorizeAttribute? - How do you create a custom AuthorizeAttribute in ASP.NET Core? 如何从 Asp.NET Core 3.1 Startup 类访问 launchSettings.json 中的 `applicationUrl` 属性? - How Do You Access the `applicationUrl` Property Found in launchSettings.json from Asp.NET Core 3.1 Startup class? 在 ASP.NET Core 3.1 中使用 Newtonsoft.JSON 的自定义 JsonConverter 属性不适用于 Json.Serialize() - Custom JsonConverter attributes not working with Json.Serialize() using Newtonsoft.JSON in ASP.NET Core 3.1 如何将AWS ASP.NET Core日志记录添加到ASP.NET Core 2.0 Razor Pages应用程序? - How do you add AWS ASP.NET Core Logging to an ASP.NET Core 2.0 Razor Pages app? ASP.NET Core Identity 在应用程序启动时添加自定义用户角色 - ASP.NET Core Identity Add custom user roles on application startup 如何在ASP.NET Core中创建自定义策略AuthorizeAttribute? - How do you create a custom Policy AuthorizeAttribute in ASP.NET Core? 你如何在 ASP.NET Core 中强制使用小写路由? - How do you enforce lowercase routing in ASP.NET Core? asp.net核心3.1启动增加定时器方法 - Add timer method to asp.net core 3.1 startup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM