Adding default rules and targets

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

Adding default rules and targets


I'd like to use NLog in my app for general logging.  But I want to 'Hard Code' a specific target and rule to log messages from my data load routines to a specific database table.

Can I programatically add rules and targets once the logger has been created?

The one issue with this that I can see is if autoReload="true" gets set in the config file I guess my hardcoded config will be removed if the config file is changed.

The other way I can think to do this is to have a seperate data load logger and main logger, so could I create a target in the data load logger of the main logger so any warnings or errors in the load process are logged by both?

I'm new to NLog so the above might be total garbage.


Reply | Threaded
Open this post in threaded view

Re: Adding default rules and targets

Jarek Kowalski
The loggers are independent from their configuration. If you create a logger and the configuration file changes the changes are propagated to all "alive" loggers.

There are some options for you:

1. Use include files:

    <include file="hardcoded.xml" />

    ... other rules and targets go here

2. Another solution for you might be a separate LogFactory instance (only added few days ago) and described in this thread:

Loggers created through this logfactory will be independent from the ordinary ones and thus you'll be able to reload the "general" configuration while preventing the "hardcoded"  from reloading.

NLog Blog