The patterns & practices SharePoint Guidance team has posted this article on how to manag custom areas and categories i SharePoint 2010 http://msdn.microsoft.com/en-us/library/ff798462.aspx. But all the examples is written in C# (and that’s a good thing
) but I don’t want to have to use a feature receiver add my custom areas and categories, so I have create this demo PowerShell script, strongly inspire by the Microsoft article.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Practices.ServiceLocation") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Practices.SharePoint.Common") | Out-Null
#EventSeverity Description
# None Indicates no event entries are written.
# ErrorCritical Indicates a problem state that needs the immediate attention of an administrator.
# Error Indicates a problem state requiring attention by a site administrator.
# Warning Indicates conditions that are not immediately significant but that may eventually cause failure.
# Information Contains noncritical information provided for the administrator.
# Verbose
#TraceSeverity Description
# None Writes no trace information to the trace log file.
# Unexpected Represents an unexpected code path and actions that should be monitored.
# Monitorable Represents an unusual code path and actions that should be monitored.
# High Writes high-level detail to the trace log file.
# Medium Writes medium-level detail to the trace log file.
# Verbose Writes low-level detail to the trace log file.
#Create a List<DiagnosticsArea> to collect the Diagnostics Area
$areaCollection = New-Object 'System.Collections.Generic.List[Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsArea]'
#Add a DiagnosticsArea with the name Custom Area
$customArea = New-Object 'Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsArea' -ArgumentList "Custom Area"
#Add a DiagnosticsCategory, to the Custom Areas DiagnosticsCategories collection, with the name Business logic, EventSeverity set to Warning and TraceSeverity to Medium
$customArea.DiagnosticsCategories.Add((New-Object 'Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsCategory'("Business logic", ([Enum]::Parse([Microsoft.SharePoint.Administration.EventSeverity], 'Warning')), ([Enum]::Parse([Microsoft.SharePoint.Administration.TraceSeverity], 'Medium')))))
#Add a DiagnosticsCategory, to the Custom Areas DiagnosticsCategories collection, with the name Data layer, EventSeverity set to Error and TraceSeverity to Medium
$customArea.DiagnosticsCategories.Add((New-Object 'Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsCategory'("Data layer", ([Enum]::Parse([Microsoft.SharePoint.Administration.EventSeverity], 'Error')), ([Enum]::Parse([Microsoft.SharePoint.Administration.TraceSeverity], 'Medium')))))
#Add the Custom Area to the List<DiagnosticsArea> collection
$areaCollection.Add($customArea)
#Create a new DiagnosticsArea witht the name Test Area and adds it to the List<DiagnosticsArea> collection
$areaCollection.Add((New-Object 'Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsArea' -ArgumentList "Test Area"))
#Get a IConfigManager from the SharePointServiceLocator
$configManager =[Microsoft.Practices.SharePoint.Common.ServiceLocation.SharePointServiceLocator]::GetCurrent().GetInstance([Microsoft.Practices.SharePoint.Common.Configuration.IConfigManager])
#Create a new DiagnosticsAreaCollection with the IConfigManager as parameter
$configuredAreas = New-Object 'Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsAreaCollection' $configManager
#loop through the newly collection of diagnosticsareas
$areaCollection | % {
#get the instance from the current configuration
$existingArea = $configuredAreas[$_.Name
]
#if the area the is null, we add add the area from the List<DiagnosticsArea> collection
#else we loop through the collection we got from current configuration
if($existingArea -eq $null)
{
}
else
{
#loop through the collection we got from current configuration
$customArea.DiagnosticsCategories | % {
#get the category instance from the $existingArea (DiagnosticsArea)
$existingCategory = $existingArea.DiagnosticsCategories
[$_.Name
] #if the category is null, we add to the current configuration
if($existingCategory -eq $null)
{
$existingArea.DiagnosticsCategories.Add
($_);
}
}
}
}
#save the current configuration
$configuredAreas.SaveConfiguration()
The demo code.