0

I need to check whether NLog section exists in my app.config and add it if it doesn't.

The app.config should contain a simple NLog configuration like

<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
<variable name="myvar" value="myvalue" />
<variable name="globalLevel" value="debug" />

<targets async="true">
    <target name="logfile" xsi:type="File" fileName="${basedir}/logs/${shortdate} mylogfile ${environment:COMPUTERNAME}.log" />
</targets>

<rules>
    <logger name="*" minlevel="${globalLevel}" writeTo="logfile" layout="${longdate} ${level} ${message}" />
</rules>

I cannot seem to find a method in NLog API which writes to config file. I can load the Config in many ways, for example

var config = new NLog.Config.XmlLoggingConfiguration("NLogTest.exe.config");

Then I can add/modify the config adding targets, rules, etc., but how do I write it to my config file?

Yuri M.
  • 11
  • 3
  • Does this answer your question? [Write values in app.config file](https://stackoverflow.com/questions/4758598/write-values-in-app-config-file) – Adassko Jan 06 '22 at 17:29
  • this doesn't help as I need a custom nlog section not AppSettings – Yuri M. Jan 06 '22 at 17:32
  • Others also wish to serialize the in-memory NLog-configuration and save to file: https://github.com/NLog/NLog/issues/4517 . I would probably focus on saving NLog-config-variables into a custom `NLog-user.config` and then have standard `NLog.config` including the user-file: https://github.com/NLog/NLog/wiki/XML-config-include-Example – Rolf Kristensen Jan 06 '22 at 20:33
  • See also: https://github.com/NLog/NLog/issues/4722 – Rolf Kristensen Jan 08 '22 at 11:48

0 Answers0