Register your custom component - NLog/NLog GitHub Wiki
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 is also required if wanting to perform application trimming (linker-tree-shake).
Note that the registrations must be done before setting LogManager.Configuration.
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)); //dynamic
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));
<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.