For azure function v2, the ReadFrom.Configuration does not work.
I have a azure function v2, and configured all settings in appsettings.json.
And in the function.cs, I use the ReadFrom.Configuration to try read settings and write log to azure blob storage. But there is no logs generated in blob storage(the function runs without errors).
my function.cs:
[FunctionName("Function1")]
public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ExecutionContext context)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile(@"appsettings.json", false, true)
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
Log.Logger.Information("test 123.");
}
my appsettings.json
{
"Serilog": {
"WriteTo": [
{
"Name": "AzureBlobStorage",
"Args": {
"connectionString": "storage account connection string",
"storageContainerName": "test-1",
"storageFileName": "testfile.txt"
}
}
]
}
}
See answer to your question here https://stackoverflow.com/questions/55023824/azure-function-failing-to-initialise-serilog-sink-when-using-appsettings-json#55067729
@imeya my knowledge of Azure Functions is fairly limited, but I believe that appsettings.json is just for local development, and once you publish your function you will have to store values in the Application Settings area of the function app. You can't read from an appsettings file in a published function.
Once you have your values there, you will configure Serilog within your static function execution method, after having retrieved your connection string. For example:
private static string azureConnectionString;
[FunctionName("ProcessUsers")]
public static async Task Run([QueueTrigger("users", Connection = "AzureStorage")])
{
azureConnectionString = GetEnvironmentVariable("AzureStorage");
Log.Logger = new LoggerConfiguration()
.WriteTo.AzureBlobStorage(azureConnectionString, LogEventLevel.Warning, "test-1", "testfile.txt")
.CreateLogger();
//method continues....
}
//convenience helper method
private static string GetEnvironmentVariable(string name)
{
return Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
}
I don't have any indication that you'll be able to store the entire configuration in the functions' settings area and read it all out like you can with the appsettings.json file.
See answer to your question here https://stackoverflow.com/questions/55023824/azure-function-failing-to-initialise-serilog-sink-when-using-appsettings-json#55067729
Well this took me hours to find... Only had to add
"Using": [
"Serilog.Sinks.Seq"
],
So in my case, I have an old Azure Function in process. I only want Seq during development so I want to add it to appsettings.Development.json. My code looks like this now:
[assembly: FunctionsStartup(typeof(MyApp.Startup))]
namespace MyApp;
public class Startup : FunctionsStartup
{
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
FunctionsHostBuilderContext context = builder.GetContext();
builder.ConfigurationBuilder
.AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.json"), optional: true, reloadOnChange: false)
.AddJsonFile(Path.Combine(context.ApplicationRootPath, $"appsettings.{context.EnvironmentName}.json"), optional: true, reloadOnChange: false)
.AddEnvironmentVariables();
}
public override void Configure(IFunctionsHostBuilder builder)
{
var configuration = builder.GetContext().Configuration;
ConfigureLogging(builder.Services, configuration, builder.GetContext().EnvironmentName);
}
/// <summary>
/// Configure logging for Azure functions.
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
private static void ConfigureLogging(IServiceCollection services, IConfiguration configuration, string environmentName)
{
var serilogOptions = configuration.GetSection(SerilogOptions.Serilog).Get<SerilogOptions>();
if (!Enum.TryParse<LogEventLevel>(serilogOptions.MinimumLevel.Default, true, out var minimumLevel))
{
minimumLevel = LogEventLevel.Information;
}
var lg = new LoggerConfiguration()
.MinimumLevel.Is(minimumLevel)
.ReadFrom.Configuration(configuration)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Worker", LogEventLevel.Warning)
.MinimumLevel.Override("Host", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Function", LogEventLevel.Warning)
.MinimumLevel.Override("Azure.Storage.Blobs", LogEventLevel.Warning)
.MinimumLevel.Override("Azure", LogEventLevel.Warning)
.MinimumLevel.Override("Azure.Core", LogEventLevel.Warning)
.MinimumLevel.Override("DurableTask", LogEventLevel.Warning)
.Enrich.WithProperty("ApplicationContext", AppName)
.Enrich.WithMachineName()
.Enrich.WithCorrelationId()
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.ApplicationInsights(TelemetryConfiguration.CreateDefault(), TelemetryConverter.Traces);
Log.Logger = lg.CreateLogger();
services.AddLogging(lb =>
{
lb.AddSerilog(Log.Logger, true);
});
}
}
appsettings.Development.json
{
"Serilog": {
"Using": [
"Serilog.Sinks.Seq"
],
"WriteTo": [
{
"Name": "Seq",
"Args": { "ServerUrl": "http://localhost:5341" }
}
]
}
}
Does anyone have an example of how to get the "Serilog" settings into the Azure App Settings? ie the values from local.settings.json to the deployed function in Azure Portal.
@SCC-BDuncan This question would be best asked over in the Serilog area, as your question is not specific to AzureBlobStorage sink