2015-11-10 83 views

回答

0

你可以使用menthod「Get Build」。它返回IBuildDetail,使用IBuildDetail.LogLocation屬性獲取此版本的日誌文件。最後下載或查詢它。下面是一個演示代碼:

publicstaticvoid GetBuildLogAndReadThroughTheLog() 
    { 
     // Get all the team projects using the TFS API 
     var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(ConfigurationManager.AppSettings["TfsUri"])); 
     var versionControlService = tfs.GetService<VersionControlServer>(); 
     var teamProject = versionControlService.GetAllTeamProjects(false)[0]; 
     // Use the build service to get build details for a team project 
     var buildService = tfs.GetService<IBuildServer>(); 
     // Get the build details 
     var build = buildService.QueryBuilds(teamProject.Name)[0]; 
     // Get the location of the build Log 
     var buildLogLocation = build.LogLocation; 
     // Get the log file 
     var logFile = versionControlService.GetItem(buildLogLocation); 
     // Do you want to download the file to a local path? 
     TextReader tr = new StreamReader(logFile.DownloadFile()); 
     string input = null; 
     while ((input = tr.ReadLine()) != null) 
     { 
     } 
    } 
    } 

整個構建日誌可以在Tbl_BuildInformation的Tfs_YourTeamProjectCollection數據庫中找到。您還可以查詢,並從數據庫中獲取它 更多的方法,供您參考:https://paulselles.wordpress.com/2013/11/15/tfs-build-log-querying-build-log-data/

更新

使用「的StreamReader」將逐行讀取日誌文件行。

using (StreamReader sr = new StreamReader(buildLogLocation) 
{ 
    while (sr.Peek() >= 0) 
      { 
       // do your work here... 
      } 
    } 
+0

嗨,帕特里克,謝謝你的迴應。當試圖使用代碼時,該行:var logFile = versionControlService.GetItem(buildLogLocation);引發異常。任何想法爲什麼? – BigAlbert

+0

你得到了哪些例外?你是否通過編程成功連接到TFS?在谷歌中有很多關於它的博客。 –

+0

是的,連接到TFS是成功的,我可以看到關於構建的各種信息。然而,當代碼到達var logFile = versionControlService.GetItem(buildLogLocation); ItemNotMappetException被引發。請注意buildLogLocation = \t「#/ 29007/logs」。 – BigAlbert