numeric literals in condition

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

numeric literals in condition

Lewap-2
Hello,

I have a problem with numeric literals in condition.

for example:

 private static Logger logger = LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {
            logger.Trace(12);
        }

and in NLog.config:

 <targets>
    <target name="console" xsi:type="ColoredConsole"
            layout="${level}  ${message}"/>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="console">
      <filters>
        <when condition="100  >= '${message}'" action="Ignore"/>
      </filters>
    </logger>
  </rules>

it doesn´t ignore this condition and writes to "console"
Reply | Threaded
Open this post in threaded view
|

Re: numeric literals in condition

Jarek Kowalski
Administrator
When comparing two arguments where one of them is a string, the other is converted to a string before comparison.

So 100 >= '${message'}' becomes '100' >= '12' in your case.

If the comparison used integer numbers 100 >= 12 is true, but when comparing strings, lexicographical (dictionary) order is assumed, so '100' <= '12'.

This can be remedied by writing a function that compares strings as numbers or one that converts string to a number:

100 >= convert-to-integer('${message}')

The functions are usually trivial:

http://svn.nlog-project.org/repos/nlog/trunk/NLog/src/NLog/Conditions/ConditionMethods.cs

BTW. I presume that using ${message} was just for demonstration purposes. It is not a very good idea to (ab)use ${message} for this.

Jarek
NLog Blog
Reply | Threaded
Open this post in threaded view
|

Re: numeric literals in condition

Lewap-2
Czesc Jarek,

Thanks for help and fast answer, I did it ten minutes before you replied me ;)

pozdrawaim lewap