Reputation: 4761
I'm working on my .Net Core website and I'm using NLog along with Azure Event hub. I've got that working but the Azure Event hub connection string is hard coded in the NLog.config file right now. I would like to store my Event hub connection string in Azure Key vault and pass that in to Nlog config file. What I'm not sure is How/where do I pass Eventhub connection string to Nlog.config.
This is how I'm taking Azure event hub connection string from my controller (sample)
_logger = logger;
_logger.LogInformation(1, "NLog injected into WeatherForecastController");
var kvUrl = configuration["AzureKeyVaultUrl"];
var secretClient = new SecretClient(
new Uri(kvUrl),
new DefaultAzureCredential());
var eventHubConnString = secretClient.GetSecret("contributor-ConnectionString");
This is My NLog config
<target type="AzureDiagnosticEventHub" name="eh" EventHubConnectionString="XXXEventHubsConnectionStringXXX" PartitionKey="" PublisherId="">
<layout xsi:type="JsonLayout" renderEmptyObject="false">
<attribute name="type" layout="appLog" />
<attribute name="date" layout="${longdate:universalTime=true}Z" />
<attribute name="version" layout="1.0.0.0" />
<attribute name="logService" layout="${LogServiceName}"/>
<attribute name="payload" encode="false">
Upvotes: 0
Views: 275
Reputation: 36780
If the connectionstring could be global, this should work (but depending of the target implementation)
In the xml config:
EventHubConnectionString="${gdc:AzureEventHubConnectionString}"
In code:
NLog.GlobalDiagnosticsContext.Set("AzureEventHubConnectionString", "yourConnectionString");
Upvotes: 1