简体   繁体   中英

.NET Core 3.1: Serilog - WriteTo AzureTable Storage - Create table if not exists

My app is .NET Core 3.1. based and I am using Serilog for logging. Sink that I use to store logs is AzureTableStorage. Is there a way to a set an option to create table if not exists when writing logs to Azure Table Storage? Table is created once app is started, but what if I want to create tables dynamically, for example, by days?

Thank you in advance!

It sounds like you're essentially after a 'rolling' version of the Serilog.Sinks.AzureTableStorage sink that creates a new table per day, and that is also resilient to the table suddenly disappearing while the app is running. Neither of those things are currently supported by the sink in its current form, so you'd have to create your own ILogEventSink to achieve what you're after. Some things to consider:

  • A good place to start is probably by looking at the existing AzureBatchingTableStorageSink .
  • To handle the case where the table is deleted while the app is running, be aware that the CreateIfNotExists() method makes an HTTP request to the blob service every time it is called, so doing it for every log event (or batch) will effectively double the number of HTTP requests made by the sink. Instead, you could optimistically write events to the table and only create the table if you get an error code indicating that the table does not exist.
  • The table name will have to be dynamically determined based on the LogEvent.Timestamp property. Since your event logging mechanism needs to account for the table not existing (as explained above), there's no real need to explicitly create a new table when the name changes. This should happen automatically when you attempt to write a log event.

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