We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Diagnostics
- {
- using System;
- using System.IO;
- using System.Text;
- using System.Security.Permissions;
- using System.Runtime.InteropServices;
- using Microsoft.Win32;
- using System.Runtime.Versioning;
-
- /// <devdoc>
- /// <para>Directs tracing or debugging output to
- /// a <see cref='T:System.IO.TextWriter'/> or to a <see cref='T:System.IO.Stream'/>,
- /// such as <see cref='F:System.Console.Out'/> or <see cref='T:System.IO.FileStream'/>.</para>
- /// </devdoc>
- [HostProtection(Synchronization = true)]
- public class TextWriterTraceListener : TraceListener
- {
- internal TextWriter writer;
- string fileName = null;
-
- /// <devdoc>
- /// <para>Initializes a new instance of the <see cref='System.Diagnostics.TextWriterTraceListener'/> class with
- /// <see cref='System.IO.TextWriter'/>
- /// as the output recipient.</para>
- /// </devdoc>
- public TextWriterTraceListener()
- {
- }
-
- /// <devdoc>
- /// <para>Initializes a new instance of the <see cref='System.Diagnostics.TextWriterTraceListener'/> class, using the
- /// stream as the recipient of the debugging and tracing output.</para>
- /// </devdoc>
- public TextWriterTraceListener(Stream stream) : this(stream, string.Empty)
- {
- }
-
- /// <devdoc>
- /// <para>Initializes a new instance of the <see cref='System.Diagnostics.TextWriterTraceListener'/> class with the
- /// specified name and using the stream as the recipient of the debugging and tracing output.</para>
- /// </devdoc>
- public TextWriterTraceListener(Stream stream, string name) : base(name)
- {
- if (stream == null)
- throw new ArgumentNullException("stream");
- this.writer = new StreamWriter(stream);
- }
-
- /// <devdoc>
- /// <para>Initializes a new instance of the <see cref='System.Diagnostics.TextWriterTraceListener'/> class using the
- /// specified writer as recipient of the tracing or debugging output.</para>
- /// </devdoc>
- public TextWriterTraceListener(TextWriter writer) : this(writer, string.Empty)
- {
- }
-
- /// <devdoc>
- /// <para>Initializes a new instance of the <see cref='System.Diagnostics.TextWriterTraceListener'/> class with the
- /// specified name and using the specified writer as recipient of the tracing or
- /// debugging
- /// output.</para>
- /// </devdoc>
- public TextWriterTraceListener(TextWriter writer, string name) : base(name)
- {
- if (writer == null)
- throw new ArgumentNullException("writer");
- this.writer = writer;
- }
-
- /// <devdoc>
- /// <para>[To be supplied.]</para>
- /// </devdoc>
- [ResourceExposure(ResourceScope.Machine)]
- public TextWriterTraceListener(string fileName)
- {
- this.fileName = fileName;
- }
-
- /// <devdoc>
- /// <para>[To be supplied.]</para>
- /// </devdoc>
- [ResourceExposure(ResourceScope.Machine)]
- public TextWriterTraceListener(string fileName, string name) : base(name)
- {
- this.fileName = fileName;
- }
-
- /// <devdoc>
- /// <para> Indicates the text writer that receives the tracing
- /// or debugging output.</para>
- /// </devdoc>
- public TextWriter Writer {
- get {
- EnsureWriter();
- return writer;
- }
-
- set { writer = value; }
- }
-
- /// <devdoc>
- /// <para>Closes the <see cref='System.Diagnostics.TextWriterTraceListener.Writer'/> so that it no longer
- /// receives tracing or debugging output.</para>
- /// </devdoc>
- public override void Close()
- {
- if (writer != null)
- writer.Close();
-
- writer = null;
- }
-
- /// <internalonly/>
- /// <devdoc>
- /// </devdoc>
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- this.Close();
- }
-
- /// <devdoc>
- /// <para>Flushes the output buffer for the <see cref='System.Diagnostics.TextWriterTraceListener.Writer'/>.</para>
- /// </devdoc>
- public override void Flush()
- {
- if (!EnsureWriter())
- return;
- writer.Flush();
- }
-
- /// <devdoc>
- /// <para>Writes a message
- /// to this instance's <see cref='System.Diagnostics.TextWriterTraceListener.Writer'/>.</para>
- /// </devdoc>
- public override void Write(string message)
- {
- if (!EnsureWriter())
- return;
- if (NeedIndent)
- WriteIndent();
- writer.Write(message);
- }
-
- /// <devdoc>
- /// <para>Writes a message
- /// to this instance's <see cref='System.Diagnostics.TextWriterTraceListener.Writer'/> followed by a line terminator. The
- /// default line terminator is a carriage return followed by a line feed (\r\n).</para>
- /// </devdoc>
- public override void WriteLine(string message)
- {
- if (!EnsureWriter())
- return;
- if (NeedIndent)
- WriteIndent();
- writer.WriteLine(message);
- NeedIndent = true;
- }
-
- private static Encoding GetEncodingWithFallback(Encoding encoding)
- {
-
- Encoding fallbackEncoding = (Encoding)encoding.Clone();
- fallbackEncoding.EncoderFallback = EncoderFallback.ReplacementFallback;
- fallbackEncoding.DecoderFallback = DecoderFallback.ReplacementFallback;
-
- return fallbackEncoding;
- }
-
-
- [ResourceExposure(ResourceScope.None)]
- [ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
- internal bool EnsureWriter()
- {
- bool ret = true;
-
- if (writer == null) {
- ret = false;
-
- if (fileName == null)
- return ret;
-
-
-
-
-
-
-
- Encoding noBOMwithFallback = GetEncodingWithFallback(new UTF8Encoding(false));
-
-
-
-
-
-
- string fullPath = Path.GetFullPath(fileName);
- string dirPath = Path.GetDirectoryName(fullPath);
- string fileNameOnly = Path.GetFileName(fullPath);
-
- for (int i = 0; i < 2; i++) {
- try {
- writer = new StreamWriter(fullPath, true, noBOMwithFallback, 4096);
- ret = true;
- break;
- }
- catch (IOException) {
-
-
-
-
- fileNameOnly = Guid.NewGuid().ToString() + fileNameOnly;
- fullPath = Path.Combine(dirPath, fileNameOnly);
- continue;
- }
- catch (UnauthorizedAccessException) {
-
- break;
- }
- catch (Exception) {
- break;
- }
- }
-
- if (!ret) {
-
-
-
-
- fileName = null;
- }
- }
- return ret;
- }
-
- }
- }