问题描述
我正在尝试使用 log4net 在 Windows Server 2008 SP1 上的 IIS7 下写入客户事件日志.但是,帐户似乎无权写入事件日志.有人有什么建议吗?
问题可能是你的事件源.您必须先创建一个事件源,然后才能写入事件日志(如果您不这样做,事件日志对象会在您第一次写入日志时尝试自动"为您创建一个).
您必须拥有更高的权限才能创建事件日志源.在我的一些网络应用程序中,我已将用于创建事件源的代码放入我的设置中(设置以管理员身份运行,因此我始终保证能够创建源).
您只需创建一次源.之后,您的 ASP.Net 应用程序应该有足够的权限来编写指定您创建的源(或源)的条目.
您可以在设置中使用 EventLogInstaller 来创建源,或者您可以编写一个小实用程序以管理员身份调用 EventLog.CreateEventSource().
我会告诉你两种方式:
<上一页><代码>//您可以在设置中的 Installer 类中执行此操作:私人无效安装事件日志(){EventLogInstaller logInstaller;//创建一个EventLogInstaller的实例.logInstaller = new EventLogInstaller();//设置事件日志的源名称.logInstaller.Source = "TheEventSourceName";Installers.Add(logInstaller);}代码>方法2:只需以管理员身份调用一次CreateEventSource(例如,您可以将以下代码放入控制台应用程序中,并以管理员身份运行控制台应用程序
<上一页><代码>EventLog.CreateEventSource("TheSourceName", "Application");代码>奖金:如果您在服务器上安装了 Powershell,则可以从 Powershell 命令提示符执行此操作:(确保您以管理员身份运行 Powershell)
<上一页><代码>[system.Diagnostics.EventLog]::CreateEventSource("SourceName", "Application")代码>有帮助的跳
I'm trying to use log4net to write to a customer event log under IIS7 on Windows Server 2008 SP1. However, account doesn't seem to have access to write to the event log. Does anyone have any suggestions?
The problem is probably your event source. You have to create an event source before you can write to the event log (if you don't, the Event log object tries to create one for you "automagically" the first time you write to the log).
You have to have hightened permissions to create an event log source. In some of my web apps, I have put the code to create the event source into my setup (setup runs as admin, so I'm always guaranteed to be able to create the source).
You just have to create the source once. After that, your ASP.Net app should have sufficient permissions to write entries specifying the source (or sources) that you created.
You can use an EventLogInstaller in your setup to create the source, or you could just write a little utility to call EventLog.CreateEventSource() as an admin.
I'll show you both ways:
// 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);
}
Method 2: just call CreateEventSource once as an admin (you could put the following code into a console app, for example, and run the console app as admin
EventLog.CreateEventSource("TheSourceName", "Application");
Bonus: If you have Powershell installed on your server, you can do it from the Powershell command prompt: (Make sure you are running Powershell as an admin)
[system.Diagnostics.EventLog]::CreateEventSource("SourceName", "Application")
Hop that helps
这篇关于在 Windows Server 2008 IIS7 上的 ASP.NET 中写入事件日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!