Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Core / Castle.Core / Logging / Loggers / StreamLogger.cs
blob994c831a69c18e18162f601d93fe026fbf825cc4
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.IO;
19 using System.Text;
21 /// <summary>
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.
25 /// </summary>
26 /// <remarks>
27 /// This logger is not thread safe.
28 /// </remarks>
29 #if !SILVERLIGHT
30 [Serializable]
31 #endif
32 public class StreamLogger : LevelFilteredLogger, IDisposable
34 private StreamWriter writer;
36 /// <summary>
37 /// Creates a new <c>StreamLogger</c> with default encoding
38 /// and buffer size. Initial Level is set to Debug.
39 /// </summary>
40 /// <param name="name">
41 /// The name of the log.
42 /// </param>
43 /// <param name="stream">
44 /// The stream that will be used for logging,
45 /// seeking while the logger is alive
46 /// </param>
47 public StreamLogger(String name, Stream stream) : this(name, new StreamWriter(stream))
51 /// <summary>
52 /// Creates a new <c>StreamLogger</c> with default buffer size.
53 /// Initial Level is set to Debug.
54 /// </summary>
55 /// <param name="name">
56 /// The name of the log.
57 /// </param>
58 /// <param name="stream">
59 /// The stream that will be used for logging,
60 /// seeking while the logger is alive
61 /// </param>
62 /// <param name="encoding">
63 /// The encoding that will be used for this stream.
64 /// <see cref="System.IO.StreamWriter"/>
65 /// </param>
66 public StreamLogger(String name, Stream stream, Encoding encoding) : this(name, new StreamWriter(stream, encoding))
70 /// <summary>
71 /// Creates a new <c>StreamLogger</c>.
72 /// Initial Level is set to Debug.
73 /// </summary>
74 /// <param name="name">
75 /// The name of the log.
76 /// </param>
77 /// <param name="stream">
78 /// The stream that will be used for logging,
79 /// seeking while the logger is alive
80 /// </param>
81 /// <param name="encoding">
82 /// The encoding that will be used for this stream.
83 /// <see cref="System.IO.StreamWriter"/>
84 /// </param>
85 /// <param name="bufferSize">
86 /// The buffer size that will be used for this stream.
87 /// <see cref="System.IO.StreamWriter"/>
88 /// </param>
89 public StreamLogger(String name, Stream stream, Encoding encoding, int bufferSize)
90 : this(name, new StreamWriter(stream, encoding, bufferSize))
94 ~StreamLogger()
96 Close(false);
99 #region IDisposable Members
101 public void Dispose()
103 Close(true);
106 #endregion
108 public void Close()
110 Close(true);
113 protected void Close(bool supressFinalize)
115 if (supressFinalize)
117 GC.SuppressFinalize(this);
120 if (writer != null)
122 writer.Close();
125 writer = null;
128 /// <summary>
129 /// Creates a new <c>StreamLogger</c> with
130 /// Debug as default Level.
131 /// </summary>
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}",
149 level.ToString(),
150 name,
151 exception.GetType().FullName,
152 exception.Message,
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");