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
19 using System
.Text
.RegularExpressions
;
21 using NUnit
.Framework
;
24 public class StreamLoggerTests
26 private const string Name
= "Test";
28 private StreamLogger logger
;
29 private MemoryStream stream
;
34 stream
= new MemoryStream();
36 logger
= new StreamLogger(Name
, stream
);
37 logger
.Level
= LoggerLevel
.Debug
;
44 string message
= "Debug message";
45 LoggerLevel level
= LoggerLevel
.Debug
;
46 Exception exception
= null;
48 logger
.Debug(message
);
50 ValidateCall(level
, message
, exception
);
54 public void DebugWithException()
56 string message
= "Debug message 2";
57 LoggerLevel level
= LoggerLevel
.Debug
;
58 Exception exception
= new Exception();
60 logger
.Debug(message
, exception
);
62 ValidateCall(level
, message
, exception
);
68 string message
= "Info message";
69 LoggerLevel level
= LoggerLevel
.Info
;
70 Exception exception
= null;
74 ValidateCall(level
, message
, exception
);
78 public void InfoWithException()
80 string message
= "Info message 2";
81 LoggerLevel level
= LoggerLevel
.Info
;
82 Exception exception
= new Exception();
84 logger
.Info(message
, exception
);
86 ValidateCall(level
, message
, exception
);
92 string message
= "Warn message";
93 LoggerLevel level
= LoggerLevel
.Warn
;
94 Exception exception
= null;
98 ValidateCall(level
, message
, exception
);
102 public void WarnWithException()
104 string message
= "Warn message 2";
105 LoggerLevel level
= LoggerLevel
.Warn
;
106 Exception exception
= new Exception();
108 logger
.Warn(message
, exception
);
110 ValidateCall(level
, message
, exception
);
116 string message
= "Error message";
117 LoggerLevel level
= LoggerLevel
.Error
;
118 Exception exception
= null;
120 logger
.Error(message
);
122 ValidateCall(level
, message
, exception
);
126 public void ErrorWithException()
128 string message
= "Error message 2";
129 LoggerLevel level
= LoggerLevel
.Error
;
130 Exception exception
= new Exception();
132 logger
.Error(message
, exception
);
134 ValidateCall(level
, message
, exception
);
138 public void FatalError()
140 string message
= "FatalError message";
141 LoggerLevel level
= LoggerLevel
.Fatal
;
142 Exception exception
= null;
144 logger
.Fatal(message
);
146 ValidateCall(level
, message
, exception
);
150 public void FatalErrorWithException()
152 string message
= "FatalError message 2";
153 LoggerLevel level
= LoggerLevel
.Fatal
;
154 Exception exception
= new Exception();
156 logger
.Fatal(message
, exception
);
158 ValidateCall(level
, message
, exception
);
161 private void ValidateCall(LoggerLevel level
, String expectedMessage
, Exception expectedException
)
163 stream
.Seek(0, SeekOrigin
.Begin
);
165 StreamReader reader
= new StreamReader(stream
);
166 String line
= reader
.ReadLine();
168 Match match
= Regex
.Match(line
, @"^\[(?<level>[^]]+)\] '(?<name>[^']+)' (?<message>.*)$");
170 Assert
.IsTrue(match
.Success
, "StreamLogger.Log did not match the format");
171 Assert
.AreEqual(Name
, match
.Groups
["name"].Value
, "StreamLogger.Log did not write the correct Name");
172 Assert
.AreEqual(level
.ToString(), match
.Groups
["level"].Value
, "StreamLogger.Log did not write the correct Level");
173 Assert
.AreEqual(expectedMessage
, match
.Groups
["message"].Value
, "StreamLogger.Log did not write the correct Message");
175 line
= reader
.ReadLine();
177 if (expectedException
== null)
183 match
= Regex
.Match(line
, @"^\[(?<level>[^]]+)\] '(?<name>[^']+)' (?<type>[^:]+): (?<message>.*)$");
185 Assert
.IsTrue(match
.Success
, "StreamLogger.Log did not match the format");
186 Assert
.AreEqual(Name
, match
.Groups
["name"].Value
, "StreamLogger.Log did not write the correct Name");
187 Assert
.AreEqual(level
.ToString(), match
.Groups
["level"].Value
, "StreamLogger.Log did not write the correct Level");
188 Assert
.AreEqual(expectedException
.GetType().FullName
, match
.Groups
["type"].Value
, "StreamLogger.Log did not write the correct Exception Type");
189 // Assert.AreEqual(expectedException.Message, match.Groups["message"].Value, "StreamLogger.Log did not write the correct Exception Message");