1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Core
.Logging
.Tests
18 using System
.Collections
.Generic
;
20 using System
.Diagnostics
;
22 using NUnit
.Framework
;
25 /// Tests the TraceLogger and TraceLoggerFactory classes
28 public class TraceLoggerTests
31 public void Initialize()
33 Listener
.ClearMessages();
39 Listener
.ClearMessages();
43 public void WritingToLoggerByType()
45 TraceLoggerFactory factory
= new TraceLoggerFactory();
46 ILogger logger
= factory
.Create(typeof(TraceLoggerTests
), LoggerLevel
.Debug
);
47 logger
.Debug("this is a tracing message");
49 Listener
.AssertContains("testsrule", "Castle.Core.Logging.Tests.TraceLoggerTests");
50 Listener
.AssertContains("testsrule", "this is a tracing message");
54 public void TracingErrorInformation()
56 TraceLoggerFactory factory
= new TraceLoggerFactory();
57 ILogger logger
= factory
.Create(typeof(TraceLoggerTests
), LoggerLevel
.Debug
);
62 string fakearg
= "Thisisavalue";
63 throw new ArgumentOutOfRangeException("fakearg", fakearg
, "Thisisamessage" );
67 throw new ApplicationException("Inner error is " + ex
.Message
, ex
);
72 logger
.Error("Problem handled", ex
);
75 Listener
.AssertContains("testsrule", "Castle.Core.Logging.Tests.TraceLoggerTests");
76 Listener
.AssertContains("testsrule", "Problem handled");
77 Listener
.AssertContains("testsrule", "ApplicationException");
78 Listener
.AssertContains("testsrule", "Inner error is");
79 Listener
.AssertContains("testsrule", "ArgumentOutOfRangeException");
80 Listener
.AssertContains("testsrule", "fakearg");
81 Listener
.AssertContains("testsrule", "Thisisavalue");
82 Listener
.AssertContains("testsrule", "Thisisamessage");
86 public void FallUpToShorterSourceName()
88 TraceLoggerFactory factory
= new TraceLoggerFactory();
89 ILogger logger
= factory
.Create(typeof(Castle
.Core
.Configuration
.Xml
.XmlConfigurationDeserializer
), LoggerLevel
.Debug
);
90 logger
.Info("Logging to config namespace");
92 Listener
.AssertContains("configrule", "Castle.Core.Configuration.Xml.XmlConfigurationDeserializer");
93 Listener
.AssertContains("configrule", "Logging to config namespace");
97 public void FallUpToDefaultSource()
99 TraceLoggerFactory factory
= new TraceLoggerFactory();
100 ILogger logger
= factory
.Create("System.Xml.XmlDocument", LoggerLevel
.Debug
);
101 logger
.Info("Logging to non-configured namespace namespace");
103 Listener
.AssertContains("defaultrule", "System.Xml.XmlDocument");
104 Listener
.AssertContains("defaultrule", "Logging to non-configured namespace namespace");
107 #region in-memory listener class
110 /// This class captures trace text and records it to StringBuilders in a static dictionary.
111 /// Used for the sake of unit testing.
113 public class Listener
: TraceListener
119 public Listener(string initializationData
)
121 traceName
= initializationData
;
124 static Dictionary
<string, StringBuilder
> traces
= new Dictionary
<string, StringBuilder
>();
125 readonly string traceName
;
127 StringBuilder
GetStringBuilder()
131 if (!traces
.ContainsKey(traceName
))
132 traces
.Add(traceName
, new StringBuilder());
134 return traces
[traceName
];
138 public override void Write(string message
)
140 GetStringBuilder().Append(message
);
143 public override void WriteLine(string message
)
145 GetStringBuilder().AppendLine(message
);
148 public static void AssertContains(string traceName
, string expected
)
150 Assert
.IsTrue(traces
.ContainsKey(traceName
), "Trace named {0} not found", traceName
);
151 Assert
.IsTrue(traces
[traceName
].ToString().Contains(expected
), string.Format("Trace text expected to contain '{0}'", expected
));
154 public static void ClearMessages()
156 traces
= new Dictionary
<string, StringBuilder
>();