How to Write event logs in ASP. NET

Source: Internet
Author: User
Tags net command
Edit the registry. Before editing the registry, you must first understand how to restore the Registry in case of a problem. 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.
Symptom

When you use Asp.net to write a new "event source" to the event log, you may get the following error message: system. Security. securityexception: the requested registry access is not allowed.

Cause

The default account for running the Asp.net process is ASPnet (NetworkService under iis6.0), and this user does not have the permission to create an "event source ".

Solution

Note: (if you edit the registry, it will cause a system crash or something like Microsoft to scare you ). If you need to solve this problem, run this Asp.netProgramPreviously, an "event source" must be created by a user with administrator permissions ". The following describes how to create an "event source ".

First Method

Use the following steps to create an "event source" under "application logs" in the registry editing"

1. Click start and then run ".

2. Enter "Regedit" in the "open" box ".

3. Find the subkeys of the following columns:

HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ Eventlog \ Application

4. Right-click "application", click "new", and click "item"

5. Rename the new item as "test"

6. Disable Registry Editor

Method 2

There is an eventloginstaller class in the system. Diagnostics namespace. It can create and configure Event Logs to be read and written by your application during operation. Follow these steps to create an "event source" using the eventloginstaller class"

1. Use VB. Net or C # to create a "class library" named eventlogsourceinstaller ".

2. Add a reference to system. configuration. Install. dll in the project.

3. Name the automatically generated class. VB \ class. CS as myeventloginstaller. VB \ myeventloginstaller. CS.

4. Replace the following content in myeventloginstaller. VB or myeventloginstaller. CS: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 .. \ computer \ downloadfiles \ article \ 27 \, to be created.
Myeventloginstaller. Source = "test"
'Set the 'log' that the source is created in.
Myeventloginstaller. log = "application"
'Add myeventloginstaller to 'installercollect '.
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 .. \ computer \ downloadfiles \ article \ 27 \, to be created.
Myeventloginstaller. Source = "test ";

// Set the log that source is created in
Myeventloginstaller. log = "application ";

// Add myeventloginstaller to the installers collection.
Installers. Add (myeventloginstaller );
}
}
}

5. generate this project and obtain eventlogsourceinstaller. dll.

6. Open the Visual Studio. NET command prompt and go to the directory where eventlogsourceinstaller. dll is located.

7. Run this command to create "event source": installutil eventlogsourceinstaller. dll

More detailed information

We create a web application to reproduce the preceding errors and solve them.

1. Use VB. Net or C # To create an Asp.net web application.

2. Replace the code in webform1.aspx with 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 .. \ computer \ downloadfiles \ article \ 27 \, e as eventargs)
Dim eV as new EventLog ("application ")
'Events' source name
Ev. Source = "test"

EventLog. createeventsource (EV. Source .. \ computer \ downloadfiles \ article \ 27 \, "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" onclick = "writeevent_click" 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 .. \ computer \ downloadfiles \ article \ 27 \, eventargs E)
{
EventLog EV = new EventLog ("application ");
// Event's source name
Ev. Source = "test ";

EventLog. createeventsource (EV. Source .. \ computer \ downloadfiles \ article \ 27 \, "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" onclick = "writeevent_click" runat = "server" name = "button1" text = "write to Event Log"> </ASP: button>
</Form>
</Body>
</Html>

3. Press F5 to start the project.

4. Enter some characters in textbox and click write to event log.

5. The error message mentioned in the "Symptom" section above will appear.

6. To solve this problem, comment out the following line of code in webform1.aspx.

EventLog. createeventsource (EV. Source .. \ computer \ downloadfiles \ article \ 27 \, "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.