在Azure上使用WebApi创建日志

时间:2017-05-28 07:10:30

标签: sql-server asp.net-web-api2 azure-storage-blobs

我目前正在使用WebApi和SQL Server开发新项目,但是我们必须生成和保存日志的每个应用程序,问题是,如何创建它以及正确的存储方式,因为我使用的是Azure有一个Azure Blob Table,听起来非常适合创建日志,但我不是专业人士。所以每个已经登录的用户,如何组织这个,我需要一些帮助!

1 个答案:

答案 0 :(得分:1)

Azure Web App提供应用程序日志功能,我们可以在Azure门户中启用它。

enter image description here

之后,我们可以使用以下代码编写日志,日志将写入我在Azure门户上配置的blob。

Trace.TraceInformation("Hello Azure Log");

如果要将应用程序日志写入Azure表存储,则可以创建自定义TraceListener。我刚创建了一个AzureTableStorageTraceListener,它将跟踪日志写入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>