~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Serialization/DiagnosticsTraceWriter.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
 
2
using System;
 
3
using System.Diagnostics;
 
4
using DiagnosticsTrace = System.Diagnostics.Trace;
 
5
 
 
6
namespace Newtonsoft.Json.Serialization
 
7
{
 
8
  /// <summary>
 
9
  /// Represents a trace writer that writes to the application's <see cref="TraceListener"/> instances.
 
10
  /// </summary>
 
11
  public class DiagnosticsTraceWriter : ITraceWriter
 
12
  {
 
13
    /// <summary>
 
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.
 
17
    /// </summary>
 
18
    /// <value>
 
19
    /// The <see cref="TraceLevel"/> that will be used to filter the trace messages passed to the writer.
 
20
    /// </value>
 
21
    public TraceLevel LevelFilter { get; set; }
 
22
 
 
23
    private TraceEventType GetTraceEventType(TraceLevel level)
 
24
    {
 
25
      switch (level)
 
26
      {
 
27
        case TraceLevel.Error:
 
28
          return TraceEventType.Error;
 
29
        case TraceLevel.Warning:
 
30
          return TraceEventType.Warning;
 
31
        case TraceLevel.Info:
 
32
          return TraceEventType.Information;
 
33
        case TraceLevel.Verbose:
 
34
          return TraceEventType.Verbose;
 
35
        default:
 
36
          throw new ArgumentOutOfRangeException("level");
 
37
      }
 
38
    }
 
39
 
 
40
    /// <summary>
 
41
    /// Writes the specified trace level, message and optional exception.
 
42
    /// </summary>
 
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)
 
47
    {
 
48
      if (level == TraceLevel.Off)
 
49
        return;
 
50
 
 
51
      TraceEventCache eventCache = new TraceEventCache();
 
52
      TraceEventType traceEventType = GetTraceEventType(level);
 
53
 
 
54
      foreach (TraceListener listener in DiagnosticsTrace.Listeners)
 
55
      {
 
56
        if (!listener.IsThreadSafe)
 
57
        {
 
58
          lock (listener)
 
59
          {
 
60
            listener.TraceEvent(eventCache, "Newtonsoft.Json", traceEventType, 0, message);
 
61
          }
 
62
        }
 
63
        else
 
64
        {
 
65
          listener.TraceEvent(eventCache, "Newtonsoft.Json", traceEventType, 0, message);
 
66
        }
 
67
 
 
68
        if (DiagnosticsTrace.AutoFlush)
 
69
          listener.Flush();
 
70
      }
 
71
    }
 
72
  }
 
73
}
 
74
#endif
 
 
b'\\ No newline at end of file'