1

It's possible to add a new column in a Serilog Table in Azure table Storage for logging new extra field like accountID or login Name?

I think it's possible to add a new column but its possible to pass extra fields in Serilog like i said for the new added columns? How i can define that in Startup.cs or web.config? Thanks

This is my configuration in web.config:

<add key="serilog:using:AzureTableStorage" value="Serilog.Sinks.AzureTableStorage" />
    <add key="serilog:write-to:AzureTableStorageWithProperties.connectionString" value="DefaultEndpointsProtocol=https;AccountName=MyAccountName;AccountKey=MyAccountKey;EndpointSuffix=core.windows.net" />
    <add key="serilog:write-to:AzureTableStorage.formatter" value="Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" />
    <add key="serilog:write-to:AzureTableStorageWithProperties.storageTableName" value="Serilog" />

startup.cs configuration:

Log.Logger = new LoggerConfiguration()
        .ReadFrom.AppSettings()
        .CreateLogger();
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Erased
  • 221
  • 1
  • 10
  • @Jawad i put my startup configuration, can you give me some pratical example? – Erased Jun 17 '20 at 21:31
  • 1
    I dont want to duplicate the effort, [see this thread](https://stackoverflow.com/q/51261177/1390548) for enriching your Logger with additional fields. You wont be able to do that in LoggerConfiguration since the account that loads it will not be the UserID that you most likely want to capture. You can also customize the BatchFormatter to modify the event json before sending it to AzureTableStorage – Jawad Jun 17 '20 at 21:35
  • 1
    please refer to https://stackoverflow.com/questions/49802904/log-structured-data-to-azure-storage-table-by-serilog-store-all-object-in-render – Jim Xu Jun 18 '20 at 08:46
  • @Erased Is useful for you? – Jim Xu Jun 19 '20 at 03:21

1 Answers1

4

If you want to add additional properties when you write log to azure table storage with Serilog, you need to call Enrich.FromLogContext() and LogContext.PushProperty. With the two methods, your application will add additional properties to Azure table. For more details, please refer to the document

For example

  1. Install SDK
Install-Package Serilog.Sinks.AzureTableStorage 
Install-Package Serilog.Enrichers.Thread
  1. Code
static void Main(string[] args)
        {

            var storage =CloudStorageAccount.Parse("");
            string tableName = "log";



            var _log = new LoggerConfiguration()
                           .Enrich.FromLogContext()
                           .WriteTo.AzureTableStorageWithProperties(storage, LogEventLevel.Information, storageTableName: tableName, propertyColumns: new string[] { "AccountId" , "Name" }) ;
            var logger = _log.CreateLogger();
            var exampleuser = new User { AccountId = 3, Name = "Allen" };
            LogContext.PushProperty("AccountId", exampleuser.AccountId);         
            logger.Information("{@User}", exampleuser);

            exampleuser = new User { AccountId = 1, Name = "Jim" };
            LogContext.PushProperty("AccountId", exampleuser.AccountId);
            logger.Information("{@User}", exampleuser);


            Console.ReadLine();



        }
 class User
    {
        public int AccountId { get; set; }
        public string Name { get; set; }

    }

enter image description here

Jim Xu
  • 21,610
  • 2
  • 19
  • 39