Created
August 5, 2015 07:06
-
-
Save RafPe/5404350e56147a093049 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
$folder4Logs = "X:\AdvancedIISLogs" | |
$LogRotateFreq = "Hourly" # Change to your desired frequency | |
#Check if we have folder for our logs - if not create one | |
if(!(Test-Path $folder4Logs)) | |
{ | |
# Create such a directory | |
New-Item -ItemType Directory -Path X:\ -Name AdvancedIISLogs | |
} | |
# Assign appropiate permissions | |
$Acl = Get-Acl $folder4Logs | |
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS_IUSRS", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow") | |
$Acl.SetAccessRule($Ar) | |
Set-Acl $folder4Logs $Acl | |
# Reconfigure username property - this is to well known bug when this property is not logged in correctly | |
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/advancedLogging/server/fields/field[@id='UserName']" -name "sourceType" -value "BuiltIn" | |
#Fields we want to be logging | |
$RequIisAdvLogFields = "Date-UTC","Time-UTC","Client-IP","Host","Referer","User Agent","Bytes Received","Method","URI-Querystring","URI-Stem","UserName","Protocol Version","BeginRequest-UTC","EndRequest-UTC","Time Taken","Bytes Sent","Status","Substatus","Server-IP","Server Port","Win32Status","X-Forwarded-For","X-Forwarded-Proto","CPU-Utilization" | |
# logging fields available on server | |
$availableFields = Get-WebConfiguration "system.webServer/advancedLogging/server/fields" | |
# configure each site with logging - we dont do that on server level | |
foreach($AppName in (Get-Website).Name) | |
{ | |
#First add logging format - $appName would be our website name! | |
Add-WebConfiguration '/system.webServer/advancedLogging/Server/logDefinitions' -Location $AppName -value @{ rollLogFileOnConfigChanges="true";baseFileName="%COMPUTERNAME%-$AppName"; enabled="true"; logRollOption="Schedule"; schedule=$LogRotateFreq; publishLogEvent="false"} | |
# TODO : add filtering.... | |
# Delay commit of settings ( otherwise each time we would wait ) | |
Start-WebCommitDelay | |
foreach( $singleField in $RequIisAdvLogFields) | |
{ | |
# Check if collection contains this singlefield | |
if ( ($availableFields.Collection | select Id -ExpandProperty Id).Contains($singleField) ) | |
{ | |
$fieldDetails = ( $availableFields.Collection | ? Id -eq $singleField) | |
# Add this required field on site level | |
Add-WebConfiguration "/system.webServer/advancedLogging/Server/logDefinitions/logDefinition[@baseFileName=""%COMPUTERNAME%-$AppName""]/selectedFields" IIS:\ -Location $AppName -value @{defaultValue="";required="false";logHeaderName=$($fieldDetails.logHeaderName);id=$($fieldDetails.id);} | |
} | |
} | |
#Stop delay | |
Stop-WebCommitDelay -Commit $true | |
} | |
# Disable IIS logging on server level | |
Set-WebConfigurationProperty -Filter "system.webServer/advancedLogging/server/logDefinitions/logDefinition[@BaseFileName='%COMPUTERNAME%-Server']" -name enabled -value false | |
# Disable standard logging | |
Set-WebConfigurationProperty -Filter system.webServer/httpLogging -PSPath machine/webroot/apphost -Name dontlog -Value true | |
# Enable advanced logging | |
Set-WebConfigurationProperty -Filter system.webServer/advancedLogging/server -PSPath machine/webroot/apphost -Name enabled -Value true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment