Ask Your Question
4

What is the process for providing Serilog with an Azure Storage connection string within code, while simultaneously retrieving additional settings from appSettings.json?

asked 2021-06-19 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-06-04 03:00:00 +0000

lakamha gravatar image

The process for providing Serilog with an Azure Storage connection string within code, while simultaneously retrieving additional settings from appSettings.json can be done by following these steps:

  1. Install the Serilog.Sinks.AzureBlobStorage package using NuGet.

  2. Add the following using directives at the top of your code file:

using Serilog;
using Serilog.Sinks.AzureBlobStorage;
using Microsoft.Extensions.Configuration;
  1. Create an instance of IConfiguration in your code to read appSettings.json:
var configurationBuilder = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", false, true);
var configuration = configurationBuilder.Build();
  1. Retrieve the Azure Storage connection string from appSettings.json:
var storageConnectionString = configuration.GetConnectionString("AzureBlobStorage");
  1. Configure Serilog to use Azure Blob Storage sink by creating a new LoggerConfiguration object and specifying the sink configuration as follows:
var log = new LoggerConfiguration()
    .WriteTo.AzureBlobStorage(storageConnectionString, "log-{Date}.txt")
    .CreateLogger();
  1. In the above example, we are specifying the blob container name using the following template: "log-{Date}.txt". This will create a new daily log file with the current date appended to the filename. You can customize this template as per your requirements.

  2. You can now use the log instance to write log statements, such as:

log.Information("Hello, world!");
  1. Finally, dispose of the logger instance when it is no longer needed:
log.Dispose();
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-19 11:00:00 +0000

Seen: 7 times

Last updated: Jun 04 '22