简体   繁体   English

如何使用Serilog读取写入Azure表存储的日志?

[英]How can I read logs written to Azure table storage with Serilog?

I am able to write logs to Azure table storage in my MVC app using AzureTableStorage like this: 我可以使用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? 如何从表存储中读取日志?

I have spent about 20 minutes looking at documentation on the AzureTableStorage and Serilog GitHub projects and also searched Stack Overflow but I can't figure out how to do it. 我花了大约20分钟的时间查看AzureTableStorageSerilog GitHub项目的文档,还搜索了Stack Overflow,但我不知道该怎么做。

What am I missing? 我想念什么?

How can I read the logs from table storage? 如何从表存储中读取日志?

Logs in Azure Table are like this, Azure Table中的日志是这样的,

在此处输入图片说明

To read them out, you could use the Azure Table Storage C# SDK to read data from the log table. 若要将它们读出,可以使用Azure Table Storage C#SDK从日志表中读取数据。

  1. Define a entity which inherited from TableEnity. 定义从TableEnity继承的实体。
public class LogEntity : TableEntity
{
    public LogEntity() { }


    public string MessageTemplate { get; set; }

    public string Level { get; set; }

    public string RenderedMessage { get; set; }
}
  1. Read logs out using following code. 使用以下代码读取注销。
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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM