【问题标题】:Enterprise Library Logging not logging to Event Log from ASP.NET企业库日志记录未从 ASP.NET 记录到事件日志
【发布时间】:2011-02-27 07:03:17
【问题描述】:

我花了一天时间尝试使 Ent Lib Logging 工作并将任何内容记录到数据库或事件日志中。我有一个具有相同 Ent Lib 配置的 Web 应用程序和控制台应用程序,但只有控制台应用程序能够登录到事件日志。我尝试了所有有权限的事情,但我不知道我到底在做什么——哪些服务应该有什么。它不起作用!

我读过这样的文章 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 是否在同一台机器上运行?
  • 是的,WebApp 和 ConsolApp 在同一台机器上运行
  • “我尝试了所有有权限的方法”——你尝试了什么?
  • 这就是为什么我从 EntLib 日志记录中惊恐地逃跑的原因。我使用 Elmah 进行未处理的异常捕获和一个非常简单的使用 Linq-2-Sql 的 sql server 审计小部件,它只允许您编写带有一些元数据的字符串来记录业务事件。

标签: .net asp.net logging enterprise-library


【解决方案1】:

我相信在 IIS7 下(我假设您正在使用)application pool will be running under NETWORK SERVICE

您可以尝试将 NETWORK SERVICE 完全控制权授予注册表项 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application
(不推荐!)

或者,您可以将每个人的完全控制权授予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 中创建事件源,您必须具有管理权限”。

【讨论】:

  • 我认为让网络服务完全访问注册表是个坏主意。
  • 是的。我同意你的看法——不推荐。
  • 您可以仅授予 NETWORK SERVICE 帐户对您需要的事件源的写入权限。 Tuzo 提到了上面的大部分内容,但这里有一个文档可能会有所帮助:我知道这是一个较老的问题,但为了使用事件日志,必须首先创建事件源(由具有管理权限的用户创建)。此外,如果您尝试在网站中使用它,则必须授予 NETWORK SERVICE 帐户写入事件日志的权限。该网站将提供帮助:msdn.microsoft.com/en-us/library/ms998320.aspx
【解决方案2】:

我使用 PowerShell 脚本来创建适当的源...

$source = "FoToIaW"
if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false) 
{
  [System.Diagnostics.EventLog]::CreateEventSource($source, "Application")
}

【讨论】:

    【解决方案3】:

    或使用此代码....

     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");
    
    }
    

    【讨论】:

      【解决方案4】:

      以管理员身份运行 Visual Studio

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-17
        • 1970-01-01
        • 1970-01-01
        • 2010-09-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多