我可以使用 AzureTableStorage
将日志写入 MVC 应用程序中的 Azure 表存储,如下所示:
var storage = CloudStorageAccount
.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
string tableName = Constants.AzureLogTableName;
_log = new LoggerConfiguration()
.WriteTo.AzureTableStorageWithProperties(storage, storageTableName: tableName)
.MinimumLevel.Debug()
.CreateLogger();
_log.Error("Error");
How can I read the logs from table storage?
我花了大约 20 分钟查看 AzureTableStorage 上的文档。和 Serilog GitHub
项目并搜索了 Stack Overflow,但我不知道该怎么做。
我错过了什么?
最佳答案
How can I read the logs from table storage?
Azure表中的日志是这样的,
要读出它们,您可以使用 Azure 表存储 C# SDK 从日志表中读取数据。
- 定义一个继承自TableEnity的实体。
public class LogEntity : TableEntity
{
public LogEntity() { }
public string MessageTemplate { get; set; }
public string Level { get; set; }
public string RenderedMessage { get; set; }
}
- 使用以下代码读取日志。
var storage = CloudStorageAccount
.Parse(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
CloudTableClient tableClient = storage.CreateCloudTableClient();
string tableName = Constants.AzureLogTableName;
CloudTable table = tableClient.GetTableReference(tableName);
TableQuery<LogEntity> query = new TableQuery<LogEntity>();
// Print the fields for each customer.
foreach (LogEntity entity in table.ExecuteQuery(query))
{
Console.WriteLine("{0}, {1}\t{2}\t{3}\t{4}", entity.PartitionKey, entity.RowKey,
entity.MessageTemplate, entity.Level, entity.RenderedMessage);
}
关于c# - 如何使用 Serilog 读取写入 Azure 表存储的日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43834082/