Summary of knowledge points of NLog routing rules and context information

  • 2021-11-10 09:22:47
  • OfStack

NLog Configure Routing Rules and Context Information

rules: Rule Node

logger: 1 routing rule


 <rules>
    <!--<logger name="*" writeTo="console" />-->
    <logger name="*" minlevel="Debug" writeTo="debugger" />
    <logger name="*" minlevel="Error" writeTo="error_file" />
  </rules>

Specific parameter description:

Routing rules are mainly used to match logs with output targets. They generally have the following attributes

name-Name of log source/logger (wildcard character * allowed) minlevel-Minimum level of matching log range maxlevel-Match the highest level of log range level-Single log level of matching levels-Matched series 1 log levels, separated by commas. writeTo-Series 1 targets to which logs should be written when rules match < target > The name attribute of the node, separated by commas. final-Marks the current rule as the last rule. Immediate matching of subsequent rules will not be run.

Such as:

< logger name="Name.Space.Class1" minlevel="Debug" writeTo="f1" / > -All log information of the class Class1 under the namespace Name. Space at levels equal to or higher than Debug is written to the target "f1".

< logger name="Name.Space.Class1" levels="Debug,Error" writeTo="f1" / > -Namespace Name. Space All log information of the class Class1 at level equal to Debug or Error is written to the target "f1".

< logger name="Name.Space.*" writeTo="f3,f4" / > -All levels of log information for all classes under the namespace Name. Space are written to the "f3" and "f4" targets.

< logger name="Name.Space.*" minlevel="Debug" maxlevel="Error" final="true" / > -Log information for all classes under the namespace Name. Space at levels between Debug and Error (including Debug, Info, Warn, Error) is not logged (because writeTo is not defined in this rule), and other subsequent rules are ignored (because final= "true" is set here).

NLog supports the following record levels:

Trace-the most common record information, 1 general for general output Debug-Also logs information, but appears 1 less frequently than Trace, and 1 is generally used to debug programs Info-Messages of Information Type Warn-Warning message, 1 generally used in more important situations Error-Error Messages Fatal-Fatal exception information. 1 Generally speaking, after a fatal exception, the program will not be able to continue execution.

Priority: Trace > Debug > Info > Warn > Error > Fatal

Layout Context Information Expression

${date} Date 2016/08/11 09:34: 33.793

${time} 24 hours HH: mm: ss. mmm.

${longdate} Long time 2016-08-05 14:06:18.9293

${shortdate} Date 08-05, 2016

${basedir} Root Directory

${message} information

${level} level

${stacktrace} stack information

${callsite} log source

That's all about NLog routing rules and context information. Thank you for your support.


Related articles: