Translate

Dienstag, 9. Juni 2020

Json.Net / Newtonsoft.Json debugging with writer to Serilog

A simple trace writer for Newtonsoft.Json serialization / deserialization debugging
and logging to Serilog.

Link: https://gist.github.com/SeppPenner/1968bbd4a47055471a135bdfbc58ac2b

Code:

// <copyright file="SerilogTraceWriter.cs" company="Hämmer Electronics">
// Copyright (c) 2020 All rights reserved.
// </copyright>
// <summary>
// A custom trace writer to write serialization information for Newtonsoft.Json to Serilog.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Data.Serialization
{
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

using Newtonsoft.Json.Serialization;

using Serilog;

/// <summary>
/// A custom trace writer to write serialization information for Newtonsoft.Json to Serilog.
/// </summary>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
public class SerilogTraceWriter : ITraceWriter
{
/// <summary>
/// The logger.
/// </summary>
private static readonly ILogger Logger = Log.ForContext<SerilogTraceWriter>();

/// <inheritdoc cref="ITraceWriter"/>
/// <summary>
/// Gets the <see cref="TraceLevel"/> that will be used to filter the trace messages passed to the writer.
/// For example a filter level of <see cref="TraceLevel.Info"/> will exclude <see cref="TraceLevel.Verbose"/> messages and include <see cref="TraceLevel.Info"/>,
/// <see cref="TraceLevel.Warning"/> and <see cref="TraceLevel.Error"/> messages.
/// </summary>
/// <value>The <see cref="TraceLevel"/> that will be used to filter the trace messages passed to the writer.</value>
/// <seealso cref="ITraceWriter"/>
public TraceLevel LevelFilter => TraceLevel.Verbose;

/// <inheritdoc cref="ITraceWriter"/>
/// <summary>
/// Writes the specified trace level, message and optional exception.
/// </summary>
/// <param name="level">The <see cref="TraceLevel"/> at which to write this trace.</param>
/// <param name="message">The trace message.</param>
/// <param name="ex">The trace exception. This parameter is optional.</param>
/// <seealso cref="ITraceWriter"/>
public void Trace(TraceLevel level, string message, Exception ex)
{
// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
switch (level)
{
case TraceLevel.Info: Logger.Information("Message: {message}, Exception: {exception}.", message, ex);
break;
case TraceLevel.Warning: Logger.Warning("Message: {message}, Exception: {exception}.", message, ex);
break;
case TraceLevel.Verbose: Logger.Verbose("Message: {message}, Exception: {exception}.", message, ex);
break;
case TraceLevel.Error: Logger.Error("Message: {message}, Exception: {exception}.", message, ex);
break;
case TraceLevel.Off: break;
}
}
}
}
// --------------------------------------------------------------------------------------------------------------------

Keine Kommentare:

Kommentar veröffentlichen