我正在尝试使用 log4net 在 Windows Server 2008 SP1 上的 IIS7 下写入客户事件日志。但是,帐户似乎无权写入事件日志。有没有人有什么建议?
最佳答案
问题可能是您的事件源。您必须先创建一个事件源,然后才能写入事件日志(如果不这样做,事件日志对象会尝试在您第一次写入日志时“自动”创建一个)。
您必须具有高级权限才能创建事件日志源。在我的一些 Web 应用程序中,我已将用于创建事件源的代码放入我的设置中(设置以管理员身份运行,因此我始终保证能够创建源)。
您只需要创建一次源。之后,您的 ASP.Net 应用程序应该有足够的权限来写入指定您创建的源(或多个源)的条目。
您可以在设置中使用 EventLogInstaller 来创建源,或者您可以编写一个小实用程序来以管理员身份调用 EventLog.CreateEventSource()。
我将向您展示两种方式:
// You would do this one from within an Installer class in a setup:
private void InstallEventLog()
{
EventLogInstaller logInstaller;
//Create an instance of an EventLogInstaller.
logInstaller = new EventLogInstaller();
//Set the source name of the event log.
logInstaller.Source = "TheEventSourceName";
Installers.Add(logInstaller);
}
方法 2:只需以管理员身份调用 CreateEventSource 一次(例如,您可以将以下代码放入控制台应用程序中,然后以管理员身份运行控制台应用程序
EventLog.CreateEventSource("TheSourceName", "Application");
奖金:
如果您的服务器上安装了 Powershell,则可以从 Powershell 命令提示符执行此操作:
(确保您以管理员身份运行 Powershell)
[system.Diagnostics.EventLog]::CreateEventSource("SourceName", "Application")
跳有帮助
关于asp.net - 在 Windows Server 2008 IIS7 上写入 ASP.NET 中的事件日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/712203/