2017-05-31 106 views
0

我們的網絡應用託管在Azure應用服務上。它生成本地存儲在/ site/wwwroot/Log /中的應用程序日誌。日誌的大小是有限的,所以我們有log.txt(最新),log0.txt..log20.txt(0是最早的)。從Azure應用程序服務導出應用程序日誌的最佳方式是什麼?

我想讓這些日誌可供團隊的其他成員使用,而不必讓他們通過ftp寫入Web應用程序。

我探討了提供ftp讀取訪問,但Azure無法實現這一點。

我願意在幾小時內實現的非常簡單的解決方案以及整體解決方案(日誌分析解決方案等)。預算是一個問題。

我們也使用臨時插槽,這可能會使問題複雜化,但現在是次要的問題。

我該如何做到最好?

回答

0

我想讓這些日誌可供團隊的其他成員使用,而不必讓他們通過ftp寫入Web應用程序。

Azure Web App提供了2種存儲應用程序日誌的方法。文件系統和Blob存儲。您可以打開Blob存儲以將應用程序日誌保存到Azure Blob存儲。之後,我們可以創建一個共享訪問簽名(SAS),它可以授予Blob存儲的只讀訪問權限。

如果您不想打開應用程序日誌Blob存儲,您可以從文件系統讀取日誌並將它們共享給您的團隊成員。日誌文件存儲在「D:\ home \ LogFiles」中。我們可以使用.NET Framework提供的File和Directory類將它們讀出來。我創建了一個ASP.NET MVC示例代碼來完成它。以下代碼供您參考。

將用於傳遞文件夾和文件信息以查看的模型。

public class LogFolder 
{ 
    public LogFolder() { } 

    public LogFolder(string virtualPath) 
    { 
     FolderVirtualPath = virtualPath; 
    } 

    public string FolderName 
    { 
     get 
     { 
      int lastIndex = FolderVirtualPath.LastIndexOf(@"\"); 
      if (lastIndex < 0 && FolderVirtualPath.Length > 0) { return FolderVirtualPath; } 
      else { return FolderVirtualPath.Substring(lastIndex + 1); } 
     } 
    } 

    public string FolderVirtualPath { get; set; } 

    public string FolderPath { 
     get 
     { 
      return Path.Combine(@"D:\home\LogFiles\", FolderVirtualPath); 
     } 
    } 

    public List<LogFolder> SubFolders { get; set; } 

    public List<LogFile> SubFiles { get; set; } 

    public void GetSubFilesAndFolders() 
    { 
     SubFolders = new List<LogFolder>(); 
     IEnumerable<string> folders = Directory.EnumerateDirectories(FolderPath); 
     foreach (var folder in folders) 
     { 
      SubFolders.Add(new LogFolder(folder.Replace(@"D:\home\LogFiles", ""))); 
     } 
     SubFiles = new List<LogFile>(); 
     IEnumerable<string> files = Directory.EnumerateFiles(FolderPath); 
     foreach (var file in files) 
     { 
      SubFiles.Add(new LogFile(file.Replace(@"D:\home\LogFiles", ""))); 
     } 
    } 
} 

public class LogFile 
{ 
    public LogFile() { } 

    public LogFile(string virtualPath) 
    { 
     FileVirtualPath = virtualPath; 
    } 

    //Used to display the file name 
    public string FileName { get { return Path.GetFileName(FilePath); } } 

    public string FileVirtualPath { get; set; } 

    //used to download the file 
    public string FilePath 
    { 
     get { return Path.Combine(@"D:\home\LogFiles\", FileVirtualPath); } 
    } 
} 

控制器,其用於顯示從服務器端文件夾信息和下載文件。

public ActionResult Folder(string folderVirtualPath) 
{ 
    LogFolder folder = new LogFolder(folderVirtualPath); 
    folder.GetSubFilesAndFolders(); 
    return View(folder); 
} 

public ActionResult DownloadFile(string fileVirtualPath) 
{ 
    LogFile file = new LogFile(fileVirtualPath); 
    if (System.IO.File.Exists(file.FilePath)) 
    { 
     return File(System.IO.File.ReadAllBytes(file.FilePath), "application/octet-stream", file.FilePath); 
    } 
    else 
    { 
     return HttpNotFound("File Not Found"); 
    } 
} 

查看它是用來顯示文件夾信息。

@model TestAccessFIles.Controllers.LogFolder 
@{ 
    ViewBag.Title = "Folder"; 
} 

<h2>Folder : @Model.FolderVirtualPath </h2> 

<h3>Sub Folders</h3> 
<ul> 
@foreach (var folder in Model.SubFolders) 
{ 
    <li><a href="@Url.Action("Folder",new { folderVirtualPath = folder.FolderVirtualPath })">@folder.FolderName</a></li> 
} 
</ul> 

<h3>Sub Files</h3> 
<ul> 
    @foreach (var file in Model.SubFiles) 
    { 
     <li><a href="@Url.Action("DownloadFile",new { fileVirtualPath = file.FileVirtualPath })">@file.FileName</a></li> 
    } 
</ul> 
相關問題