通过 Serilog 将结构化数据记录到 Azure 存储 table,将所有对象存储在 RenderedMessage 中,我想要 class 中每个字段的列

Log structured data to Azure Storage table by Serilog store all object in RenderedMessage, I want column for each field in class

我使用 SeriLog 将下面的代码写入 log/store 带有对象的日志到 Azure Table 存储,但是我将对象存储在 "RenderedMessage" 列中(在 azure table) 或 "Data" 列,虽然我需要将 class 中的每个 field/property 存储到 Table 存储中的单独列。 请看下面:

 var _simpleTransation = new SimpleTransation(99, "some title9", "type99");

 var logger = new LoggerConfiguration()
                .WriteTo.AzureTableStorage(storage,LogEventLevel.Information,null, "LogStorage",false,null,null,null,false)
                .Enrich.WithProperty("SimpleTransation", "@_simpleTransation", true)
                .Enrich.FromLogContext()
                .CreateLogger()
                .ForContext<SimpleTransation>();

   logger.Information<SimpleTransation>("{@SimpleTransation}", _simpleTransation);

所以我需要向 azure 存储 table 添加代表我的对象字段的列,而不是在 RenderedMessage 日志中序列化我的整个对象?

So I need to add columns to azure storage table that represent my object fields and not serialize my whole object inside RenderedMessage log?

您可以在写入 Azure table 存储时使用 propertyColumns 属性添加新列。

注意:您需要调用 Enrich.FromLogContext() 并且 属性 将显示 在 Azure Table 存储。

这是一篇关于 Enrichment 的文章。可以通过多种方式使用属性丰富日志事件。

您需要使用 LogContext.PushProperty 添加您要添加​​的内容 属性 和值。

您可以参考以下代码试一试:

    static void Main(string[] args)
    {
        var storage = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

        string tableName = "table411";
        var exampleuser = new User { Id = 1, Name = "joey", Age = 23 };
        var _log = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .WriteTo.AzureTableStorageWithProperties(storage, propertyColumns: new string[] { "UserId" });

        LogContext.PushProperty("UserId", exampleuser.Id);

        var logger = _log.CreateLogger();

        logger.Information("{@User}",exampleuser);
    }
    class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }

屏幕截图: