List all event log entries (C#) : Eventlog « Development « ASP.NET Tutorial






File: Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="EventReviewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Event Log:"/>
        <asp:TextBox ID="txtLog" runat="server">your Name</asp:TextBox>&nbsp;
        <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True" OnCheckedChanged="chkAll_CheckedChanged"
            Text="Get all entries for this log" /><br />
        <asp:Label ID="Label2" runat="server" Text="Source:"/>
        <asp:TextBox ID="txtSource" runat="server">DivideByZeroApp</asp:TextBox><br />
        <br />
        <asp:Button ID="cmdGet" runat="server" OnClick="cmdGet_Click" Text="Get Records" /><br />
        <br />
        <asp:Panel ID="Panel1" runat="server" BorderStyle="Groove" BorderWidth="2px" Height="168px"
            ScrollBars="Vertical" Width="488px">
            <asp:Label ID="lblResult" runat="server"></asp:Label></asp:Panel>
    
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;

public partial class EventReviewPage : System.Web.UI.Page
{
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        if (chkAll.Checked)
        {
            txtSource.Text = "";
            txtSource.Enabled = false;
        }
        else
        {
            txtSource.Enabled = true;
        }

    }
    protected void cmdGet_Click(object sender, EventArgs e)
    {
        lblResult.Text = "";

        if (!EventLog.Exists(txtLog.Text))
        {
            lblResult.Text = "The event log " + txtLog.Text;
            lblResult.Text += " doesn't exist.";
        }
        else
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            EventLog log = new EventLog(txtLog.Text);
            foreach (EventLogEntry entry in log.Entries)
            {
                if (chkAll.Checked || entry.Source == txtSource.Text)
                {
                    sb.Append("<b>Entry Type:</b> ");
                    sb.Append(entry.EntryType.ToString());
                    sb.Append("<br /><b>Message:</b> ");
                    sb.Append(entry.Message);
                    sb.Append("<br /><b>Time Generated:</b> ");
                    sb.Append(entry.TimeGenerated);
                    sb.Append("<br /><br />");
                }
            }
            lblResult.Text = sb.ToString();
        }

    }
}








9.17.Eventlog
9.17.1.Write the information to the event log. Register the event source if needed. (C#)
9.17.2.Write the exception information to the event log (C#)
9.17.3.List all event log entries (C#)
9.17.4.Catch exception and log it (C#)
9.17.5.Catch exception and log it (VB)