Skip to content

Register your custom component

Rolf Kristensen edited this page Aug 29, 2023 · 22 revisions

NLog extensions must be registered before they can be used in NLog configuration. This can be done in different ways:

Register the NLog extensions from code

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 is also required if wanting to perform application trimming (linker-tree-shake).

Include assembly-name in type for NLog extensions

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> 

Register NLog extensions from assembly in NLog config

Include your assembly with the <extensions> syntax. Use the assembly name (not the filename)

<nlog> 
  <extensions> 
    <add assembly="MyAssemblyName"/> 
  </extensions> 

Register NLog extensions from assembly at runtime

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.

Register NLog extension from code (Old style)

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)); //dynamic

Register NLog extension from code (Older style)

Now 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));

Automatic loading NLog extensions from assemblies with NLog prefix

⚠️ NLog 5.0 no longer automatically scans all assemblies to register NLog extensions. This avoids an overhead during startup, and because it doesn't work with <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.

Clone this wiki locally