-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fluent Logger API
Rolf Kristensen edited this page Oct 14, 2024
·
9 revisions
NLog 5.0 replaces the Fluent-namespace with the new optimized LogEventBuilder
, that skips memory-allocation when LogLevel not enabled. The new fluent builder 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.
_logger.ForInfoEvent()
.Message("This is a fluent message {0}.", "test")
.Property("PropertyName", "PropertyValue")
.Log();
try
{
string text = File.ReadAllText(path);
}
catch (Exception ex)
{
_logger.ForExceptionEvent(ex)
.Message("Error reading file '{0}'.", path)
.Property("Test", "InfoWrite")
.Log();
}
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
- Articles about NLog
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json