企业库日志记录在ASP.NET中无法记录到事件日志

12

我花了一天时间尝试让Ent Lib Logging工作,并将任何日志记录到数据库或事件日志中。我有一个 Web 应用程序和控制台应用程序,它们使用相同的 Ent Lib 配置,但只有控制台应用程序能够将日志记录到事件日志中。我尝试了所有的权限设置,但我不知道具体要做什么 —— 哪些服务需要什么权限。它就是无法工作!

我读了像这篇文章的文章: http://imar.spaanjaars.com/275/logging-errors-to-the-event-log-in-aspnet-applications 我想尝试为 ASPNET 账户分配这些权限。我正在使用 Windows 7,但找不到 ASPNET 用户账户。那么它在哪里呢?

这是通过 Ent Lib 工具自动生成的配置文件,它仅适用于 App.config,而不适用于 web.config。

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"
    revertImpersonation="false">
    <listeners>
      <add source="Logger" formatter="Text Formatter" log="Application"
        machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Formatted EventLog TraceListener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

你能发布配置文件的相关部分吗?WebApp 和 ConsoleApp 运行在同一台机器上吗? - RoelF
是的,WebApp和ConsolApp在同一台机器上运行。 - Costa
"我尝试了所有的权限" -- 你都尝试了什么? - Randy Levy
这就是为什么我害怕使用EntLib日志记录。我使用Elmah来捕获未处理的异常,并使用Linq-2-Sql编写了一个非常简单的SQL Server审计小部件,只需让您编写带有一些元数据的字符串以记录业务事件。 - Chris Marisic
4个回答

4

我认为在IIS7下(我假设您正在使用),应用程序池将在NETWORK SERVICE下运行

您可以尝试给NETWORK SERVICE赋予注册表键HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application的完全控制权限。
(不推荐!)

或者,您可以授予EVERYONE对HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application的完全控制权限,记录一条消息,然后恢复该更改。一旦设置好了键,您就不需要在写入注册表时获得权限。

或者,您可以预先手动配置所需的注册表键以避免权限问题:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application\Logger]
"EventMessageFile"="c:\\WINNT\\Microsoft.NET\\Framework\\v2.0.50727\\EventLogMessages.dll"


这是对答案的更新,根据MSDN:在Windows Vista及更高版本或Windows Server 2003中创建事件源,您必须拥有管理员权限。


2
我认为将网络服务赋予完全访问注册表的权限是一个不好的想法。 - Costa
是的,我同意你的观点 -- 不推荐。 - Randy Levy
1
您可以为NETWORK SERVICE帐户授予对所需事件源的写入权限。Tuzo在上面提到了大部分内容,但这里有一份文件可能会有所帮助:我知道这是一个较旧的问题,但为了使用事件日志,必须首先创建事件源(由具有管理特权的用户)。此外,如果您正在尝试在网站内使用它,则必须授予NETWORK SERVICE帐户访问写入事件日志的权限。此网站将有所帮助:http://msdn.microsoft.com/en-us/library/ms998320.aspx - Mark

4
我使用PowerShell脚本创建适当的源...
$source = "FoToIaW"
if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false) 
{
  [System.Diagnostics.EventLog]::CreateEventSource($source, "Application")
}

2

以管理员身份运行Visual Studio

  1. 右键点击Visual Studio图标
  2. 选择“以管理员身份运行”

1

或者使用这段代码...

 public static void RunSnippet(string logName, string eventSource)
{
    // this will throw an exception if you don't have admin rights
    if (!EventLog.SourceExists(eventSource))
    {
        System.Diagnostics.EventLog.CreateEventSource(eventSource, logName);
        Console.WriteLine("Event Log and Source: {0},{1} were created successfully.",logName, eventSource);
    }
    else
    {
        Console.WriteLine("Event log/source: {0}/{1} already exists",logName, eventSource);
    }
    Console.WriteLine("Done");

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接