How to write event logs in ASP.NET

zhaozj2021-02-17  47

How to write event logs with ASP.NET

Source: http://support.microsoft.com/default.aspx? ScID = Kb; EN-US; 329291

Keywords: ASP.NET event log error message

Important matter

This article contains information about editing registry. Before editing the registry, you must first understand how to restore the registry when you have problems. For information on how to restore registry, check the "Restore Registry" help topics in Regedit.exe, or RegentT32.exe Help topics.

phenomenon

When you write a new "event source" in the event log, you may get the following error message: system.security.securityException: The requested registry access is not allowed

the reason

The default 怅怅 running the ASP.NET process is an ASPNET (below IIS6.0 is a NetworkService), and this user does not have permission to create an "event source."

Solution

Note: (Edit the registry will lead to the microsoft, which can cause the system crash, and not say much). If you need to resolve this issue, you must create an "event source" by users with administrator programs before you run this ASP.NET program. There are several ways to create an "event source".

First method

Use the following steps to create an "event source" below "Application Log" in the Registry Editing

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

2. Enter "regedit" in the "Open" box.

3. Find the following subkey:

HKEY_LOCAL_MACHINE / SYSTEM / CURRENTCONTROLSET / SERVICES / EVENTLOG / APPLICATION

4. Right click on "Application" to click "New" and then "Item"

5. Rename this new item to "test"

6. Close Registry Editor

Second method

There is an EventLogInstaller class in the System.Diagnostics namespace. It can create and configure event logs for your application to read and write. With the following steps, we can use the EventLoginStaller class to create an "event source"

1. Create a "class library" called EventLogSourceInstaller with VB.NET or C #.

2. Add a reference to System.Configuration.install.dll to the project.

3. The automatically generated class.vb / class.cs is more named myeventloginstaller.vb / myeventloginstaller.cs.

4. Replace the content in MyEventLoginstaller.vb or MyEventLoginstaller.cs:

Visual Basic .NET SAMPLE

Imports system.diagnostics

Imports system.configuration.install

Imports system.componentmodel

_

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 time log, to be created.myeventloginstaller.source = "test"

'Set the' log 'That The Source is created.

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 evenet log, to be created.

MyEventLoginstaller.Source = "Test";

// set the log what source is created in the Log That Source IS CREATED IN

MyEventLoginstaller.log = "Application";

// add myeventloginstaller to the installers collection.

Installers.Add (MyEventLoginstaller);

}

}

}

5. Generate this item to get 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 an "event source": installutil EventLogsourceInstaller.dll

More detailed information

We reproduce the above errors through a Web Application and resolve this issue.

1. Establish an ASP.NET Web Application using VB.NET or C #.

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

Visual Basic .NET SAMPLE

<% @ Page language = "vb" autoeventwireup = "true"%>

<% @ Import namespace = "system.diagnostics"%>