Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » XML » Utilities » Downloads
 
Add your own
alternative version

Use XML for Log Files

, 22 Aug 2008 CPOL
How to use XML to write and read log files.
NRSoftware.XMLforLogFiles.zip
NRSoftware.XMLforLogFiles.2005
NRSoftware.XMLforLogFiles
bin
Debug
NRSoftware.XMLforLogFiles.csproj.user
Properties
Settings.settings
/***************************
 This code is copyright by 
 
 Norbert Ruessmann
 http://www.devtracer.com
 Mail: nruessmann@devtracer.com
 
 License : The Code Project Open License
           http://www.codeproject.com/info/cpol10.aspx
 
***************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace NRSoftware.XMLforLogFiles
{
    public class XmlLogfileStream : Stream
    {
        FileStream _fileStream = null;
        private bool _firstTime = true;
        private bool _closingTagRead = false;

        public XmlLogfileStream(string filename)
        {
            _fileStream = new FileStream(filename, FileMode.Open);
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_closingTagRead) return 0; // file completely read
            if (_firstTime)  // nothing read so far, therefore provide the opening tag
            {
                _firstTime = false;
                Byte[] data = System.Text.Encoding.ASCII.GetBytes("<x>");
                //
                // this code is not foolprof. Here I return "<x>" in one go, even if buffer does not provide enough space. Therefore the assert.
                // From experience I know that the buffer is always big enough.
                //
                Debug.Assert(data.Length <= count);
                for (int idx = 0; idx < data.Length; idx++)
                    buffer[offset + idx] = data[idx];
                return data.Length;
            }
            int bytesRead = _fileStream.Read(buffer, offset, count);
            if (bytesRead == 0 && !_closingTagRead)   // all contents of file read, but closing tag not provided yet
            {
                _closingTagRead = true;
                Byte[] data = System.Text.Encoding.ASCII.GetBytes("</x>");
                Debug.Assert(data.Length <= count);
                for (int idx = 0; idx < data.Length; idx++)
                    buffer[offset + idx] = data[idx];
                return data.Length;
            }
            return bytesRead;
        }

        protected override void Dispose(bool disposing)
        {
            if (_fileStream != null)
            {
                _fileStream.Dispose();
                _fileStream = null;
            }
            base.Dispose(disposing);
        }
        public new void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        // 
        // the foollowing methods nned to be overridden, equired by base class
        // just do default processing calling default implementations of FileStream

        public override bool CanRead
        {
            get { return _fileStream.CanRead; }
        }
        public override bool CanSeek
        {
            get { return _fileStream.CanSeek; }
        }
        public override bool CanWrite
        {
            get { return false; }
        }
        public override long Length
        {
            get { return _fileStream.Length; }
        }
        public override long Position
        {
            get
            {
                return _fileStream.Position;
            }
            set
            {
                _fileStream.Position = value;
            }
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            return _fileStream.Seek(offset, origin);
        }
        public override void Flush()
        {
            _fileStream.Flush();
        }
        public override void SetLength(long value)
        {
            _fileStream.SetLength(value);
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Norbert Ruessmann
freelance developer
Germany Germany
I studied physics at the university of cologne. After university I somehow got into software developemnt, starting with the Apple II.
Today I am focused on Windows developement using C# / .NET (Forms, ASP.NET, WPF) and sometimes C++.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 22 Aug 2008
Article Copyright 2008 by Norbert Ruessmann
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid