在 Azure 上使用 WebApi 创建日志
Creating logs with WebApi on Azure
我目前正在使用 WebApi 和 SQL 服务器开发新项目,但是作为每个应用程序我们都必须生成和保存日志,问题是如何创建它以及正确的存储方式,因为我使用的是 Azure,所以有一个 Azure Blob Table,这听起来很适合创建日志,但我不是专业人士。所以每个已经登录的用户,如何组织这个,我需要一些帮助!
Azure Web App 提供应用程序日志功能,我们可以在Azure 门户中启用它。
之后,我们可以使用以下代码写入日志,日志将写入我在 Azure Portal 上配置的 blob。
Trace.TraceInformation("Hello Azure Log");
如果您想将应用程序日志写入 Azure table 存储,您可以创建自定义 TraceListener。我刚刚创建了一个 AzureTableStorageTraceListener,它将跟踪日志写入 AzureTable,下面的代码供您参考。
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>
我目前正在使用 WebApi 和 SQL 服务器开发新项目,但是作为每个应用程序我们都必须生成和保存日志,问题是如何创建它以及正确的存储方式,因为我使用的是 Azure,所以有一个 Azure Blob Table,这听起来很适合创建日志,但我不是专业人士。所以每个已经登录的用户,如何组织这个,我需要一些帮助!
Azure Web App 提供应用程序日志功能,我们可以在Azure 门户中启用它。
之后,我们可以使用以下代码写入日志,日志将写入我在 Azure Portal 上配置的 blob。
Trace.TraceInformation("Hello Azure Log");
如果您想将应用程序日志写入 Azure table 存储,您可以创建自定义 TraceListener。我刚刚创建了一个 AzureTableStorageTraceListener,它将跟踪日志写入 AzureTable,下面的代码供您参考。
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>