If we modify the trace level in the configuration file of our services, we will be forced to restart them so that the change to take effect, both when modifying to Verbose and when modifying to Warning. However, in some productive environments it's not possible restart the services (processes).
For this reason, the best option to modify the trace level in runtime is WMI. We can activate the WMI provider of our WCF services including the following entry in the "system.serviceModel" section of the configuration file:
<diagnostics wmiProviderEnabled="true" />
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True)] [string]$serviceName, | |
[Parameter()] [string]$traceLevel, | |
[Parameter()] [bool]$logMessagesAtTransportLevel, | |
[Parameter()] [bool]$logMessagesAtServiceLevel, | |
[Parameter()] [bool]$logMalformedMessages | |
) | |
$namespace = "root\servicemodel" | |
$filter = "Name='$serviceName'" | |
cls | |
Write-Host | |
Write-Host "Get the instance of the Service class by Name: '$serviceName'" -ForegroundColor Green | |
$wmiService = Get-WmiObject Service -filter $filter -Namespace $namespace -ComputerName localhost | |
if ($wmiService) | |
{ | |
$processId = $wmiService.ProcessId | |
Write-Host "Get the instance of the AppDomainInfo class by ProcessId: '$processId'" -ForegroundColor Green | |
$filter = “ProcessId=$processId” | |
$wmiAppDomain = Get-WmiObject AppDomainInfo -filter $filter -Namespace $namespace -ComputerName localhost | |
if ($wmiAppDomain) | |
{ | |
Write-Host "Setting de new values..." -ForegroundColor Green | |
if ($PSBoundParameters.ContainsKey('logMessagesAtTransportLevel')) { $wmiAppDomain.LogMessagesAtTransportLevel = $logMessagesAtTransportLevel } | |
if ($PSBoundParameters.ContainsKey('logMessagesAtServiceLevel')) { $wmiAppDomain.LogMessagesAtServiceLevel = $logMessagesAtServiceLevel } | |
if ($PSBoundParameters.ContainsKey('logMalformedMessages')) { $wmiAppDomain.LogMalformedMessages = $logMalformedMessages } | |
if ($PSBoundParameters.ContainsKey('traceLevel')) { $wmiAppDomain.TraceLevel = $traceLevel } | |
$wmiAppDomain.Put() | |
Write-Host "End" -ForegroundColor Green | |
} | |
else | |
{ | |
Write-Error "Instance of class AppDomainInfo not found" | |
} | |
} | |
else | |
{ | |
Write-Error "Instance of class Service not found" | |
} | |
Write-Host |
No comments:
Post a Comment