Click here to Skip to main content
15,896,278 members
Articles / Programming Languages / C#

Errors in XML Log with C#/.NET

Rate me:
Please Sign up or sign in to vote.
4.63/5 (10 votes)
23 Oct 20022 min read 185.7K   1K   40  
How to write all your errors when using Console.Error to an XML file.
/*
* ErrorLogWriter class: XML error log writer
*
* Authors:		R. LOPES
* Contributors:	R. LOPES
* Created:		23 October 2002
* Modified:		24 October 2002
*
* Version:		1.0
*/

using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace ErrorLogger.Utility{
	/// <summary>
	/// ErrorLogWriter class
	/// </summary>
	public class ErrorLogWriter:TextWriter{
		// Variables
		private bool Disposed;
		private XmlTextWriter Writer;

		// Constructor
		public ErrorLogWriter(string FilePath){
			Disposed=false;
			Writer=new XmlTextWriter(FilePath,Encoding.Unicode);

			// Write header
			Writer.Formatting=Formatting.Indented;
			Writer.WriteStartDocument();
			Writer.WriteStartElement("error");
			Writer.Flush();
			}

		// Destructor (equivalent to Finalize() without the need to call base.Finalize())
		~ErrorLogWriter(){
			Dispose(false);
			}

		// Free resources immediately
		protected override void Dispose(bool Disposing){
			if(!Disposed){
				if(Disposing){
					}
				// Close file
				Writer.Close();
				Writer=null;
				// Disposed
				Disposed=true;
				// Parent disposing
				base.Dispose(Disposing);
				}
			}

		// Close the file
		public override void Close(){
			// Write footer
			Writer.WriteEndElement();
			Writer.WriteEndDocument();
			Writer.Flush();
			// Free resources
			Dispose(true);
			GC.SuppressFinalize(this);
			}

		// Implement Encoding() method from TextWriter
		public override Encoding Encoding{
			get{
				return(Encoding.Unicode);
				}
			}

		// Implement WriteLine() method from TextWriter (remove MethodImpl attribute for single-threaded app)
		// Use stack trace and reflection to get the calling class and method
		[MethodImpl(MethodImplOptions.Synchronized)]
		public override void WriteLine(string Txt){
			Writer.WriteStartElement("event");
			Writer.WriteStartElement("time");
			Writer.WriteString(DateTime.Now.ToLongTimeString());
			Writer.WriteEndElement();
			Writer.WriteStartElement("class");
			Writer.WriteString(new StackTrace().GetFrame(2).GetMethod().ReflectedType.FullName);
			Writer.WriteEndElement();
			Writer.WriteStartElement("method");
			Writer.WriteString(new StackTrace().GetFrame(2).GetMethod().ToString());
			Writer.WriteEndElement();
			Writer.WriteStartElement("text");
			Writer.WriteString(Txt);
			Writer.WriteEndElement();
			Writer.WriteEndElement();
			Writer.Flush();
			}
		}
	}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Siliconz Ltd
New Zealand New Zealand
Richard Lopes
Just Programmer

Comments and Discussions