Thursday, December 1, 2011

PowerShell script for creation of Custom Event Source and Event Log

Copy the PowerShell script below and save it as EventSourceCreation.ps1 for creation of Event Source and Event Log:

#Write-Host "Remove event source SharePointEventSource"
Remove-EventLog -Source SharePointEventSource
#Write-Host "SharePointEventSource removed"

#Write-Host "Create event source SharePointEventSource"
New-EventLog -LogName "SharePoint Event Source" -Source SharePointEventSource
#Write-Host "Event source SharePointEventSource created"

Once event source has been created, go to Run => eventvwr and you will see that the new
"SharePoint Event Source" has be created under the Application and Services Logs, see snapshot below:

To automatically run the above script as a batch utility, copy and paste the code below and save it as a .bat file extension:

cd /d %~dp0
powershell -noexit -file ".EventSourceCreation.ps1" "%CD%"

Can write custom logging in SharePoint using the SPDiagnosticsService class to log messages and exceptions against the above custom source created namely: SharePointEventSource

Run the script and enjoy :)