2017-05-28 5 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を作成できます。 AzureTableStorageListenerを作成しました。AzureTableStorageListenerはトレースログをAzure Tableに書き込みます。下のコードは参照用です。

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 –

関連する問題