How can I write to the event log from my ASMX web service running as a network service?

I have ASMX web services running as "network services". I want to be able to write to the event log every time the web service is called (and when errors occur).

I created a new event log (MyLog) using the following code, run as administrator on a Windows 2008 server machine:

                if (!EventLog.SourceExists(APPLICATIONNAME))
            {
                EventLog.CreateEventSource(APPLICATIONNAME, EVENTLOGNAME);
            }
            EventLog.WriteEntry(APPLICATIONNAME, "Service started", EventLogEntryType.Information);

      

where APPLICATIONAME is "MyApp" and EVENTLOGNAME is "MyLog" (names have been changed to protect the innocent).

First, it creates a new event log "MyLog" (verified with eventvwr), but its content matches the content of the application event log. I was expecting to get an empty unprotected event log.

Secondly, when I try to write to the newly created event log from the Application_Start method using the following code:

EventLog.WriteEntry(APPLICATIONNAME, "Service started", EventLogEntryType.Information);

      

I am getting a SecurityException of type System.Diagnostics.EventLogPermission .

I have tried changing the MyLog hive ACL in the registry (as suggested on MSDN ) but to no avail.

I would prefer not to change the identity of the web service or change the ACLs in the application event log that are used by other applications on the system.

0


a source to share


1 answer


Since my web service is running in a SharePoint context, I just wrapped the EventLog.WriteEntry call with SPSecurity.RunWithElevatedPrivileges .



0


a source







All Articles