Web based Event Log (contributed by Gregory Lomax)

In the next 5 minute , I will demonstrate how we can develop Web based Event Log application
using Visual WebGui.

image 

Let's open a new Visual WebGui Application project and Name it "EventLog"

image

image

Open the Form1 designer and add a button using the drag and drop, set the button attributes
as follow Name: btnShow, Text: Event Log

image

Add a new Form and name it EventLogDialog

image

image

Open the form designer ,use the drag an drop functionality to create the following 
form (SplitContainer, TreeView and ListView.

image

Add a "txt" file as an embedded resource to the project root and name it EventLogEntry.txt
open it and add the following text

Event Date : #EVENT_DATE#
Event Time : #EVENT_TIME#
Event Type : #EVENT_TYPE#
Source : #EVENT_SOURCE#
Category : #EVENT_CATEGORY#
Event ID : #EVENT_ID#
User : #USER#
Description:
#DESCRIPTION#

Add a new Form and name it EventEntryDialog, switch to the form designer and edit
the form as shown here.

image

register the following controls events

this.btnOk.Click += new System.EventHandler(this.btnOk_Click);

this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);

Switch to the "View Code" mode and add the following code

        public EventEntryDialog(EventLogEntry entry)

        {

            InitializeComponent();

            this.lblCategory.Text = entry.Category;

            this.lblComputer.Text = entry.MachineName;

            this.lblDate.Text = entry.TimeWritten.ToString("MM/dd/yyyy");

            this.lblEventID.Text = entry.InstanceId.ToString();

            this.lblSource.Text = entry.Source;

            this.lblTime.Text = entry.TimeWritten.ToString("hh:mm:ss tt");

            this.lblType.Text = entry.EntryType.ToString();

            this.lblUser.Text = entry.UserName;

            this.txtDescription.Text = entry.Message;

        }

 

        private void btnOk_Click(object sender, EventArgs e)

        {

            this.DialogResult = DialogResult.OK;

            this.Close();

        }

 

        private void btnCopy_Click(object sender, EventArgs e)

        {

            StringBuilder Text = new StringBuilder( GetEmbeddedResource("EventLogEntry.txt"));

            Text = Text.Replace("#EVENT_DATE#", this.lblDate.Text);

            Text = Text.Replace("#EVENT_TIME#", this.lblTime.Text);

            Text = Text.Replace("#EVENT_TYPE#", this.lblType.Text);

            Text = Text.Replace("#EVENT_SOURCE#", this.lblSource.Text);

            Text = Text.Replace("#EVENT_CATEGORY#", this.lblCategory.Text);

            Text = Text.Replace("#EVENT_ID#", this.lblEventID.Text);

            Text = Text.Replace("#USER#", this.lblUser.Text);

            Text = Text.Replace("#DESCRIPTION#", this.txtDescription.Text);

            Gizmox.WebGUI.Forms.Clipboard.SetData(Gizmox.WebGUI.Forms.DataFormats.Text, Text.ToString());

            Gizmox.WebGUI.Forms.Clipboard.Update(Gizmox.WebGUI.Forms.TextDataFormat.Text);

        }

 

        private string GetEmbeddedResource(string ResourceName)

        {

            Assembly ThisAssembly = Assembly.GetExecutingAssembly();

            System.IO.Stream resStream = ThisAssembly.GetManifestResourceStream
                             (ThisAssembly.GetName().Name + "." + ResourceName);

            if (resStream == null)

            {

                throw new Exception("Can't load resource " + ResourceName);

            }

            byte[] Bytes = new Byte[resStream.Length];

            resStream.Read(Bytes, 0, (int)resStream.Length);

            resStream.Close();

            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

            return enc.GetString(Bytes);

        }

the btnCopy and GetEmbeddedResource functions are used in this case to search and replace the data according
to the template file found in the root project, after replacing the data according to the template the template data 
is copied to the windows clipboard.

Open the EventLogDialog form designer and register the following control event

this.lvwEvents.DoubleClick += new System.EventHandler(this.lvwEvents_DoubleClick);

Open the EventLogDialog form in "View Code" mode and add the following code.

        public EventLogDialog()

        {

            InitializeComponent();

 

            this.Load += new EventHandler(EventLogDialog_Load);

        }

 

        private void EventLogDialog_Load(object sender, EventArgs e)

        {

            InitializeTreeView();

        }

 

        private void InitializeTreeView()

        {

            TreeNode root = new TreeNode(Environment.MachineName);

            this.tvwLogs.Nodes.Add(root);

            System.Diagnostics.EventLog[] EventLogs =  EventLog.GetEventLogs();

            foreach (EventLog log in EventLogs)

            {

                TreeNode node = new TreeNode(log.LogDisplayName);

                node.Tag = log;

                root.Nodes.Add(node);

            }

            this.tvwLogs.NodeMouseClick += new TreeNodeMouseClickEventHandler(tvwLogs_NodeMouseClick);

        }

 

        private void tvwLogs_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)

        {

            InitializeListView((EventLog)(e.Node).Tag);

        }

 

        private void InitializeListView(EventLog log)

        {

            this.lvwEvents.Items.Clear();

            this.lvwEvents.Columns.Clear();

            this.lvwEvents.Columns.Add(new ColumnHeader("EntryType", "Entry Type", 120, ListViewColumnType.Text));

            this.lvwEvents.Columns.Add(new ColumnHeader("DateStamp", "Date/Time", 160, ListViewColumnType.Date));

            this.lvwEvents.Columns.Add(new ColumnHeader("Source", "Source", 120, ListViewColumnType.Text));

            this.lvwEvents.Columns.Add(new ColumnHeader("Category", "Category", 120, ListViewColumnType.Text));

            this.lvwEvents.Columns.Add(new ColumnHeader("Event", "Event", 100, ListViewColumnType.Number));

            this.lvwEvents.Columns.Add(new ColumnHeader("User", "User", 140, ListViewColumnType.Text));

            this.lvwEvents.Columns.Add(new ColumnHeader("Computer", "Computer", 140, ListViewColumnType.Text));

            this.lvwEvents.View = View.Details;

            this.lvwEvents.FullRowSelect = true;

            this.lvwEvents.ItemsPerPage = 100;

            this.lvwEvents.UseInternalPaging = true;

            foreach (EventLogEntry entry in log.Entries)

            {

                ListViewItem item = new ListViewItem(new string[] {

                        entry.EntryType.ToString(),

                        entry.TimeWritten.ToString("MM/dd/yyyy hh:mm:ss tt"),

                        entry.Source,

                        entry.Category,

                        entry.InstanceId.ToString(),

                        entry.UserName});

                item.Tag = entry;

                this.lvwEvents.Items.Add(item);

            }

        }

 

        private void lvwEvents_DoubleClick(object sender, EventArgs e)

        {

            if (this.lvwEvents.SelectedIndex >= 0)

            {

                EventLogEntry entry = (EventLogEntry)(this.lvwEvents.SelectedItem.Tag);

                EventEntryDialog dialog = new EventEntryDialog(entry);

                dialog.ShowDialog(this);

            }

        }

Finally open the Form1 designer and register the button click event

this.btnShow.Click += new System.EventHandler(this.btnShow_Click);

Now you can add the following syntax

        private void btnShow_Click(object sender, EventArgs e)

        {

            EventLogDialog dialog = new EventLogDialog();

            dialog.ShowDialog(this);

        }

After 3 forms and less then 120 lines of code we have a Web based Event Log, all you have
to do is run your application.

image 

Enjoy.

Download this Quick Start sources here.

Please visit our site www.visualwebgui.com

No Comments