See how Microsoft Consulting Services can help you
Contact Microsoft Services
Contact The SharePoint Guys
Apply for a job at Microsoft Consulting Services
MCS Solution Dev
The Deployment Guys
MSDN UK
MSDN UK Newsletter
MSDN UK Team Blog
TechNet UK
TechNet UK Newsletter
TechNet UK Blog
As you will surely be aware, it is best practice to install production SharePoint farms using something called the “least privilege account principle”. This effectively means that each service account in the farm is dedicated to a specific job and has only the minimum rights required in order to do that job. This is best practice as one of many common steps to ensuring your farm is secure, you can read all about it here Plan for administrative and service accounts (Office SharePoint Server).
If you deploy a custom solution on SharePoint that contains some for of error logging, it is likely that you will want to write events to the Windows event log. These events are typically written to the application log under a custom source that matches the name of your solution/application. Typically the custom source is created as part of the code itself and the code to do this is something like the following:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
if (EventLog.SourceExists("MyCustomSource") == false)
EventLog.CreateEventSource("MyCustomSource ", "Application");
}
EventLog.WriteEntry("MyCustomSource", "My message", EventLogEntryType.Error);
});
The problem with the above code is that in a least privilege scenario, this will cause an exception because the code will execute under the context of the Application Pool which will not have any local rights on the machine and is not able to create the event source. You may see an error both on-screen and in the logs along the lines of “The source was not found, but some or all event logs could not be searched.”.
To work around this issue, the event log source must be created under the context of an account which does have increased rights on the local machine. There are several ways of doing this, which include:
In my projects, i tend to go for the simplest solution and in this case it is very simple. It turns out that Event Log Sources are simply keys in the registry.
All event log sources are stored in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog. In this key there are subkeys for each of the logs (Application, System etc) and a further sub-key for each of the sources in that log. All you need to do is add a key which matches the name of the source you wish to create.
All event log sources need to refer to an ‘EventMessageFile’. These files define the message text that is available for events in that particular source. Typically you’ll want to use the standard .net file which allows any text. To do this you need to add an ‘Expandable String Value’ beneath the key you created for your custom source. The value of this should typically be set to “C:\Windows\Microsoft.NET\Framework\v2.0.50727\EventLogMessages.dll” or “C:\Windows\Microsoft.NET\Framework64\v2.0.50727\EventLogMessages.dll” on 64-bit machines.
Your registry should look something like this:
You can do this manually (on every server in the farm) through something like regedit.,exe or you can create a .reg file which you can simply double click-on to add the registry hive. To create a .reg file, follow these steps:
1. Manually create the registry hive on one mahcine
2. Right-click on the hive (i.e. the key you created beneath HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\Application and choose ‘export’
3. Save the file as a .reg file
4. Simply double-click the .reg file on other servers to import the key
5. Reboot the machine (you’ll nee to reboot before you can use your custom source)
Once the source is created, the following code will work perfectly:
This article was published by
Martin Kearn Senior Consultant Microsoft Consulting Services UK Martin.Kearn@Microsoft.com
Click here for my bio page