如何在.NET Core 6.0中配置NLog的Azure Event Hub目标(AppSettings版)
使用NLog.Extensions.AzureEventHub连接Azure Event Hub时配置出错,传入连接字符串参数仍报错,官方文档仅提供XML配置示例,需要适用于C# .NET Core 6.0的AppSettings配置示例。
原有配置
"NLog":{ "internalLogLevel":"Info", "internalLogFile":"c:\\temp\\internal-nlog.txt", "extensions": [ { "assembly": "NLog.Extensions.Logging" }, { "assembly": "NLog.Web.AspNetCore" }, { "assembly": "NLog.Extensions.AzureEventHub" } ], "targets": { "allfile": { "type": "File", "fileName": "c:\\temp\\nlog-all-${shortdate}.log", "layout": "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" }, "ownFile-web": { "type": "File", "fileName": "c:\\temp\\nlog-own-${shortdate}.log", "layout": "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" }, "eh": { "type": "AzureEventHub", "credentials": { "name": "eventhub1", "connectionString": "Endpoint=sb://eventhubnp198007.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=4z/JbjHp7UqxCWPD343434343GZbJpfG5E=" }, "layout": "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" } }, "rules":[ { "logger":"*", "minLevel":"Trace", "writeTo":"allfile" }, { "logger":"Microsoft.*", "maxLevel":"Info", "final":"true" }, { "logger":"*", "minLevel":"Trace", "writeTo":"ownFile-web" } ] }
报错信息
2022-08-23 12:59:10.0781 Error AzureEventHub(Name=eh): Failed to
create EventHubClient with connectionString= to EventHubName=.
Exception: System.ArgumentException: Value cannot be an empty string.
(Parameter 'connectionString') at
Azure.Core.Argument.AssertNotNullOrEmpty(String value, String name)
at
Azure.Messaging.EventHubs.Producer.EventHubProducerClient..ctor(String
connectionString, String eventHubName, EventHubProducerClientOptions
clientOptions) at
NLog.Targets.EventHubTarget.EventHubService.Connect(String
connectionString, String eventHubName, String serviceUri, String
tenantIdentity, String resourceIdentity, String clientIdentity) at
NLog.Targets.EventHubTarget.InitializeTarget()
错误原因是AzureEventHub目标的配置参数结构不符合组件要求,JSON配置无需嵌套credentials对象,直接将参数作为目标的一级属性即可。
修改后的完整AppSettings配置:
{ "NLog":{ "internalLogLevel":"Info", "internalLogFile":"c:\\temp\\internal-nlog.txt", "extensions": [ { "assembly": "NLog.Extensions.Logging" }, { "assembly": "NLog.Web.AspNetCore" }, { "assembly": "NLog.Extensions.AzureEventHub" } ], "targets": { "allfile": { "type": "File", "fileName": "c:\\temp\\nlog-all-${shortdate}.log", "layout": "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" }, "ownFile-web": { "type": "File", "fileName": "c:\\temp\\nlog-own-${shortdate}.log", "layout": "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" }, "eh": { "type": "AzureEventHub", "eventHubName": "eventhub1", "connectionString": "Endpoint=sb://eventhubnp198007.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=4z/JbjHp7UqxCWPD343434343GZbJpfG5E=", "layout": "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" } }, "rules":[ { "logger":"*", "minLevel":"Trace", "writeTo":"allfile" }, { "logger":"Microsoft.*", "maxLevel":"Info", "final":"true" }, { "logger":"*", "minLevel":"Trace", "writeTo":"ownFile-web" }, { "logger":"*", "minLevel":"Trace", "writeTo":"eh" } ] } }
关键修改点
- 移除
credentials嵌套对象,将eventHubName(对应原配置中的name)和connectionString作为eh目标的一级配置项 - 在
rules中添加日志写入Azure Event Hub的规则(原配置缺少此规则,即使目标配置正确也不会有日志输出)
报错原因说明
原配置使用的credentials嵌套结构并非NLog.Extensions.AzureEventHub的JSON配置支持的格式,导致组件无法读取到connectionString和eventHubName参数,从而抛出空字符串异常。
内容的提问来源于stack exchange,提问作者sansalk

