In our applications we log to the database, but our connection strings are encrypted so they need to be decrypted within the application and then the connection string needs to be set via the API.
What I've been doing is using the GDC to set the connection string at runtime:
<connectionString>${gdc:LoggingConnectionString}</connectionString>
GlobalDiagnosticsContext.Set("LoggingConnectionString", decryptedConnectionString);
I've recently discovered variables in NLog and it seems that they accomplish a lot of the same things. My question is when is it more appropriate to use the contexts versus variables. Thanks!
Michael
NLog variables is mainly for use in NLog-config-files, and modifying them at runtime before/after having loaded NLog-config-file can cause some unexpected behavior. Should mainly perform readonly access at runtime.
NLog GDC is mainly for runtime handling only, and will not be affected by modification/loading of NLog-config.
Ok. So in this case where I need to modify a value during runtime, the GDC would be a better way of doing it with less side effects.
Variables would be better for things like a file path that I might need to specify multiple times in my NLog.config.
Thanks!