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

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Documentation/TraceWriterTests.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
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
#if !(NET35 || NET20 || PORTABLE)
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.ComponentModel;
 
30
using System.Diagnostics;
 
31
using System.Dynamic;
 
32
using System.IO;
 
33
using System.Linq;
 
34
using System.Net;
 
35
using System.Reflection;
 
36
using System.Runtime.Serialization;
 
37
using System.Text;
 
38
using System.Threading.Tasks;
 
39
using Newtonsoft.Json.Converters;
 
40
using Newtonsoft.Json.Linq;
 
41
#if !NETFX_CORE
 
42
using NUnit.Framework;
 
43
#else
 
44
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
45
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
46
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
47
#endif
 
48
using Newtonsoft.Json.Serialization;
 
49
using Newtonsoft.Json.Tests.Serialization;
 
50
using Newtonsoft.Json.Tests.TestObjects;
 
51
using Newtonsoft.Json.Utilities;
 
52
using System.Globalization;
 
53
 
 
54
namespace Newtonsoft.Json.Tests.Documentation
 
55
{
 
56
  [TestFixture]
 
57
  public class TraceWriterTests : TestFixtureBase
 
58
  {
 
59
    public class LogEventInfo
 
60
    {
 
61
      public LogLevel Level;
 
62
      public string Message;
 
63
      public Exception Exception;
 
64
    }
 
65
 
 
66
    public class LogLevel
 
67
    {
 
68
      public static LogLevel Info;
 
69
      public static LogLevel Trace;
 
70
      public static LogLevel Error;
 
71
      public static LogLevel Warn;
 
72
      public static LogLevel Off;
 
73
    }
 
74
 
 
75
    public class Logger
 
76
    {
 
77
      public void Log(LogEventInfo logEvent)
 
78
      { 
 
79
      }
 
80
    }
 
81
 
 
82
    public static class LogManager
 
83
    {
 
84
      public static Logger GetLogger(string className)
 
85
      {
 
86
        return new Logger();
 
87
      }
 
88
    }
 
89
 
 
90
    #region CustomTraceWriterExample
 
91
    public class NLogTraceWriter : ITraceWriter
 
92
    {
 
93
      private static readonly Logger Logger = LogManager.GetLogger("NLogTraceWriter");
 
94
 
 
95
      public TraceLevel LevelFilter
 
96
      {
 
97
        // trace all messages. nlog can handle filtering
 
98
        get { return TraceLevel.Verbose; }
 
99
      }
 
100
 
 
101
      public void Trace(TraceLevel level, string message, Exception ex)
 
102
      {
 
103
        LogEventInfo logEvent = new LogEventInfo
 
104
        {
 
105
          Message = message,
 
106
          Level = GetLogLevel(level),
 
107
          Exception = ex
 
108
        };
 
109
 
 
110
        // log Json.NET message to NLog
 
111
        Logger.Log(logEvent);
 
112
      }
 
113
 
 
114
      private LogLevel GetLogLevel(TraceLevel level)
 
115
      {
 
116
        switch (level)
 
117
        {
 
118
          case TraceLevel.Error:
 
119
            return LogLevel.Error;
 
120
          case TraceLevel.Warning:
 
121
            return LogLevel.Warn;
 
122
          case TraceLevel.Info:
 
123
            return LogLevel.Info;
 
124
          case TraceLevel.Off:
 
125
            return LogLevel.Off;
 
126
          default:
 
127
            return LogLevel.Trace;
 
128
        }
 
129
      }
 
130
    }
 
131
    #endregion
 
132
 
 
133
    [Test]
 
134
    public void MemoryTraceWriterTest()
 
135
    {
 
136
      #region MemoryTraceWriterExample
 
137
      Staff staff = new Staff();
 
138
      staff.Name = "Arnie Admin";
 
139
      staff.Roles = new List<string> { "Administrator" };
 
140
      staff.StartDate = DateTime.Now;
 
141
 
 
142
      ITraceWriter traceWriter = new MemoryTraceWriter();
 
143
 
 
144
      JsonConvert.SerializeObject(
 
145
        staff,
 
146
        new JsonSerializerSettings { TraceWriter = traceWriter, Converters = { new JavaScriptDateTimeConverter() } });
 
147
 
 
148
      Console.WriteLine(traceWriter);
 
149
      // 2012-11-11T12:08:42.761 Info Started serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''.
 
150
      // 2012-11-11T12:08:42.785 Info Started serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'.
 
151
      // 2012-11-11T12:08:42.791 Info Finished serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'.
 
152
      // 2012-11-11T12:08:42.797 Info Started serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'.
 
153
      // 2012-11-11T12:08:42.798 Info Finished serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'.
 
154
      // 2012-11-11T12:08:42.799 Info Finished serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''.
 
155
      #endregion
 
156
 
 
157
      MemoryTraceWriter memoryTraceWriter = (MemoryTraceWriter)traceWriter;
 
158
 
 
159
      Assert.AreEqual(743, memoryTraceWriter.ToString().Length);
 
160
      Assert.AreEqual(6, memoryTraceWriter.GetTraceMessages().Count());
 
161
    }
 
162
  }
 
163
}
 
164
#endif
 
 
b'\\ No newline at end of file'