Programmatically retrieving the location of an IIS log file in an ASP.NET application

I am trying to locate the location of the IIS log file of my ASP.NET application. I tried WMI but could not find it. Any suggestions?

Note. I want to be able to get the location programmatically; inside my application for future reference. Edit: Here is my code: this works, but does not give me the actual location of the directory in the log. So this is basically useless.

ManagementPath p2=new ManagementPath("IIsLogModule.Name='logging/Microsoft IIS Log File Format'"); ManagementObject log = new ManagementObject(scope, p2, objectGet); log.Get(); logPath.Text = log["__PATH"].ToString(); 
+7
source share
2 answers

In IIS7, you can use the Microsoft.Web.Administration assembly, the Site class has a property called LogFile , you can get various information about the log file for the site, for example, the directory of log files can be obtained using this code:

  ServerManager manager = new ServerManager(); Site mySite = manager.Sites["SiteName"]; Response.Write("Log file directory : " + mySite.LogFile.Directory + "\\W3svc" + mySite.Id.ToString()); 

I don’t really like this hardcoded part with the directory prefix for the site, but did not find another better way

+5
source

You can use ADSI (or WMI) for this - browse the IIS metabase and find the "LogFileDirectory" property for the node website. For example,

 var root = new DirectoryEntry(@"IIS://localhost/W3SVC"); var sites = root.Children; foreach(DirectoryEntry site in sites) { var name = site.Properties["ServerComment"][0]; var logFile = site.Properties["LogFileDirectory"][0]; } 

Disclaimer: Unverified Code

See this powershell example using a similar idea.

0
source

All Articles