You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor Server中使用serilog-sinks-azureblobstorage的问题求助

Blazor Server中Serilog Azure Blob存储Sink的问题及排查

最新更新

  • 制作了极简示例,File和AzureBlobStorage Sink均无法正常工作。启用AzureBlobStorage时,加载Blazor_Logger_bug.styles.css会抛出异常,这是页面无法渲染的核心原因。
  • 已自行编写日志记录器,耗时比调试Serilog更短。提醒其他开发者:该组件存在功能异常,还会引发应用无关问题。

问题场景

在Blazor Server应用中使用serilog-sinks-azureblobstorage时遇到多类问题(Serilog的File Sink可正常工作):

配置方式1:预创建Logger实例

var _logger = new LoggerConfiguration()
    .ReadFrom.Configuration(config)
    .CreateLogger();
Log.Logger = _logger;
builder.Logging.AddSerilog(Log.Logger);
  • 日志可成功写入Azure Blob,但应用页面显示空白,页面源码仅包含_Host.cshtml内容,缺失核心的<component>节点。

配置方式2:直接添加Serilog

builder.Logging.AddSerilog();
  • 网页可正常加载,但无任何日志写入Azure Blob。

配置方式3:通过Host配置Serilog

builder.Host.UseSerilog((hostingContext, loggerConfiguration) =>
{
    loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);
});
  • 网页可正常加载,但无日志写入Azure Blob;Console和Debug也无日志输出,推测该配置替换了原有日志设置。

完整相关代码

if (builder.Environment.IsDevelopment())
{
    builder.Logging.ClearProviders();
    builder.Logging.AddJsonConsole();
    builder.Logging.AddDebug();
}
builder.Logging.AddSerilog();

相关配置(appsettings.json)

"Serilog": {
    "Using": [ "Serilog.Sinks.File", "Serilog.Sinks.AzureBlobStorage" ],
    "MinimumLevel": {
        "Default": "Warning",
        "Override": {
            "LouisHowe": "Information",
            "ThirdPartyServices": "Information",
            "CommonUtilities": "Information",
            "Microsoft": "Warning"
        }
    },
    "WriteTo": [
        {
            "Name": "AzureBlobStorage",
            "Args": {
                "connectionString": "*****secret*****",
                "storageContainerName": "logs",
                "storageFileName": "{yyyy}-{MM}-{dd}.txt"
            }
        },
        {
            "Name": "File",
            "Args": {
                "path": "C:/temp/log-.txt",
                "rollOnFileSizeLimit": true,
                "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter,Serilog.Formatting.Compact",
                "rollingInterval": "Day"
            }
        }
    ],
    "Enrich": [ "FromLogContext", "WithThreadId", "WithMachineName" ]
},

调试信息补充

添加Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));后,得到以下调试结果:

配置方式1或3时的输出

2023-10-09T23:06:38.5791924Z Unable to find a method called WithThreadId. Candidate methods are:
Serilog.LoggerConfiguration With(Serilog.Configuration.LoggerEnrichmentConfiguration, Serilog.Core.ILogEventEnricher)
Serilog.LoggerConfiguration AtLevel(Serilog.Configuration.LoggerEnrichmentConfiguration, System.Action`1[Serilog.Configuration.LoggerEnrichmentConfiguration], Serilog.Events.LogEventLevel, Serilog.Core.LoggingLevelSwitch)
Serilog.LoggerConfiguration FromLogContext(Serilog.Configuration.LoggerEnrichmentConfiguration)
2023-10-09T23:06:38.5807420Z Unable to find a method called WithMachineName. Candidate methods are:
Serilog.LoggerConfiguration With(Serilog.Configuration.LoggerEnrichmentConfiguration, Serilog.Core.ILogEventEnricher)
Serilog.LoggerConfiguration AtLevel(Serilog.Configuration.LoggerEnrichmentConfiguration, System.Action`1[Serilog.Configuration.LoggerEnrichmentConfiguration], Serilog.Events.LogEventLevel, Serilog.Core.LoggingLevelSwitch)
Serilog.LoggerConfiguration FromLogContext(Serilog.Configuration.LoggerEnrichmentConfiguration)
  • 此场景下未创建Blob,网页无法显示。

配置方式2时的输出

无任何包含Serilog的调试消息,未创建Blob,但网站可正常加载。


内容的提问来源于stack exchange,提问作者David Thielen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 01:40:31