A simple trace writer for Newtonsoft.Json serialization / deserialization debugging and logging to Serilog.
Last active
March 17, 2022 06:16
-
-
Save SeppPenner/1968bbd4a47055471a135bdfbc58ac2b to your computer and use it in GitHub Desktop.
A simple trace writer for Newtonsoft.Json serialization / deserialization debugging and logging to Serilog.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -------------------------------------------------------------------------------------------------------------------- | |
// <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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment