ASP.Net/Page/Log Error

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

In case of page error, write log to system event log (C#)

<%@ Import Namespace="System.Diagnostics" %>
<script Language="c#" runat="server" >
  void EntrytoLog()
  {
    int[] array = new int[9];
    for(int intCounter=0; intCounter <= 9;intCounter++)
    {
       array[intCounter] = intCounter;
       Response.Write("The value of the counter is:" + intCounter + "<br>");
    }
  }
  void Page_Error(object sender, EventArgs e)
  {
    string errorMessage = "Error occurred" + Server.GetLastError();
    Server.ClearError();
    string LogName = "MyApplicationLog";
    string SourceName = "MyApplicationSource";
    if (!(EventLog.SourceExists(SourceName)))
    {
       EventLog.CreateEventSource(SourceName, LogName);
    }
    // Insert into Event Log;
    EventLog MyLog = new EventLog();
    MyLog.Source = SourceName;
    MyLog.WriteEntry(errorMessage, EventLogEntryType.Error);
  }
</script>
<%
 EntrytoLog();
%>