I log to Azure Blob Storage and want to have one three logfiles (containing different things) per start and I use a cached timestamp for this. Ideally the timestamp should be the same in all three files, but they differ if I log to the specific targets at different times.
I set the variable like this
<variable name='blobprefix' value='${cached:cached=true:Inner=${date:format=yyyy-MM-dd\\THH.mm.ss}' />
And this is how I use it for every blob:
<target type="AzureBlobStorage"
name="general-azureblob"
connectionString="${blobConnection}"
blobName="${blobprefix}.log">
</target>
How can I have the variable evaluate exactly once.
You can use the ${processinfo:StartTime} like this:
<variable name='blobprefix' value='${processinfo:StartTime:format=yyyy-MM-dd\\THH.mm.ss:cached=true}' />
If you want to control the StartTime, then you can use NLog GDC and do the following at application startup (Before creating the first NLog Logger-object):
NLog.GlobalDiagnosticsContext.Set("StartupTime", DateTime.UtcNow);
And have the following in NLog.config (whenEmpty
is only there as fallback):
<variable name='blobprefix' value='${gdc:StartupTime:format=yyyy-MM-dd\\THH.mm.ss:cached=true:whenEmpty:${date:format=yyyy-MM-dd\\THH.mm.ss:cached=true}}' />