Quantcast
Channel: NLog
Viewing all articles
Browse latest Browse all 71

Have you seen our new fluent interface? (3.2.0 feature)

$
0
0

With the release of 3.2.0 a new fluent interface has also been introduced. This feature simplifies writing complex logging statements and can be extended with extension methods.

varlogger=LogManager.GetCurrentClassLogger();logger.Info().Message("This is a test fluent message '{0}'.",DateTime.Now.Ticks).Property("Test","InfoWrite").Write();

To start with the fluent interface:

  1. Import the namespace NLog.Fluent
  2. Create a Logger as regular
  3. Start the fluent interface with logger.Log(LogLevel...), logger.Debug(), logger.Error() etc.
  4. Use the fluent interface
  5. Finish the chain with .Write()

Full example

usingSystem;usingNLog;usingNLog.Fluent;classProgram{voidTest(){varlogger=LogManager.GetCurrentClassLogger();try{//ex 1logger.Info().Property("id",123).Property("category","test").Write();DoCrash();}catch(Exceptionex){//ex 2logger.Log(LogLevel.Error).Exception(ex).Message("log a message with {0} parameter",1).Write();throw;}}privatestaticvoidDoCrash(){//..}}

Viewing all articles
Browse latest Browse all 71

Trending Articles