Change Log Level at Runtime

1,045 views
Skip to first unread message

Gavin Hill

unread,
Aug 12, 2013, 5:21:00 AM8/12/13
to nlog-...@googlegroups.com
Firstly, I'm new to NLog so forgive my use of inaccurate terminology.

I have an application that I would like the user to select the level of logging produced and whether it is to a file or not.

The easy way is to edit the NLOG.config XML file but the user doesn't have that level of knowledge.  I would like to user to be able to select the level of logging displayed and whether the log is written to file or not form within the application.  This suggest I need to enable/disable a target and modify the logging rules based on some user-selected variables.

I'm not sure of the most appropriate way to achieve this.  My code is littered with Log.<loglevel>("Message") so it would seem that if I could programatically set the "minlevel" property of each rule I might achieve what I want, but is that the correct approach (and how do I do that?).

It seems I could also write a "proxy" and check my user preference before executing the Log.<loglevel> call, but that would involve eidting all my code to include calls to my new proxy.  Is that a preferred method?

Regards
Gavin.

Kim Christensen

unread,
Aug 13, 2013, 8:35:58 AM8/13/13
to nlog-...@googlegroups.com
Hi,

It is possible to use LoggingConfiguration.FindTargetByName, for example see this test, https://github.com/NLog/NLog/blob/8201a362b8702be97facae2c6af83c2a6e9b54d1/tests/NLog.UnitTests/Config/TargetConfigurationTests.cs.
But in this case I would say it is better to create a proxy, controlling the behaviour.

Gavin Hill

unread,
Aug 13, 2013, 9:32:19 AM8/13/13
to nlog-...@googlegroups.com
Thanks for the advice Kim.  I'm able to change the log level now, but adding/removing targets is proving challenging.  I'm surprised there isn't more native functionality like this built into NLog given most developers desire to have users enable/disable logging to troubleshoot problems in the field.

Regards
Gavin
Reply all
Reply to author
Forward
0 new messages