通过Serilog将结构化数据记录到Azure存储表中,将所有对象存储在RenderedMessage中,我想要类中每个字段的列 [英] Log structured data to Azure Storage table by Serilog store all object in RenderedMessage, I want column for each field in class

查看:74
本文介绍了通过Serilog将结构化数据记录到Azure存储表中,将所有对象存储在RenderedMessage中,我想要类中每个字段的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写以下代码,使用SeriLog将带有对象的日志记录/存储到Azure表存储,但是我将对象存储在"RenderedMessage"列(在Azure表中)或数据"列中,而我需要存储该类中的每个字段/属性都存储在Table存储中的单独列中.请参见以下内容:

  var _simpleTransation = new SimpleTransation(99,"some title9","type99");var logger = new LoggerConfiguration().WriteTo.AzureTableStorage(存储,LogEventLevel.Information,null,"LogStorage",false,null,null,null,false).Enrich.WithProperty("SimpleTransation","@_simpleTransation",true).Enrich.FromLogContext().CreateLogger().ForContext< SimpleTransation>();logger.Information< SimpleTransation>("{@ SimpleTransation}",_simpleTransation); 

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

解决方案

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

写入天蓝色表存储时,可以使用 propertyColumns 属性添加新列.

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

此处是有关

I write below code to log/store logs with an objects to Azure Table Storage by using SeriLog, But I got the object stored in "RenderedMessage" column (in azure table) or "Data" column, While I need to store each field/property in the class to a separated column in the Table storage. Please see below:

 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);

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

解决方案

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

You could use propertyColumns attribute to add a new column when you write to azure table storage.

Note:You need to call Enrich.FromLogContext() and the property would show up in the Azure Table Storage.

Here is an article about Enrichment. Log events can be enriched with properties in various ways.

You need to use LogContext.PushProperty to add what property and value you want to add.

You could refer to the following code to have a try:

    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; }
    }

Screen shot:

这篇关于通过Serilog将结构化数据记录到Azure存储表中,将所有对象存储在RenderedMessage中,我想要类中每个字段的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆