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
22 /// The Stream Logger class. This class can stream log information
23 /// to any stream, it is suitable for storing a log file to disk,
24 /// or to a <c>MemoryStream</c> for testing your components.
27 /// This logger is not thread safe.
32 public class StreamLogger
: LevelFilteredLogger
, IDisposable
34 private StreamWriter writer
;
37 /// Creates a new <c>StreamLogger</c> with default encoding
38 /// and buffer size. Initial Level is set to Debug.
40 /// <param name="name">
41 /// The name of the log.
43 /// <param name="stream">
44 /// The stream that will be used for logging,
45 /// seeking while the logger is alive
47 public StreamLogger(String name
, Stream stream
) : this(name
, new StreamWriter(stream
))
52 /// Creates a new <c>StreamLogger</c> with default buffer size.
53 /// Initial Level is set to Debug.
55 /// <param name="name">
56 /// The name of the log.
58 /// <param name="stream">
59 /// The stream that will be used for logging,
60 /// seeking while the logger is alive
62 /// <param name="encoding">
63 /// The encoding that will be used for this stream.
64 /// <see cref="System.IO.StreamWriter"/>
66 public StreamLogger(String name
, Stream stream
, Encoding encoding
) : this(name
, new StreamWriter(stream
, encoding
))
71 /// Creates a new <c>StreamLogger</c>.
72 /// Initial Level is set to Debug.
74 /// <param name="name">
75 /// The name of the log.
77 /// <param name="stream">
78 /// The stream that will be used for logging,
79 /// seeking while the logger is alive
81 /// <param name="encoding">
82 /// The encoding that will be used for this stream.
83 /// <see cref="System.IO.StreamWriter"/>
85 /// <param name="bufferSize">
86 /// The buffer size that will be used for this stream.
87 /// <see cref="System.IO.StreamWriter"/>
89 public StreamLogger(String name
, Stream stream
, Encoding encoding
, int bufferSize
)
90 : this(name
, new StreamWriter(stream
, encoding
, bufferSize
))
99 #region IDisposable Members
101 public void Dispose()
113 protected void Close(bool supressFinalize
)
117 GC
.SuppressFinalize(this);
129 /// Creates a new <c>StreamLogger</c> with
130 /// Debug as default Level.
132 /// <param name="name">The name of the log.</param>
133 /// <param name="writer">The <c>StreamWriter</c> the log will write to.</param>
134 protected StreamLogger(String name
, StreamWriter writer
) : base(name
, LoggerLevel
.Debug
)
136 this.writer
= writer
;
137 writer
.AutoFlush
= true;
140 protected override void Log(LoggerLevel level
, String name
, String message
, Exception exception
)
142 if (writer
== null) return; // just in case it's been disposed
144 writer
.WriteLine("[{0}] '{1}' {2}", level
.ToString(), name
, message
);
146 if (exception
!= null)
148 writer
.WriteLine("[{0}] '{1}' {2}: {3} {4}",
151 exception
.GetType().FullName
,
153 exception
.StackTrace
);
157 public override ILogger
CreateChildLogger(string name
)
159 // TODO: We could create a ChildStreamLogger that didn't take ownership of the stream
161 throw new NotSupportedException("A streamlogger does not support child loggers");