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

Use XML for Log Files

, 21 Aug 2008
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.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace NRSoftware.XMLforLogFiles
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            InitializeComponent();
        }

        private void buttonCreateLogfile_Click(object sender, EventArgs e)
        {
            try
            {
                string filename = textBoxNameForLogFile.Text;
                for (int i = 1; i <= 5; i++)
                {
                    WriteLogInformation(filename, string.Format("Trace Information <{0}>", i), string.Format("& some aditional Information {0}", i));
                    System.Threading.Thread.Sleep(700); // just to see some dufference in time
                }
                MessageBox.Show(this, "Done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void buttonReadLogfile_Click(object sender, EventArgs e)
        {
            try
            {
                ReadLogfileRaw(textBoxNameForLogFile.Text);
                ReadLogfileXml(textBoxNameForLogFile.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void WriteLogInformation(string filename, string info1, string info2)
        {
            StringBuilder sbuilder = new StringBuilder();
            using (StringWriter sw = new StringWriter(sbuilder))
            {
                using (XmlTextWriter w = new XmlTextWriter(sw))
                {
                    w.WriteStartElement("LogInfo");
                    w.WriteElementString("Time", DateTime.Now.ToString());
                    w.WriteElementString("Info1", info1);
                    w.WriteElementString("Info2", info2);
                    w.WriteEndElement();
                }
            }
            using (StreamWriter w = new StreamWriter(filename, true, Encoding.UTF8))
            {
                w.WriteLine(sbuilder.ToString());
            }
        }

        private void ReadLogfileRaw(string filename)
        {
            using (StreamReader rdr = new StreamReader(filename))
            {
                textBoxRawContents.Text = rdr.ReadToEnd();
            }
        }

        private void ReadLogfileXml(string filename)
        {
            using (XmlLogfileStream logfileStream = new XmlLogfileStream(filename))
            {
                DataSet ds = new DataSet();
                ds.ReadXml(logfileStream);
                dataGridViewLogfile.DataSource = ds;
                dataGridViewLogfile.DataMember = ds.Tables[0].TableName;
            }
        }



    }
}

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 | Mobile
Web04 | 2.8.140916.1 | Last Updated 22 Aug 2008
Article Copyright 2008 by Norbert Ruessmann
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid