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

标签 c# asp.net-mvc azure-table-storage serilog

我可以使用 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表中的日志是这样的,

enter image description here

要读出它们,您可以使用 Azure 表存储 C# SDK 从日志表中读取数据。

  1. 定义一个继承自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/

    相关文章:

    c# - 当表中存在记录时,Azure 表存储查询不会从有效筛选器中检索任何实体

    c# - Azure 表存储 - 管理并发

    c# - C#,TeamCity-避免在TeamCity服务器上进行后期构建事件

    c# - 删除可选的最后一个括号

    c# - 未调用 kendogrid 更新

    c# - 在 ASP.NET MVC 中动态生成许多路由...这是一个完全糟糕的主意吗?

    Azure 表存储 : Ignoring a property of a TableEntity when using the Azure. Data.Tables 包

    c# - IDX10501 : Signature validation failed. 无法匹配 key

    c# - 从 DataGrid 中删除无效行后,我无法更新行

    asp.net-mvc - 获取 Controller 和操作的完全限定 URL?