ASP.NET Tutorial/Development/Eventlog
Содержание
Catch exception and log it (C#)
<%@ Page %>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
int a=0;
System.Diagnostics.EventLog myEventLog;
try {
a=1/a;
} catch (Exception ex) {
myEventLog=new System.Diagnostics.EventLog();
myEventLog.Log="Application";
myEventLog.Source="asp";
myEventLog.WriteEntry(ex.ToString());
}
}
</script>
<html>
<body>
<form id="form1" method="post" runat="server">
</form>
</body>
</html>
Catch exception and log it (VB)
<%@ Page %>
<script language="vb" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim a as Integer=0
Dim myEventLog as System.Diagnostics.EventLog
Try
a=1/a
Catch ex as Exception
myEventLog=new System.Diagnostics.EventLog()
myEventLog.Log="Application"
myEventLog.Source="asp"
myEventLog.WriteEntry(ex.ToString())
End Try
End Sub
</script>
<html>
<body>
<form id="form1" method="post" runat="server">
</form>
</body>
</html>
List all event log entries (C#)
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>
<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();
}
}
}
Write the exception information to the event log (C#)
File: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ErrorTestLog" %>
<!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="A:"></asp:Label>
<asp:TextBox ID="txtA" runat="server">5</asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="B:"></asp:Label>
<asp:TextBox ID="txtB" runat="server">0</asp:TextBox><br />
<br />
<asp:Button ID="cmdCompute" runat="server" OnClick="cmdCompute_Click" Text="Divide A / B" /><br />
<br />
<br />
<asp:Label ID="lblResult" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></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.Drawing;
using System.Diagnostics;
public partial class ErrorTestLog : System.Web.UI.Page
{
protected void cmdCompute_Click(object sender, EventArgs e)
{
try
{
decimal a, b, result;
a = Decimal.Parse(txtA.Text);
b = Decimal.Parse(txtB.Text);
result = a / b;
lblResult.Text = result.ToString();
lblResult.ForeColor = Color.Black;
}
catch (Exception err)
{
lblResult.Text = "<b>Message:</b> " + err.Message + "<br /><br />";
lblResult.Text += "<b>Source:</b> " + err.Source + "<br /><br />";
lblResult.Text += "<b>Stack Trace:</b> " + err.StackTrace;
lblResult.ForeColor = Color.Red;
EventLog log = new EventLog();
log.Source = "DivisionPage";
log.WriteEntry(err.Message, EventLogEntryType.Error);
}
}
}
Write the information to the event log. Register the event source if needed. (C#)
File: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ErrorTestCustomLog" %>
<!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="A:"></asp:Label>
<asp:TextBox ID="txtA" runat="server">5</asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="B:"></asp:Label>
<asp:TextBox ID="txtB" runat="server">0</asp:TextBox><br />
<br />
<asp:Button ID="cmdCompute" runat="server" OnClick="cmdCompute_Click" Text="Divide A / B" /><br />
<br />
<br />
<asp:Label ID="lblResult" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></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;
using System.Drawing;
public partial class ErrorTestCustomLog : System.Web.UI.Page
{
protected void cmdCompute_Click(object sender, EventArgs e)
{
try
{
decimal a, b, result;
a = Decimal.Parse(txtA.Text);
b = Decimal.Parse(txtB.Text);
result = a / b;
lblResult.Text = result.ToString();
lblResult.ForeColor = Color.Black;
}
catch (Exception err)
{
lblResult.Text = "<b>Message:</b> " + err.Message + "<br /><br />";
lblResult.Text += "<b>Source:</b> " + err.Source + "<br /><br />";
lblResult.Text += "<b>Stack Trace:</b> " + err.StackTrace;
lblResult.ForeColor = Color.Red;
if (!EventLog.SourceExists("DivideByZeroApp"))
{
EventLog.CreateEventSource("DivideByZeroApp", "ProseTech");
}
EventLog log = new EventLog("ProseTech");
log.Source = "DivideByZeroApp";
log.WriteEntry(err.Message, EventLogEntryType.Error);
}
}
}