我在WebService/WebForm中调用EventlLog.WriteEntry方法,抛出异常说没有操作注册表的权限,但是网上大量的例子都使用这种方法记录日志,请问如何解决这个问题。谢谢!

解决方案 »

  1.   

    ASP.NET 应用程序试图在 EventLog 中写入新的 EventSource 时出现“Requested Registry Access Is Not Allowed”(不允许所请求的注册表访问)错误信息
    http://support.microsoft.com/kb/329291/zh-cn原因
    默认情况下,ASP.NET 工作进程的用户令牌是 ASPNET(或者,对于 Internet 信息服务 [IIS] 6.0 上运行的应用程序是 NetworkService)。由于您的帐户不具有创建事件源的正确用户权限,会出现“症状”部分中的问题。
     返回页首 解决方案
    警告:注册表编辑器使用不当可导致严重问题,从而可能需要重新安装操作系统。Microsoft 不能保证您可以解决因注册表编辑器使用不当而导致的问题。使用注册表编辑器需要您自担风险。 要解决此问题,在您运行 ASP.NET Web 应用程序之前,拥有管理权限的用户必须创建事件源。要创建事件源,请使用下列方法之一。
    第一种方法
    在注册表编辑器中,在应用程序事件日志下创建一个事件源。为此,请执行下列步骤: 1. 单击“开始”,然后单击“运行”。 
    2. 在“打开”文本框中,键入 regedit。 
    3. 找到以下注册表子项:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application 
    4. 右键单击“Application”子项,指向“新建”,然后单击“项”。 
    5. 键入 TEST 作为该项的名称。 
    6. 关闭注册表编辑器。 第二种方法
    System.Diagnostics 名称空间中的 EventLogInstaller 类允许您安装和配置一个事件日志,您的应用程序在运行时可以读取或写入该事件日志。您可以使用 EventLogInstaller 创建一个事件源。为此,请执行下列步骤: 1. 使用 Microsoft Visual Basic .NET 或 Microsoft Visual C# .NET 创建一个新的名为 EventLogSourceInstaller 的“类库”。默认情况下,将创建“Class1.vb”文件或“Class1.cs”文件。 
    2. 在解决方案资源管理器中,右键单击“EventLogSourceInstaller”,然后单击“添加引用”。 
    3. 在“添加引用”对话框中,双击“System.Configuration.Install.dll”,然后单击“确定”。 
    4. 将 Class1.vb\Class1.cs 重命名为 MyEventLogInstaller.vb\MyEventLogInstaller.cs。 
    5. 用以下示例代码替换 MyEventLogInstaller.vb 或 MyEventLogInstaller.cs 中现有的代码:Visual Basic .NET 示例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 ClassVisual C# .NET 示例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 source is created in
    myEventLogInstaller.Log = "Application"; // Add myEventLogInstaller to the Installers Collection.
    Installers.Add(myEventLogInstaller);
    }
    }
    }
     
    6. 在“生成”菜单中,单击“生成解决方案”以生成“EventLogSourceInstaller.dll”。 
    7. 打开“Visual Studio .NET 命令提示符”。 
    8. 在该命令提示符下,更改到“EventLogSourceInstaller.dll”所在的文件夹。 
    9. 运行以下命令以生成 EventSource:
    InstallUtil EventLogSourceInstaller.dll