Dot-Net

企業庫日誌記錄未從 ASP.NET 記錄到事件日誌

  • January 22, 2013

我花了一天時間嘗試讓 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}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"
       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 & Warnings">
       <listeners>
         <add name="Formatted EventLog TraceListener" />
       </listeners>
     </errors>
   </specialSources>
 </loggingConfiguration>

我相信在 IIS7(我假設您正在使用)下,應用程序池將在 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 中創建事件源,您必須具有管理權限”。

引用自:https://stackoverflow.com/questions/2965841