Reputation: 8616
I am using NLog Azure Storage Logger. My config is as below,
I don't want to put the storage key in the target. Instead, I will use it from app settings so that I can set the Azure website "Application Settings" in the deployment environment. How can I programmatically set the connectionString
for this target?
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
xmlns="http://www.nlog-project.org/schemas/NLog.xsd">
<target type="AzureAppendBlob"
name="azure"
layout="${longdate} ${level:uppercase=true} - ${message}"
connectionString="DefaultEndpointsProtocol=https;AccountName=mylogs;AccountKey=xxxoxxx=="
container="myappcontainer"
blobName="${date:format=yyyy-MM-dd}.log" />
Looking for something like,
public class NLogService: IMyLogService
{
....
public NLogService()
{
Logger azureLogger = LogManager.GetLogger("AzureAppendBlob");
AzureAppendBlobTarget t = dbLogger.????;
t.ConnectionString = "...";
}
}
Upvotes: 1
Views: 1294
Reputation: 36700
The recommended way:
var azureBlobTarget = LogManager.Configuration.FindTargetByName<AzureAppendBlobTarget>("azure");
azureBlobTarget.ConnectionString = "DefaultEndpointsProtocol=https;AccountName=mylogs;AccountKey=xxx";
This also works if NLog is configured with a xml file.
Upvotes: 1
Reputation: 8616
Figured out how to do it,
public NLogService()
{
var config = new LoggingConfiguration();
var azureBlobTarget = new AzureAppendBlobTarget();
config.AddTarget("azureAppender", azureBlobTarget);
azureBlobTarget.BlobName = "${date:format=yyyy-MM-dd}.log";
azureBlobTarget.ConnectionString = "DefaultEndpointsProtocol=https;AccountName=mylogs;AccountKey=xxx";
azureBlobTarget.Container = "myapp";
azureBlobTarget.Layout = @"${longdate} ${level:uppercase=true} - ${message}";
var rule1 = new LoggingRule("*", LogLevel.Debug, azureBlobTarget);
config.LoggingRules.Add(rule1);
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
Upvotes: 0