Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET错误的事件日志源属性的自定义值_C#_Asp.net_Logging_Event Log - Fatal编程技术网

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

C# ASP.NET错误的事件日志源属性的自定义值,c#,asp.net,logging,event-log,C#,Asp.net,Logging,Event Log,默认情况下,ASP.NET会将所有未捕获的异常记录到系统事件日志中。我知道一个人应该有一个适当的伐木设施,但这总比没有好,它可以作为一个临时解决方案 我希望能够有效地过滤日志中的事件。我了解到,当以编程方式记录时,可以通过以下方式为事件日志中的源列设置自定义值: EventLog eventLog = new EventLog("Application"); eventLog.Source = "My custom name"; eventLog.WriteEntry("Some error d

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

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

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

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

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

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

使用源属性似乎不是一个好主意。最初,我认为这是一个自由形式的文本。但我刚刚发现,它必须通过RegisterEventSource(…)Win32 API函数注册,而且这似乎只有在应用程序以管理员权限运行时才起作用。NET会默默地为您创建一个新的源,但如果您不是管理员,它会抛出一个异常。因此,总体而言,在ASP.NET中使用特定源名称可能需要一些预注册,这将在部署中引入另一个步骤。

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

使用制度; 使用系统集合; 使用System.Collections.Generic; 使用系统组件模型; 使用System.Configuration.Install; 使用系统诊断; 命名空间安装类 { [运行安装程序(true)] 公共部分类事件日志:安装程序 { 私有EventLogInstaller EventLogInstaller; /// ///为MyApp创建事件日志 /// 公共事件日志() { 初始化组件(); //创建EventLogInstaller的实例。 eventLogInstaller=新的eventLogInstaller(); //设置事件日志的源名称。 eventLogInstaller.Source=“MySource”; //设置源将条目写入的事件日志。 eventLogInstaller.Log=“应用程序”; //将myEventLogInstaller添加到安装程序集合。 Add(eventLogInstaller); } } }
并确保它在安装程序中作为自定义操作运行。

谢谢。我知道一般来说这是一个更好的选择,但我一直在寻找一个简单的配置开关。谢谢。这证实了我的发现,即源属性不打算用作特殊字符串。 using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Diagnostics; namespace InstallerClasses { [RunInstaller(true)] public partial class EventLog : Installer { private EventLogInstaller eventLogInstaller; /// /// Creates the event log for MyApp /// public EventLog() { InitializeComponent(); // Create an instance of an EventLogInstaller. eventLogInstaller = new EventLogInstaller(); // Set the source name of the event log. eventLogInstaller.Source = "MySource"; // Set the event log that the source writes entries to. eventLogInstaller.Log = "Application"; // Add myEventLogInstaller to the Installer collection. Installers.Add(eventLogInstaller); } } }