ASP.NET错误的事件日志源属性的自定义值

默认情况下,ASP.NET将所有未捕获的exception记录到系统事件日志中。 我知道应该有一个适当的日志设施,但这比没有好,它可以作为一个临时解决方案。

我希望能够有效地过滤日志中的事件。 我了解到,在以编程方式进行日志记录时,您可以通过以下方式为事件日志中的Source列设置自定义值:

EventLog eventLog = new EventLog("Application"); eventLog.Source = "My custom name"; eventLog.WriteEntry("Some error description ...", EventLogEntryType.Error); 

但是,ASP.NET将此值设置为“ASP.NET”,后跟其版本。 我简要地检查了web.config的文档,但没有找到明显的地方来改变它。 我想知道它是否可以改变。

您最好的选择是按预期使用source属性,但在安装程序中使用安装程序类在安装时设置注册表(在Admin下),例如:

使用系统;
使用System.Collections;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Configuration.Install;
使用System.Diagnostics;

命名空间InstallerClasses
 {
     [runInstaller的(真)]
     public partial class EventLog:Installer
     {
         private EventLogInstaller eventLogInstaller;

         /// 
         ///为MyApp创建事件日志
         /// 
         public EventLog()
         {
            的InitializeComponent();

             //创建EventLogInstaller的实例。
             eventLogInstaller = new EventLogInstaller();

             //设置事件日志的源名称。
             eventLogInstaller.Source =“MySource”;

             //设置源写入条目的事件日志。
             eventLogInstaller.Log =“应用程序”;

             //将myEventLogInstaller添加到Installer集合中。
             Installers.Add(eventLogInstaller);
         }
     }
 }

并确保它在安装程序中作为自定义操作运行。

似乎使用source属性并不是一个好主意。 最初,我认为这是一个自由forms的文本。 但我发现它必须通过RegisterEventSource(…)Win32 API函数注册,这似乎仅在应用程序以管理员权限运行时才有效。 .NET默默地为您创建一个新源,但如果您不是管理员,则会抛出exception。 总的来说,在ASP.NET中使用ad-hoc源名称可能需要一些预注册,这将在部署中引入另一个步骤。

您可能希望在global.asax中处理未捕获的exception,然后以编程方式记录Exception,如下所示:

 void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError().GetBaseException(); // logging code here }