How to use EntLib with SharePoint TimerJob
Monitoring the health of custom SharePoint TimerJob is very important for trouble-shooting. Capturing an exception or recording activities to an EventLog is commonly used approach for monitoring an application. EntLib is a good choice for handling exceptions and recording logs to an Event Log, for example, because it provides Exception Handling block, Logging block and other aspects right out-of-box. EntLib relies on reading configuration information from .Net config file, and so no coding is required for changing configuration.
Microsoft Enterprise Library(or EntLib) [1] saves lots of time for providing Exception Handling, Logging, Data Connection, etc. EntLib can be easily used with SharePoint. Since TimerJob runs on "OwsTimer.exe" process, you need to create "OwsTimer.exe.config", and add the configuration information for EntLib and deploy the OwsTimer.exe.config under the BIN folder of 12 Hive [2].
As a side note, you can use this *.exe.config technique to create a configuration file for StsAdm.exe as well. When would you use StsAdm.exe.config? One possible example is when you want to install or activate a feature which contains SPFeatureReceiver using "StsAdm.exe -o installfeature" or "StsAdm.exe -o activatefeature."
Creating OwsTimer.Exe.Config
Creating an OwsTimer.Exe.Config file is straightforward. You can simply copy contents from the SharePoint web.config and paste them inside the OwsTimer.Exe.Config file after creating a blank OwsTimer.Exe.Config file.
This technique is useful not only for the EntLib but for any AppSettings section which exists in the web.config.
SharePoint web.config
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<configuration>
<!-- TODO: EntLib section goes here -->
<sectionGroup name="SharePoint">
…
</sectionGroup>
<appSettings>
<add key="FeedCacheTime" value="300" />
<add key="FeedPageUrl" value="/_layouts/feed.aspx?" />
...
<!-- TODO: Add app settings here -->
</appSettings>
</configuration>
Here is an example of OwsTimer.exe.Config based on the above SharePoint web.config. As you notice, appSettings section contains a custom key, "PortalUrl."
OwsTimer.Exe.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- TODO: EntLib section goes here -->
<appSettings>
<!-- TODO: Add app settings here -->
</appSettings>
</configuration>
Once you deploy the OwsTimer.Exe.config to BIN folder under 12 hive, you will need to restart the Windows TimerJob Services so that the services can read OwsTimer.exe.config.
Deploying the OwsTimer.Exe.config
The best way to deploy OwsTimer.exe.config is to use a SharePoint Solution Package. The following code will create a solution package which deploys the OwsTimer.exe.config to a correct location. A SharePoint Solution Package is a cab file which is created based on Manifest.xml, ddf file and other files referenced in the ddf file.
Manifest.xml
Using RootFiles node, you can deploy a file to any location under the BIN folder of 12 hive [2]. As you see on the code below, the Manifest.xml file indicates that OWSTIMER.exe.config will be deployed under the BIN folder of 12 hive.
<?xml version="1.0" encoding="utf-8" ?>
<Solution SolutionId="D3E73441-BE6A-4513-89C7-F9B4FD74E929" xmlns="http://schemas.microsoft.com/sharepoint/">
<RootFiles>
<RootFile Location="BIN\OWSTIMER.exe.config" />
</RootFiles>
</Solution>
package.ddf
The ddf file shown below has a reference to an OWSTIMER.exe.config file which is located under 12\BIN folder.
;
.OPTION EXPLICIT
.Set CabinetNameTemplate=JyHuh_RootFiles.cab
.Set DiskDirectoryTemplate="."
.Set CompressionType=MSZIP
.Set UniqueFiles="ON"
.Set Cabinet=on
.Set DiskDirectory1=.
manifest.xml
.Set DestinationDir=BIN
12\BIN\OWSTIMER.exe.config
;*** <The End>
Running MAKECAB.exe /f package.ddf from a command prompt will create a SharePoint Solution Package.
The next step is to deploy the SharePoint Solution Package using "stsadm -o addsolution ..." and "stsadm -o deploysolution ..." command.
Once the SharePoint Solution Package is successfully deployed, the OwsTimer.Exe.Config file will be successfully copied under the BIN folder of 12 hive as shown on the picture below:
How about deploying SharePoint Web.config?
You cannot really deploy SharePoint Web.Config which is typically located under C:\Inetpub\wwwroot\wss\VirtualDirectories\[foldername]. Instead, you can use SPWebConfigModification class (under Microsoft.SharePoint.Administration) to programmatically modify the SharePoint web.config. Using a SPWebConfigModification class is a topic which will require its own article, and so I'll not go further on this.
Donwload: You can click here to download a complete sample project file for this article.
[2] - 12 Hive = C:\Program Files\Common Files\Microsoft Shared\web server extensions\12