azureloggingazure-blob-storage

azure blob storage account - log file is not generated as per setup


I need to add log in my azure storage account on daily basis. here is the code. As per code log file should be generated as service-2025-01-16.log, service-2025-01-17.log But it is not generating file here. Should I update any configuration on Azure portal?

builder.Host.UseSerilog((context, services, configuration) =>
{
    configuration.ReadFrom.Configuration(context.Configuration)  // Reads settings from appsettings.json
                .Enrich.FromLogContext()  // Adds contextual info to logs
                .WriteTo.AzureBlobStorage(
                    connectionString: context.Configuration["AzureBlobStorageConnectionString"],  // Connection string from config
                    storageContainerName: context.Configuration["AzureBlobStorageContainer"],  // Azure Blob container name
                    storageFileName: "service-{Date:yyyy-MM-dd}.log",
                    restrictedToMinimumLevel: LogEventLevel.Information);  // Log level to Azure Blob Storage
});

Here is the log statement

public static void LogError<T>(ILogger _logger, Exception ex,T model, string MethodName)
{
    StringBuilder sb = new();
    sb.AppendLine("Error with "+ MethodName+": " + ex.Message);
    sb.AppendLine("Request Body: " + JsonSerializer.Serialize(model));
    _logger.LogError(ex, sb.ToString());
}

Here is the error details, enter image description here

enter image description here


Solution

  • Updated code as below in program.cs. My statement of configuration.ReadFrom was raising error and application stopped working after that.

    // This captures startup issues before Serilog is configured
    Log.Logger = new LoggerConfiguration()
                     .WriteTo.AzureBlobStorage(
                    connectionString: builder.Configuration["BlobconnectionString"],
                    storageContainerName: "api-logs",
                    storageFileName: "{yyyy}/{MM}/{dd}/APIName-api.log",
                    restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information, // Minimum log level
                    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}) {Message}{NewLine}{Exception}"
                )
                     .WriteTo.Debug()
                     .CreateLogger();
    
    builder.Host.UseSerilog();