1
#if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
3
using System.Diagnostics;
4
using DiagnosticsTrace = System.Diagnostics.Trace;
6
namespace Newtonsoft.Json.Serialization
9
/// Represents a trace writer that writes to the application's <see cref="TraceListener"/> instances.
11
public class DiagnosticsTraceWriter : ITraceWriter
14
/// Gets the <see cref="TraceLevel"/> that will be used to filter the trace messages passed to the writer.
15
/// For example a filter level of <code>Info</code> will exclude <code>Verbose</code> messages and include <code>Info</code>,
16
/// <code>Warning</code> and <code>Error</code> messages.
19
/// The <see cref="TraceLevel"/> that will be used to filter the trace messages passed to the writer.
21
public TraceLevel LevelFilter { get; set; }
23
private TraceEventType GetTraceEventType(TraceLevel level)
27
case TraceLevel.Error:
28
return TraceEventType.Error;
29
case TraceLevel.Warning:
30
return TraceEventType.Warning;
32
return TraceEventType.Information;
33
case TraceLevel.Verbose:
34
return TraceEventType.Verbose;
36
throw new ArgumentOutOfRangeException("level");
41
/// Writes the specified trace level, message and optional exception.
43
/// <param name="level">The <see cref="TraceLevel"/> at which to write this trace.</param>
44
/// <param name="message">The trace message.</param>
45
/// <param name="ex">The trace exception. This parameter is optional.</param>
46
public void Trace(TraceLevel level, string message, Exception ex)
48
if (level == TraceLevel.Off)
51
TraceEventCache eventCache = new TraceEventCache();
52
TraceEventType traceEventType = GetTraceEventType(level);
54
foreach (TraceListener listener in DiagnosticsTrace.Listeners)
56
if (!listener.IsThreadSafe)
60
listener.TraceEvent(eventCache, "Newtonsoft.Json", traceEventType, 0, message);
65
listener.TraceEvent(eventCache, "Newtonsoft.Json", traceEventType, 0, message);
68
if (DiagnosticsTrace.AutoFlush)
b'\\ No newline at end of file'