How to write an event log in asp.net

Source: Internet
Author: User
Tags net command visual studio
The asp.net text contains information about editing the registry. Before you edit the registry, be sure to understand how to restore the registry if a problem occurs. For information about how to restore the registry, see the "Restore Registry" Help topic in Regedit.exe or the "Restore registry key" Help topic in Regedt32.exe.
Phenomenon

When you use ASP.net to write a new "event source" to the event log, you may receive the following error message: System.Security.SecurityException: The requested registry access is not allowed

Reason

The default despair for running the asp.net process is the ASPNET (NetworkService below IIS6.0), and this user does not have permission to create an "event source."

Solutions

Note: (Editing the registry can cause a system crash, and so on, Microsoft won't say much). If you need to resolve this issue, before you run this asp.net program, you must create an "event source" by a user with administrator privileges. Here are a few ways to create an "event source."

First method

Use the following procedure to create an "event source" under Application log in Registry editing

1. Click "Start" and then click "Run".

2. Enter "regedit" in the Open box.

3. Locate the following subkeys:

Hkey_local_machine\system\currentcontrolset\services\eventlog\application

4. Right-click "Application" and click "New" and Point "item"

5. Rename this new item to "Test"

6. Close Registry Editor



A second method

There is a EventLogInstaller class in the System.Diagnostics namespace. It can create and configure event logs to be read and written by your application when it is shipped. With the following steps, we can use the EventLogInstaller class to create an event industry source

1. Use vb.net or C # to create a class library called EventLogSourceInstaller.

2. Add a reference to the System.Configuration.Install.dll in your project.

3. The automatically generated Class.vb\class.cs is more named Myeventloginstaller.vb\myeventloginstaller.cs.

4. The content in Myeventloginstaller.vb or MyEventLogInstaller.cs is replaced with the following code:

Visual Basic. NET Sample
Imports System.Diagnostics
Imports System.Configuration.Install
Imports System.ComponentModel

<runinstaller (True) > _
Public Class myEventLogInstaller
Inherits Installer
Private myEventLogInstaller as EventLogInstaller

Public Sub New ()
' Create An instance of ' EventLogInstaller '.
myEventLogInstaller = New EventLogInstaller ()
' Set ' the ' Source ' of the event log, to be created.
Myeventloginstaller.source = "TEST"
' Set the ' Log ' that ' the source is created in.
MyEventLogInstaller.Log = "Application"
' Add myeventloginstaller to ' installercollection '.
Installers.add (myEventLogInstaller)
End Sub
End Class

Visual C #. NET Sample
Using System;
Using System.Diagnostics;
Using System.ComponentModel;
Using System.Configuration.Install;


Namespace EventLogSourceInstaller
{
[Runinstaller (True)]
public class Myeventloginstaller:installer
{
Private EventLogInstaller myEventLogInstaller;

Public myEventLogInstaller ()
{
Create Instance of EventLogInstaller
myEventLogInstaller = new EventLogInstaller ();

Set the Source of Event Log, to be created.
Myeventloginstaller.source = "TEST";

Set the Log that this source is created in
MyEventLogInstaller.Log = "Application";

Add myEventLogInstaller to the installers Collection.
Installers.add (myEventLogInstaller);
}
}
}

5. Build this project and get EventLogSourceInstaller.dll.

6. Open the visual Studio. NET command Prompt and go to the EventLogSourceInstaller.dll directory.

7. Run this command to create an "event source": InstallUtil EventLogSourceInstaller.dll

More detailed information

We reproduce the above error and resolve the problem by creating a Web application.

1. Use vb.net or C # to build a asp.net Web application.

2. The code in WebForm1.aspx is replaced by the following code:

Visual Basic. NET Sample

<%@ Page language= "vb" autoeventwireup= "true"%>
<%@ Import namespace= "System.Diagnostics"%>
! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
<HTML>
<script language= "VB" runat= "Server"
Sub WriteEvent_Click (SRC as Object, e as EventArgs)
Dim ev as New EventLog ("Application")
' Event ' s Source name
Ev. Source = "TEST"

EventLog.CreateEventSource (EV. Source, "Application")

Try
Ev. WriteEntry (TextBox1.Text)
Catch B As exception
Response.Write ("WriteEntry" & B.message & "<br>")
End Try
EV = Nothing
End Sub
</script>

<body>
<form id= "Form1" runat= "Server"
Event message:
<asp:textbox id= "TextBox1" runat= "Server" width= "233px" > </asp:textbox>
<asp:button id= "Button1" runat= "Server" Name= "Button1" text= "Write to Event log" > </asp:button>
</form>
</body>
</HTML>

Visual C #. NET Sample
<%@ Page language= "C #" autoeventwireup= "true"%>
<%@ Import namespace= "System.Diagnostics"%>
! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
<HTML>
<script language= "C #" runat= "Server"
void WriteEvent_Click (Object Src, EventArgs e)
{
EventLog ev = new EventLog ("Application");
Event ' s Source name
Ev. Source = "TEST";

EventLog.CreateEventSource (EV. Source, "Application");

Try
{
Ev. WriteEntry (TextBox1.Text);
}
catch (Exception B)
{
Response.Write ("WriteEntry" + b.message + "<br>");
}
EV = NULL;
}
</script>

<body>
<form id= "Form1" runat= "Server"
Event message:
<asp:textbox id= "TextBox1" runat= "Server" width= "233px" > </asp:textbox>
<asp:button id= "Button1" runat= "Server" Name= "Button1" text= "Write to Event log" > </asp:button>
</form>
</body>
</HTML>

3. Press F5 to start this project.

4. Enter some characters in the textbox and click Write to Event Log.

5. The error message that is mentioned in the "Symptoms" section above appears.

6. To resolve this issue, in WebForm1.aspx the following line of code is commented

EventLog.CreateEventSource (EV. Source, "Application");

7. Restart the project.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.