建立自定義目標
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
}
}
}
以 MyFirst
的名義註冊 - 儘快 - 例如在 main()
,application_start()
。
ConfigurationItemFactory
.Default
.Targets
.RegisterDefinition("MyFirst", typeof(MyNamespace.MyFirstTarget));
用法:
<?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>