azure blob 存储帐户 - 未按照设置生成日志文件

问题描述 投票:0回答:1

我需要每天添加登录我的天蓝色存储帐户。这是代码。根据代码,日志文件应生成为 service-2025-01-16.log、service-2025-01-17.log 但这里并没有生成文件。我应该更新 Azure 门户上的任何配置吗?

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
});

这是日志语句

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());
}
azure logging azure-blob-storage
1个回答
0
投票

service-2025-01-17.log 但这里没有生成文件。我应该更新 Azure 门户上的任何配置吗?.

您可以使用以下示例代码创建

service-{Date:yyyy-MM-dd}.log
文件,并将其发送到 Azure Blob 存储。

代码:

using Serilog;
using System.Text;
using System.Text.Json;

public class Program
{
    public static void Main(string[] args)
    {

        string currentDate = DateTime.Now.ToString("yyyy-MM-dd");

        Log.Logger = new LoggerConfiguration()
            .WriteTo.AzureBlobStorage(
                connectionString: "your-connection-string", 
                storageContainerName: "container-name",
                storageFileName: $"service-{currentDate}.log",
                restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information // Minimum log level
            )
            .CreateLogger();

        // Example usage of the LogError method
        try
        {
            // Simulate some code that throws an exception
            throw new InvalidOperationException("Something went wrong!");
        }
        catch (Exception ex)
        {
            var model = new { Name = "John", Age = 30 };  // Sample request body
            LogError(Log.Logger, ex, model, "ProcessRequest");
        }

        // Ensure to flush and close the logger
        Log.CloseAndFlush();
    }

    public static void LogError<T>(ILogger logger, Exception ex, T model, string methodName)
    {
        // Build the log message
        StringBuilder sb = new();
        sb.AppendLine("Error with " + methodName + ": " + ex.Message);
        sb.AppendLine("Request Body: " + JsonSerializer.Serialize(model));

        logger.Error(ex, sb.ToString());
    }
}

上面的代码将

Serilog
配置为将错误记录到
Azure Blob Storage
,日志文件使用当前日期命名(带有
service-YYYY-MM-DD.log
)。它模拟错误,将其记录到指定的 Blob 容器,并确保使用
Log.CloseAndFlush()
写入日志。日志条目存储在 Azure Blob 存储帐户的
logs
容器中。

传送门: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.