如何禁用.NET事件日志警告?

时间:2012-01-11 18:24:56

标签: asp.net error-handling event-log

根据我们的策略,我们不再允许写入事件日志,因此我将所有事件日志代码从写入事件日志中删除,这有效,但是我不断从错误中获取随机ASP.NET 4.0警告,甚至虽然我的Application_Error中有代码来处理所有错误。

完全禁用事件日志记录的任何方法,可能是web.config更改或IIS设置以禁用它?

也注意到很多错误..不仅仅是我的HTTPHandler

EventLog记录示例:

Event code: 3005 
Event message: An unhandled exception has occurred. 
Event time: 9/8/2011 10:26:04 AM 
Event time (UTC): 9/8/2011 2:26:04 PM 
Event ID: 6b56761296d24e1aa01038ce125be044 
Event sequence: 97 
Event occurrence: 1 
Event detail code: 0 

Application information: 
    Application domain: /LM/W3SVC/1/ROOT/-2-129599642336131368 
    Trust level: Full 
    Application Virtual Path: /
    Application Path: 
    Machine name: 

Process information: 
    Process ID: 6396 
    Process name: w3wp.exe 
    Account name: IIS APPPOOL\MyAppPool

Exception information: 
    Exception type: System.Exception
    Exception message: STACKTRACE


2 个答案:

答案 0 :(得分:4)

ASP.NET的默认行为是将未处理的异常写入事件日志。阻止它将它们写入事件日志的方法是自己处理它们。您可以通过Application_OnError处理程序中的Global.asax文件执行此操作。

您也可以调用Server.ClearError()来防止它冒泡到任何其他处理程序。

答案 1 :(得分:1)

确实,默认行为是将所有未处理的异常记录到应用程序事件日志中,如in this answer所述。此行为由<system.web><healthMonitoring>中的web.config元素控制,默认设置显示为here。我们的想法是,对于每个未处理的异常,都会引发System.Web.Management.WebBaseErrorEvent的实例,然后ASP.NET默认设置会将其记录到应用程序事件日志中。

您可以处理所有错误,也可以更改ASP.NET设置。可以使用<system.web><healthMonitoring><rules>更改导致记录这些事件的规则。既然你说你被禁止写入事件日志,我猜你最好的办法就是将默认规则删除为explained here

<healthMonitoring>
  <rules>
    <clear />
  </rules>
</healthMonitoring>

删除了两个默认规则,每个规则都会导致写入事件日志。