Kirk Evans is a Microsoft Architect for the Azure Center of Excellence.
Introduction to SharePoint and Azure IaaS
Building SharePoint Apps with Windows Azure Platform as a Service
SharePoint Solutions and Architectures on Windows Azure Infrastructure Services
Understanding Authentication and Permissions with Apps for SharePoint and Office
This post shows how to set the log fields for IIS web sites programmatically. Code is attached at the end of the post.
I have a customer that has many web applications, and we need to set the IIS log fields to do some investigation. James Blackwell posted a great sample of how to do this using VBScript (http://blogs.msdn.com/b/jb/archive/2008/02/03/script-for-enabling-full-iis-logging.aspx). I had a need to do this via C#, and most of the work was simply converting James’ sample to use the System.DirectoryServices.DirectoryEntry class to manipulate the sites. The reason I chose to use this instead of IIS7’s new Microsoft.Web.Administration namespace is because I am working with an IIS6 web server, however the code below works with both IIS6 and IIS7.
A fantastic resource for measuring your site’s performance is the whitepaper, “Analyzing Microsoft SharePoint Products and Technologies Usage”. It covers how to use tools like LogParser to get a better understanding of how your servers are performing. Of course, to process the data means that your server needs to log the data in the first place.
We will create a .NET 2.0 console application and reference the System.DirectoryServices assembly. You can query more than just Active Directory using the DirectoryEntry class, as we will see in this post. We need to set the LogExtFiles property for a web site, which expects a DWORD, which is a 32-bit integer. To set the flags, we first need to determine what the values are by looking them up in the LogExtFileFlags documentation. That is where I got the values for the LogExt* constants below.
The rest is simple, just choose which fields you want to set and add them together to form the value. For instance, if you only want to set LogExtFileDate and LogExtFileTime, the value is 1 + 2 = 3. In my example below, I set all values except LogExtFileCookie because this field can cause your log files to grow considerably.
const int LogExtFileDate = 1;
const int LogExtFileTime = 2;
const int LogExtFileClientIp = 4;
const int LogExtFileUserName = 8;
const int LogExtFileSiteName = 16;
const int LogExtFileComputerName = 32;
const int LogExtFileServerIp = 64;
const int LogExtFileMethod = 128;
const int LogExtFileUriStem = 256;
const int LogExtFileUriQuery = 512;
const int LogExtFileHttpStatus = 1024;
const int LogExtFileWin32Status = 2048;
const int LogExtFileBytesSent = 4096;
const int LogExtFileBytesRecv = 8192;
const int LogExtFileTimeTaken = 16384;
const int LogExtFileServerPort = 32768;
const int LogExtFileUserAgent = 65536;
const int LogExtFileCookie = 131072;
const int LogExtFileReferer = 262144;
const int LogExtFileProtocolVersion = 524288;
const int LogExtFileHost = 1048576;
const int LogExtFileHttpSubStatus = 2097152;
static void Main(string args)
string serverName = args;
static void DisplayUsage()
Console.WriteLine("Enables all logging fields on all web sites.");
static void SetLogFields(string serverName)
DirectoryEntry w3svc = new DirectoryEntry(
foreach (DirectoryEntry site in w3svc.Children)
Console.WriteLine("Site ID: " + site.Name);
if (null != site.Properties["LogExtFileFlags"].Value)
Console.WriteLine("Setting log fields for Site ID: " + site.Name);
Script for enabling full IIS logging
How to Use Microsoft.Web.Administration
Analyzing Microsoft SharePoint Products and Technologies Usage
Here is a way to do with AppCMD
$AppCMDPath = $env:windir + "\system32\inetsrv"
.\appcmd.exe set config /section:system.applicationHost/sites /siteDefaults.logFile.logExtFileFlags:"Date, Time, ClientIP, UserName, SiteName, ComputerName, ServerIP, Method, UriStem, UriQuery, HttpStatus, Win32Status, BytesSent, BytesRecv, TimeTaken, ServerPort, UserAgent, Referer, ProtocolVersion, Host, HttpSubStatus" /commit:apphost
That's why I've always been a fan of yours, Steve! That's awesome, thanks for posting. Any way to do this with Server 2003 R2 (doesn't have AppCMD that I know of).