Flush IIS HTTP and FTP Logs to Disk

Today I wanted to find a way to flush the IIS FTP logs on-demand.  The logs for IIS FTP flush to disk every 6 minutes, and the HTTP logs every 1 minute (or 64kb).  This can make troubleshooting difficult when you don’t receive immediate access to the latest log data.

After looking everywhere I could think of, from search engine searches to perusing through the IIS schema files, I figured I had better go to the source and ask Robert McMurray.

Sure enough, Robert had the answer and even wrote a blog post in response to my question with code examples for four scripting/programming languages (C#, VB.NET, JavaScript, VbScript).

There is not a netsh or appcmd solution though, so the scripting or programming options are the way to do it.  Actually, you can also flush the logs by restarting the Microsoft FTP Service (ftpsvc) but, as you would assume, it will impact currently active FTP sessions.

This blog post serves three purposes. 

  1. It’s a reference pointing to Robert’s examples
  2. I’ll include how to do the same for the HTTP logs
  3. I’ll provide a PowerShell example which I based on Robert’s examples

1. The reference is mentioned above already, but to give me something useful to write in this paragraph, I’ll include it again. Programmatically Flushing FTP Logs.

2. For HTTP there is a method to flush the logs using netsh.

netsh http flush logbuffer

This will immediately flush the HTTP logs for all sites.

3. The FTP logs can be done from PowerShell too.  Here’s a script which is the PowerShell equivalent of Robert’s examples.  Just update $siteName, or pass it as a parameter to the script.

Param($siteName = "Default Web Site") 
 
#Get MWA ServerManager
[System.Reflection.Assembly]::LoadFrom( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll" ) | Out-Null
$serverManager = new-object Microsoft.Web.Administration.ServerManager 
 
$config = $serverManager.GetApplicationHostConfiguration()
 
#Get Sites Collection
$sitesSection = $config.GetSection("system.applicationHost/sites")
$sitesCollection = $sitesSection.GetCollection() 
 
#Find Site
foreach ($item in $sitesCollection){ 
 
    if ($item.Attributes.Item("Name").Value -eq $siteName){
        $site = $item
    }
}
#Validation
if ($site -eq $null) { 
    Write-Host "Site '$siteName' not found"
    return
}
 
#Flush the Logs
$ftpServer = $site.ChildElements.Item("ftpServer")
 
if (!($ftpServer.ChildElements.Count)){
    Write-Host "Site '$siteName' does not have FTP bindings set"
    return
}
 
$ftpServer.Methods.Item("FlushLog").CreateInstance().Execute()

I hope one of these programming/scripting options come in handy for times when you want immediate access to the latest FTP log data.

5 Comments

  • Very helpful. Thanks for posting.

  • What to do if IIS logs are not flushed under WinServer 2008 R2 ?

    "netsh http flush logbuffer" reports OK, PowerShell scripts also shows no errors, but logs are flushed only on iisreset executing.

  • Hi Alex,

    That command is for the HTTP logs (in a w3svc# folder). The longer powershell command is for FTP logs (in a MSFTP# folder). I'm not aware of a reason why netsh http flush logbuffer wouldn't work for any of the files in w3svc# folders. My recommendation is to confirm that you're using the right command for ftp vs. http or vice versa.

  • It seems the netsh command only flushes logs in the default site. The logs of other sites do not flush.

  • Hi bseddon. That should work for all sites. I've used it often on non w3svc1 sites. If you wait a minute, do the logs flush for the non-default site? You can test that to confirm that you're looking at the correct log folder.

    Also, make sure that you're running as administrator.

Comments have been disabled for this content.