记录仪允许你通过一个配置文件达到多个目标。 Don tabes to set起来ignoreFailures,以确保任何装置/单位故障都被忽视:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" />
<target name="db" xsi:type="Database" />
</targets>
<rules>
<logger name="*" levels="Info" writeTo="logfile,db" />
</rules>
</nlog>
See database Target in Nlog documentation for more information
<><>Edit>: 为了在法典中做到这一点,你还可以设定一个习俗目标:
using NLog;
using NLog.Targets;
namespace MyNamespace
{
[Target("MyFirst")]
public sealed class MyFirstTarget: TargetWithLayout
{
public MyFirstTarget()
{
this.Host = "localhost";
}
[Required]
public string Host { get; set; }
protected override void Write(LogEventInfo logEvent)
{
string logMessage = this.Layout.Render(logEvent);
SendTheMessageToRemoteHost(this.Host, logMessage);
}
private void SendTheMessageToRemoteHost(string host, string message)
{
// Detect your database state here or do something else with the error.
}
}
}
并使用:
<nlog>
<extensions>
<add assembly="MyAssembly"/>
</extensions>
<targets>
<target name="a1" type="MyFirst" host="localhost"/>
</targets>
<rules>
<logger name="*" minLevel="Info" appendTo="a1"/>
</rules>
</nlog>
See this page for more information.