2017-05-28 76 views
0

我目前正在使用WebApi和SQL Server處理新項目,但由於每個應用程序都必須生成並保存日誌,因此問題是如何創建它以及存儲的正確方式,因爲我使用的是Azure,所以有一個Azure Blob Table,這聽起來非常適合創建日誌,但我不是專業人士。所以每個已經登錄的用戶,如何組織這個,我需要一些幫助!在Azure上使用WebApi創建日誌

+0

我會去應用程序的見解,因爲這是蔚藍的方式 –

回答

1

Azure Web App提供應用程序日誌功能,我們可以在Azure門戶中啓用它。

enter image description here

在那之後,我們就可以寫日誌使用下面的代碼和日誌將在我在Azure門戶配置的斑點被寫入。

Trace.TraceInformation("Hello Azure Log"); 

如果要將應用程序日誌寫入Azure表存儲,可以創建自定義TraceListener。我剛剛創建了一個將跟蹤日誌寫入Azure Table的AzureTableStorageTraceListener,下面的代碼供您參考。

public class AzureTableStorageTraceListener : System.Diagnostics.TraceListener 
{ 
    protected override string[] GetSupportedAttributes() 
    { 
     return new[] { "StorageConnectionString", "LogsTableName" }; 
    } 

    public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message) 
    { 
     Write(message, eventType.ToString()); 
    } 

    public override void Write(string message, string category) 
    { 
     string stroageConnectionString = Attributes["StorageConnectionString"]; 
     string tableName = Attributes["LogsTableName"]; 

     // Retrieve the storage account from the connection string. 
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(stroageConnectionString); 

     // Create the table client. 
     CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 

     // Create the CloudTable object that represents the "people" table. 
     CloudTable table = tableClient.GetTableReference(tableName); 

     // Create a new log entity. 
     LogEntity log = new LogEntity(category, message); 

     // Create the TableOperation object that inserts the log entity. 
     TableOperation insertOperation = TableOperation.Insert(log); 
     // Execute the insert operation. 
     table.Execute(insertOperation); 

    } 

    public override void WriteLine(string message, string category) 
    { 
     Write(message + "\n", category); 
    } 

    public override void Write(string message) 
    { 
     Write(message, null); 
    } 

    public override void WriteLine(string message) 
    { 
     Write(message + "\n"); 
    } 
} 

public class LogEntity : TableEntity 
{ 
    public LogEntity(string category, string message) 
    { 
     category = category == null ? "Default" : category; 
     this.PartitionKey = category; 
     this.RowKey = Guid.NewGuid().ToString(); 
     this.Message = message; 
     this.CreatedDate = DateTime.Now; 
    } 

    public LogEntity() { } 

    public string Category { get; set; } 

    public string Message { get; set; } 

    public DateTime CreatedDate { get; set; } 
} 

要使用此TraceListener,需要將以下配置部分添加到Web.config中。

<system.diagnostics> 
    <trace autoflush="true"> 
    <listeners> 
     <add name="AzureTableStorageListener" 
      type="{your namespace name}.AzureTableStorageTraceListener,{your assembly name which contains the custom trace listener}" 
      StorageConnectionString="{your storage connection string}" 
      LogsTableName="{azure storage table name}"/> 
    </listeners> 
    </trace> 
</system.diagnostics> 
+0

謝謝! :d –