Files
Newtonsoft.Json/Src/Newtonsoft.Json/Serialization/DiagnosticsTraceWriter.cs
T
Florian Greinacher b8e60c773b Change ifdefs to be by feature, not by platform (#1146)
* First pass

* Second pass

* Third pass

* Temporarily change build script to build only portable

* Add portable features for release build

* Add feature constants to build script

* Add more feature constants to build script

* Fix build issues

* Fix build issues

* Get rid of constants in build.ps1

* HAVE_NUMERICS -> HAVE_BIG_INTEGER

* Remove HAVE_NULLABLE

* Remove HAVE_NUMERICS from csprojs

* Add missing ifdef

* Add feature defines to pj

* Re-enable netstd builds

* Fix defines in pj

* Trying to get better build outputs

* Trying to get the build error

* Use newer cli

* Address review feedback; make netstd build work

* Introduce HAVE_DB_NULL_TYPE

* Adapt to upstream changes; address remaining review feedback

* Adapt to upstream changes; address remaining review feedback

* Rename some constants for constistency

* Address review feedback

* -Fixed build with signed assembly

* Fix merge erorr

* Address review feedback

* Introduce HAVE_NON_SERIALIZED_ATTRIBUTE

* HAVE_IGNORE_DATA_MEMBER_ATTRIBUTE
2017-01-12 11:07:02 +13:00

79 lines
2.9 KiB
C#

#if HAVE_TRACE_WRITER
using System;
using System.Diagnostics;
using DiagnosticsTrace = System.Diagnostics.Trace;
namespace Newtonsoft.Json.Serialization
{
/// <summary>
/// Represents a trace writer that writes to the application's <see cref="TraceListener"/> instances.
/// </summary>
public class DiagnosticsTraceWriter : 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>
public TraceLevel LevelFilter { get; set; }
private TraceEventType GetTraceEventType(TraceLevel level)
{
switch (level)
{
case TraceLevel.Error:
return TraceEventType.Error;
case TraceLevel.Warning:
return TraceEventType.Warning;
case TraceLevel.Info:
return TraceEventType.Information;
case TraceLevel.Verbose:
return TraceEventType.Verbose;
default:
throw new ArgumentOutOfRangeException(nameof(level));
}
}
/// <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>
public void Trace(TraceLevel level, string message, Exception ex)
{
if (level == TraceLevel.Off)
{
return;
}
TraceEventCache eventCache = new TraceEventCache();
TraceEventType traceEventType = GetTraceEventType(level);
foreach (TraceListener listener in DiagnosticsTrace.Listeners)
{
if (!listener.IsThreadSafe)
{
lock (listener)
{
listener.TraceEvent(eventCache, "Newtonsoft.Json", traceEventType, 0, message);
}
}
else
{
listener.TraceEvent(eventCache, "Newtonsoft.Json", traceEventType, 0, message);
}
if (DiagnosticsTrace.AutoFlush)
{
listener.Flush();
}
}
}
}
}
#endif