life runs on code

bending technology to do your bidding

Getting the IIS log folder path using C#

You can use the Microsoft.Web.Administration namespace to obtain information about local and remote IIS configuration settings and perform administrative functions via code.  In this example, I will be using the assembly to determine the log file folder path location on a remote server for all of the sites.  It turns out that the bulk of the work is in installing and configuring the components necessary to make use of the assembly when performing operations on a remote server.
 
First you must add a reference to the assembly which houses the Microsoft.Web.Administration Namespace.  There are one of two ways you can do this: If you have the IIS Management Console installed, you can directly reference Microsoft.Web.Administration.dll in: %SYSTEMROOT%\SYSTEM32\InetSrv. Note that you do not actually have to have the IIS web server component installed; all you need is the Management Console. Alternatively, you can install the Microsoft.Web.Administration nuget package.  The nuget package (as of this post) is for the 7.0 version of the assembly.  While it will work with IIS8+, functionality specific to IIS 8+ will not be available.
 
The Microsoft.Web.Administration namespace includes several objects; however, the one we will use to get the log file folder path location is the ServerManager.  The ServerManager object is the top most configuration object.  It exposes all of the other IIS objects such as Applications, Virtual Directories, etc. If your code will be running locally on the machine which hosts IIS, then your work is done; simply create an new instance of the ServerManager Object and traverse the Sites collection and look at the Directory property of the LogFile object.
var serverManager = new ServerManager();
foreach(var site in serverManager.Sites)
{
System.Diagnostics.Debug.Print(System.IO.Path.Combine(site.LogFile.Directory, "WSVC"+site.Id));
}
Making It Work Remotely:
To use the ServerManager object with a remote server, you must make sure that your remote server has the IIS Remote Management Service installed, configured and enabled. This page will walk you through the necessary steps to get IIS Remote Management up and running; it references IIS7, but the procedure is similar for Windows Server 2012 and IIS8.  When the Remote Management Service is installed, it creates a default Windows Firewall rule for port 8172 (unless you specified a different port during configuration).  This port is important to know if you plan on connecting to the remote IIS Instance using a remote management console; however, the Microsoft.Web.Administration.dll actually uses COM to talk to the Remote Management Service.  Since we will be connecting from a remote machine to our host IIS server, we are now talking DCOM and in order for you to make things work, you will need to add appropriate Windows Firewall rules on the host IIS server to allow the inbound connection. This post will walk you through the specifics of adding the appropriate Windows Firewall rules. If you are attempting to make changes to the ahadmin COM+ package and the configuration dialog options on the Endpoints tab (and any other tabs) are grayed out see this ServerFault post.  You will have to close and re-open dcomcnfg.exe after you make the registry changes.
 
Once the WMSVC service is up and running, the .NET code necessary to get the log file location is relatively straight-forward; the only difference is that you invoke the ServerManager’s static OpenRemote method instead of calling the class constructor:
var serverManager = ServerManager.OpenRemote("myhost");

//the component does not actually attempt to perform the connection until you get something
foreach(var site in serverManager.Sites)
{
System.Diagnostics.Debug.Print(System.IO.Path.Combine(site.LogFile.Directory, "WSVC"+site.Id));
}