-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Register your custom component
NLog extensions must be registered before they can be used in NLog configuration. This can be done in different ways:
NLog 5.2 encourage registration from code of the NLog-extension-types, when you know up-front what will be needed.
NLog.LogManager.Setup().SetupExtensions(ext => {
ext.RegisterTarget<MyCustomTarget>();
ext.RegisterLayout<MyCustomLayout>();
ext.RegisterLayoutRenderer<MyCustomLayoutRenderer>();
});This will improve application startup-time, as one can skip assembly-loading and assembly-scanning for NLog-extension-types. It also ensures that build-trimming for the application will work (linker-tree-shake).
Note registrations must be performed before LogManager.Configuration is loaded, which happens automatically on calling LogManager.GetLogger("..") or LogManager.GetCurrentClassLogger()
NLog 5.0 introduces the ability to use fully qualified name when specifying the type for a config-item.
<nlog>
<targets>
<target name="mytarget" type="CustomTarget, MyAssemblyName" />
</targets> Include your assembly with the <extensions> syntax. Use the assembly name (not the filename)
<nlog>
<extensions>
<add assembly="MyAssemblyName"/>
</extensions> Provide the name of the assembly, which contains the NLog extension types:
NLog.LogManager.Setup().SetupExtensions(s => s.RegisterAssembly("MyAssemblyName"));Since NLog v5.2 this is no longer recommended. Instead one should explicit register the NLog-extension-types from code.
Introduced with NLog v4.4. Now marked as obsolete with NLog v5.2
//target
Target.Register<MyNamespace.MyFirstTarget>("MyFirst"); //generic
Target.Register("MyFirst", typeof(MyNamespace.MyFirstTarget)); //OR, dynamic
//layout renderer
LayoutRenderer.Register<MyNamespace.MyFirstLayoutRenderer>("MyFirst"); //generic
LayoutRenderer.Register("MyFirst", typeof(MyNamespace.MyFirstLayoutRenderer)); //dynamic
//layout
Layout.Register<MyNamespace.CsvLayout>("csv"); //generic
Layout.Register("csv", typeof(MyNamespace.CsvLayout)); //dynamicNow marked as obsolete with NLog v5.2
//target
ConfigurationItemFactory.Default.Targets
.RegisterDefinition("MyFirst", typeof(MyNamespace.MyFirstTarget));
//layout renderer
ConfigurationItemFactory.Default.LayoutRenderers
.RegisterDefinition("hello-world", typeof(MyNamespace.HelloWorldLayoutRenderer));
//layout
ConfigurationItemFactory.Default.Layouts
.RegisterDefinition("csv", typeof(MyNamespace.CsvLayout));
<packagereference>. To explicit enable the automatic scanning, then one can specify autoLoadAssemblies="true" in <nlog>.
Introduced in NLog 4.0, where it automatically scans all assemblies starting with "NLog", and attempt to register any available NLog extensions types.
Name your assembly "NLog*.dll", like “NLog.CustomTarget.dll”. When the assembly is in the same folder as nlog.dll, it will be autoloaded when the configuration changes.
- 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