我目前正在使用WebApi和SQL Server處理新項目,但由於每個應用程序都必須生成並保存日誌,因此問題是如何創建它以及存儲的正確方式,因爲我使用的是Azure,所以有一個Azure Blob Table,這聽起來非常適合創建日誌,但我不是專業人士。所以每個已經登錄的用戶,如何組織這個,我需要一些幫助!在Azure上使用WebApi創建日誌
0
A
回答
1
Azure Web App提供應用程序日誌功能,我們可以在Azure門戶中啓用它。
在那之後,我們就可以寫日誌使用下面的代碼和日誌將在我在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 –
相關問題
- 1. WebApi自定義日誌和Azure存儲
- 2. 使用VBS創建日誌
- 3. 在Azure上創建NLog日誌文件的目錄
- 4. 使用Log4j創建每日日誌?
- 5. 使用日誌記錄在多臺機器上創建任務
- 6. 尋找使用WebAPI的日誌記錄模塊的建議
- 7. 在c#中創建日誌#
- 8. 如何使用Java創建MySQL日誌
- 9. 使用python創建只讀日誌
- 10. 使用Log4Net創建ReadOnly日誌文件
- 11. 使用htaccess創建日誌文件
- 12. 創建MySql日誌
- 13. SQL創建日誌
- 14. 如何在使用diagnostics.wadcfg創建的Azure Localstorage中存儲自定義日誌
- 15. Laravel停止在Azure上寫日誌
- 16. Elmah在Azure上的XML日誌文件
- 17. django日誌記錄:未創建日誌
- 18. 如何創建日誌日誌圖
- 19. 在Windows Azure上使用RavenDB Embedded寫入日誌文件失敗
- 20. 如何將日誌從Asp Net Core WebApi存儲到Azure Table Store?
- 21. 使用NLog記錄Azure日誌流
- 22. 使用PowerShell下載Azure診斷日誌
- 23. 在使用tomcat server.xml或web.xml創建新日誌之前備份日誌文件
- 24. Azure日誌管理
- 25. 在客戶機上爲WPF應用程序創建日誌?
- 26. 在Web應用程序上創建日誌
- 27. log4j2不創建RollingFile日誌
- 28. 未創建日誌文件?
- 29. 創建備份日誌C#
- 30. 日誌文件創建
我會去應用程序的見解,因爲這是蔚藍的方式 –