using NLog;
using NLog.Config;
using NLog.Targets;
namespace MyNamespace
{
[Target("MyFirst")]
public sealed class MyFirstTarget: TargetWithLayout //or inherit from Target
{
public MyFirstTarget()
{
//set defaults
this.Host = "localhost";
}
[RequiredParameter]
public string Host { get; set; }
protected override void Write(LogEventInfo logEvent)
{
string logMessage = this.Layout.Render(logEvent);
//TODO write to target
}
}
}
Register it under the name MyFirst
- as soon as possible - e.g. in main()
, application_start()
.
ConfigurationItemFactory
.Default
.Targets
.RegisterDefinition("MyFirst", typeof(MyNamespace.MyFirstTarget));
Usage:
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="target1" type="MyFirst" Host="somehost.com" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="target1" />
</rules>
</nlog>