NLog rewriting my config file?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

NLog rewriting my config file?

dave
I have a couple of applications that I am working on. Both of them utilize NLog. Both of them have an event source registered on my computer. When I start one of them it works, and writes things to the windows event log. No problems. When I start the other app NLog apparently decides it doesn't want to write to the event log, and it rewrites the NLog.config file to write to a default file with default settings instead. That is maybe ok on my computer, but there is no way I want that to ever happen in a production environment. How can I prevent the file from getting rewritten?

My NLog.config file looks like this Before:

    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <targets>
            <target name="eventlog" xsi:type="EventLog" log="Application" source="MyAppEventSource" layout="${level:uppercase=true} | ${logger} | ${message} ${onexception:inner=${newline}${exception:format=tostring}}" />
            <target name="file" xsi:type="File" fileName="${basedir}/log.txt" layout="${longdate}|${level:uppercase=true}|${logger}|${message}${onexception:inner=${newline}${exception:format=tostring}}" />
        </targets>
        <rules>
            <logger name="*" minlevel="Trace" writeTo="eventlog,file" />
        </rules>
    </nlog>

After it gets rewritten it looks like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <!-- make sure to set 'Copy To Output Directory' option for this file -->
    <!-- go to http://nlog-project.org/wiki/Configuration_file for more information -->

    <targets>
        <target name="eventlog" xsi:type="EventLog" log="Application" category="${level}" source="MyAppEventSource" />
        <target name="file" xsi:type="File" fileName="${basedir}/log.txt" />
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
    </nlog>

As a side note, both of these applications are services as well. If I run the problematic one as a service instead of in the console, then everything works well. The other app works correctly whether it is running as a service or not. The NLog.config files for both apps are identical, except for the event log source. Any idea what could cause this?
Reply | Threaded
Open this post in threaded view
|

Re: NLog rewriting my config file?

dave
I finally figured out what was happening. It has nothing to do with NLog. I have a few executables in the same solution, pointed to the same output folder. I added NLog.config files to all of them. Somehow it never occurred to me that these would all end up in the same folder and be conflicting with each other.

When I built normally it ended up pulling the NLog.config for my main project, but when debugging through VS2010, it ended up using one of the other NLog.config files for some reason. Thus the strange issue where the NLog.config file would be "overwritten" based on how I start the program.

The solution is obviously to remove the redundant NLog.config files from the secondary executables and stick with just one.