- Notifications
You must be signed in to change notification settings - Fork1.4k
Fluent Logger API
Rolf Kristensen edited this pageDec 28, 2024 ·11 revisions
NLog 5.0 introduces new fluentLogEventBuilder
that can be activated with one of these extensions methods forILogger
:
ForLogEvent
- Builds base LogEventForTraceEvent
- Builds LogEvent with Trace-LogLevelForDebugEvent
- Builds LogEvent with Debug-LogLevelForInfoEvent
- Builds LogEvent with Info-LogLevelForWarnEvent
- Builds LogEvent with Warn-LogLevelForErrorEvent
- Builds LogEvent with Error-LogLevelForFatalEvent
- Builds LogEvent with Fatal-LogLevelForExceptionEvent
- Builds LogEvent with Exception and Error-LogLevel
To complete the fluent builder then callLog()
to capture availablecallsite information with help fromCaller Member Attributes, and then writes the actual LogEvent to any configured NLog targets.
Replaces the oldFluent-namespace and also reduces memory-allocation when LogLevel not enabled.
_logger.ForInfoEvent().Message("This is a fluent message {0}.","test").Property("PropertyName","PropertyValue").Log();
try{stringtext=File.ReadAllText(path);}catch(Exceptionex){_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
- All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json