Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve values from C# code into NLog.config file

Tags:

c#

nlog

Is it possible to use "variables" in the NLog.config configuration file to get values from some C# code ?

The reason I want to use a "variable" is to retrieve the password (that I use to log info in my database) previously entered by the user in a windows form.

like image 880
Otiel Avatar asked Jul 13 '11 14:07

Otiel


1 Answers

I found the answer:

I need to use the Global Diagnostics Context class, like that:

  • In the C# code, set the variable: GlobalDiagnosticsContext.Set("myVariableName", "myValue");
  • In the NLog.config file, access to the variable : ${gdc:item=myVariableName} gives me "myValue"
like image 187
Otiel Avatar answered Sep 22 '22 14:09

Otiel