NLog.config中AzureStorage目标batchSize不生效及超时配置咨询
问题解答
一、batchSize配置不生效的排查方向
动态Blob名称导致批次拆分
你的blobName配置中包含${event-properties:customernumber}动态参数,如果测试时未给所有日志设置统一的customernumber事件属性,或者每次日志的该属性值不同,会导致日志被写入不同的Blob文件,无法积累到设定的batchSize。测试时可以临时将blobName改为固定值(比如test/Logs_${date:format=yyyy-MM-dd}.txt),或者在测试代码中添加统一的事件属性:// 在循环内添加 using (NLog.MappedDiagnosticsLogicalContext.SetScoped("customernumber", "test-customer")) { _logger?.Info("print line no: {LineNo}", i); }检查Blob块大小限制
Azure Blob存储的单个块最大为100MB(针对块Blob),如果单条日志长度较大,10000条日志的总大小超过块大小限制,NLog会自动拆分批次,导致CommittedBlockCount和默认值一致。可以计算单条日志的长度,估算批次总大小是否超过限制。确认配置项兼容性
确保使用的NLog.Extensions.AzureStorage版本确实支持batchSize配置,部分旧版本可能没有该参数,建议核对当前版本的官方文档。
二、超时自动推送的配置方法
可以通过flushTimeout配置项实现超时推送,单位为毫秒。当积累的日志未达到batchSize但超过设定的超时时间时,会自动将当前批次推送至Azure Blob。修改你的NLog.config配置如下:
<target xsi:type="AzureBlobStorage" name="azure" layout="${longdate}|${level:uppercase=true}|${machinename}|${callsite:className=true:includeSourcePath=false:methodName=false}|${callsite:className=false:includeSourcePath=false:methodName=true}|${message} ${exception:format=tostring}" connectionString="${gdc:item=BlobConnectionString}" container="azuresyncservice" blobName="${event-properties:environment}/Logs/Logs_${event-properties:customernumber}_${date:format=yyyy-MM-dd}.txt" batchSize="10000" queueLimit="10000" flushTimeout="30000" /> <!-- 30秒超时,可根据需求调整 -->
内容的提问来源于stack exchange,提问作者Rizwan Gohar
相关产品推荐
相关产品推荐

