- Notifications
You must be signed in to change notification settings - Fork1.4k
Fluent Configuration API
The NLog LogFactory has support for fluent setup of the initial NLog configuration.
SetupLogFactory
- LogFactory specific options. Introduced with NLog 5.0SetupInternalLogger
- InternalLogger specific options. Introduced with NLog 4.7SetupExtensions
- Registration of NLog extensions before loading configuration. Introduced with NLog 4.7SetupSerialization
- Override default log output for specific object types. Introduced with NLog 4.7LoadConfigurationFromFile
- Explicit load NLog config from xml file. Introduced with NLog 4.7LoadConfigurationFromXml
- Explicit load NLog config from xml content. Introduced with NLog 4.7LoadConfiguration
- Explicit build/adjust NLog config. Introduced with NLog 4.7 but heavily improved with NLog 5.0
Build NLog config that writes everything to console:
NLog.LogManager.Setup().LoadConfiguration(builder=>{builder.ForLogger().WriteToConsole()});
Build NLog config that writes to file and console:
NLog.LogManager.Setup().LoadConfiguration(builder=>{builder.ForLogger().FilterMinLevel(LogLevel.Info).WriteToConsole();builder.ForLogger().FilterMinLevel(LogLevel.Debug).WriteToFile(fileName:"App_${shortdate}.txt");});
Build NLog config that writes to custom target::
NLog.LogManager.Setup().LoadConfiguration(builder=>{builder.ForLogger().FilterMinLevel(LogLevel.Info).WriteTo(newMyCustomTarget(){Layout="${message}"});});
Build NLog config that restricts output from noisy logger:
NLog.LogManager.Setup().LoadConfiguration(builder=>{builder.ForLogger("Microsoft.*").WriteToNil(finalMinLevel:LogLevel.Warn);builder.ForLogger().FilterMinLevel(LogLevel.Info).WriteToConsole();});
Loads NLog config from appsettings.json (with fallback to NLog.confg) and requiresNLog.Web.AspNetCore:
varlogger=LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
See alsoNLog configuration with appsettings.json
Loads NLog config from Microsoft Configuration Section, and requiresNLog.Extensions.Logging:
varconfig=newConfigurationBuilder().SetBasePath(basePath??Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json",optional:false,reloadOnChange:true).AddJsonFile($"appsettings.{environment}.json",optional:true).AddEnvironmentVariables().Build();varlogger=LogManager.Setup().LoadConfigurationFromSection(config).GetCurrentClassLogger();
See alsoNLog configuration with appsettings.json
Ensure NLog config has been loaded, and fail with exception when not:
NLog.LogManager.Setup().LoadConfigurationFromFile(optional:false);
Load NLog config from custom location (like assembly resource):
varfileXmlContent=File.ReadAllText(xmlFilePath);NLog.LogManager.Setup().LoadConfigurationFromXml(fileXmlContent);
To register a custom LayoutRenderer:
NLog.LogManager.Setup().SetupExtensions(ext=>ext.RegisterLayoutRenderer("trace_id",(logevent)=>CorrelationIdentifier.TraceId.ToString()));
To register a custom Target:
NLog.LogManager.Setup().SetupExtensions(ext=>ext.RegisterTarget<MyNamespace.MyFirstTarget>("MyFirst"));
Explicit configure the global NLog TimeSource:
NLog.LogManager.Setup().SetupLogFactory(fac=>fac.SetTimeSourcAccurateUtc());
Explicit activate exceptions on configuration errors:
NLog.LogManager.Setup().SetupLogFactory(fac=>fac.SetThrowConfigExceptions(true));
Explicit configure the global LogLevel threshold:
NLog.LogManager.Setup().SetupLogFactory(fac=>fac.SetGlobalThreshold(LogLevel.Info));
Write internal warnings and errors to console output:
NLog.LogManager.Setup().SetupExtensions(intern=>intern.SetMinimumLogLevel(LogLevel.Warn).LogToConsole(true));
Register event handler to be called on warnings and errors:
NLog.LogManager.Setup().SetupExtensions(intern=>intern.SetMinimumLogLevel(LogLevel.Warn).AddLogSubscription((sender,evt)=>ReportEvent(evt.Message)));
Override the default object reflection forSystem.Net.WebException
:
NLog.LogManager.Setup().SetupSerialization(s=>s.RegisterObjectTransformation<System.Net.WebException>(ex=>new{Type=ex.GetType().ToString(),Message=ex.Message,StackTrace=ex.StackTrace,Source=ex.Source,InnerException=ex.InnerException,Status=ex.Status,Response=ex.Response.ToString(),// Call your custom method to render stream as string}));
Skip default object reflection forReadOnlyMemory<byte>
:
NLog.LogManager.Setup().SetupSerialization(s=>s.RegisterObjectTransformation<ReadOnlyMemory<byte>>(obj=>obj.Length));
-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