**原始答案** <logger>上的minlevel,maxlevel和level属性应为固定字符串.在这种情况下,您可以使用<filter>,例如<nlog> <variable name="loglevel" value="Debug"/> <targets> <target ... /> </targets> <rules> <logger name="*" writeTo="LogFile" > <filter condition="${level} >= ${loglevel}" action="Log"> </logger> </rules></nlog>请参见文档 I would like to use Layouts in NLog to be able to change the minimum log level using an external variable :<nlog> <variable name="loglevel" value="Debug"/> <targets> <target ... /> </targets> <rules> <logger name="*" minlevel="${loglevel}" writeTo="LogFile" /> </rules></nlog>After starting NLog, all log levels (eg : Tracing, Debug, Info, ...) are set to false which indicate NLog failed to parse minlevel attribute properly.The NLog layout feature seems to works with target attributes only.What I want to achieve : in my real app, loglevel is not a constant but rather a custom layout renderer.I have also tried to replace value="Debug" by value="LogLevel.Debug" without success. 解决方案 ** Updated Answer **NLog ver. 4.6 added support for using NLog-Config-Variables in minLevel. See https://github.com/NLog/NLog/pull/2709NLog ver. 4.6.7 added support for adjusting minLevel at runtime, by modifying NLog-Config-Variables and calling ReconfigExistingLoggers(). See https://github.com/NLog/NLog/pull/3184** Original Answer **The minlevel, maxlevel and level attributes on <logger> should be fixed strings.In this case you could use a <filter>, e.g.<nlog> <variable name="loglevel" value="Debug"/> <targets> <target ... /> </targets> <rules> <logger name="*" writeTo="LogFile" > <filter condition="${level} >= ${loglevel}" action="Log"> </logger> </rules></nlog>See the docs 这篇关于在NLog中,是否可以使用布局来定义日志级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 06:07