简体   繁体   中英

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:

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.

What am I missing?

How can I read the logs from table storage?

Logs in Azure Table are like this,

在此处输入图片说明

To read them out, you could use the Azure Table Storage C# SDK to read data from the log table.

  1. Define a entity which inherited from 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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