I have a ASMX web services running as 'network service'. I want to be able to write to a custom event log every time a web service is called (and when errors happen).
我有一个ASMX Web服务作为“网络服务”运行。我希望每次调用Web服务时(以及发生错误时)都能写入自定义事件日志。
I have created a new event log (MyLog) using the following code running as admin on a Windows server 2008 machine:
我使用以下代码在Windows Server 2008计算机上以管理员身份创建了一个新的事件日志(MyLog):
if (!EventLog.SourceExists(APPLICATIONNAME))
{
EventLog.CreateEventSource(APPLICATIONNAME, EVENTLOGNAME);
}
EventLog.WriteEntry(APPLICATIONNAME, "Service started", EventLogEntryType.Information);
where APPLICATIONAME is "MyApp" and EVENTLOGNAME is "MyLog" (names changed to protect the innocents).
其中APPLICATIONAME为“MyApp”,EVENTLOGNAME为“MyLog”(更改名称以保护无辜者)。
Firstly, it creates a new "MyLog" event log (verified with eventvwr), but its contents are the same as the Application event log. I was expecting to get an empty, non-connected event log.
首先,它创建一个新的“MyLog”事件日志(使用eventvwr验证),但其内容与Application事件日志相同。我期待得到一个空的,未连接的事件日志。
Secondly, when I try to write to the newly created event log from within the Application_Start method using the following code:
其次,当我尝试使用以下代码从Application_Start方法中写入新创建的事件日志时:
EventLog.WriteEntry(APPLICATIONNAME, "Service started", EventLogEntryType.Information);
I get a SecurityException of type System.Diagnostics.EventLogPermission.
我得到一个System.Diagnostics.EventLogPermission类型的SecurityException。
I have tried to change the ACLs of the "MyLog" hive in the registry (as suggested on MSDN) but to no avail.
我试图在注册表中更改“MyLog”配置单元的ACL(如MSDN上所示),但无济于事。
I'd rather not change the identity of the web service nor change the ACLs on the Application event log which is used by other applications on the system.
我宁愿不更改Web服务的标识,也不要更改系统上其他应用程序使用的应用程序事件日志中的ACL。
1 个解决方案
#1
Since my web service is running in the context of SharePoint, I simply wrapped the call to EventLog.WriteEntry with SPSecurity.RunWithElevatedPrivileges.
由于我的Web服务是在SharePoint的上下文中运行的,因此我只是使用SPSecurity.RunWithElevatedPrivileges将调用包装到EventLog.WriteEntry中。
#1
Since my web service is running in the context of SharePoint, I simply wrapped the call to EventLog.WriteEntry with SPSecurity.RunWithElevatedPrivileges.
由于我的Web服务是在SharePoint的上下文中运行的,因此我只是使用SPSecurity.RunWithElevatedPrivileges将调用包装到EventLog.WriteEntry中。