Skip to content
Rolf Kristensen edited this page Dec 28, 2024 · 11 revisions

Fluent Logger API

NLog 5.0 introduces new fluent LogEventBuilder that can be activated with one of these extensions methods for ILogger:

  • ForLogEvent - Builds base LogEvent
  • ForTraceEvent - Builds LogEvent with Trace-LogLevel
  • ForDebugEvent - Builds LogEvent with Debug-LogLevel
  • ForInfoEvent - Builds LogEvent with Info-LogLevel
  • ForWarnEvent - Builds LogEvent with Warn-LogLevel
  • ForErrorEvent - Builds LogEvent with Error-LogLevel
  • ForFatalEvent - Builds LogEvent with Fatal-LogLevel
  • ForExceptionEvent - Builds LogEvent with Exception and Error-LogLevel

To complete the fluent builder then call Log() to capture available callsite information with help from Caller Member Attributes, and then writes the actual LogEvent to any configured NLog targets.

Replaces the old Fluent-namespace and also reduces memory-allocation when LogLevel not enabled.

Examples

Writing Info Message

_logger.ForInfoEvent()
       .Message("This is a fluent message {0}.", "test")
       .Property("PropertyName", "PropertyValue")
       .Log();

Writing Exception Message.

try
{
    string text = File.ReadAllText(path);
}
catch (Exception ex)
{
    _logger.ForExceptionEvent(ex)
           .Message("Error reading file '{0}'.", path)
           .Property("Test", "InfoWrite")
           .Log();
}
Clone this wiki locally
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy