FallbackGroup Target A compound target that provides fallback-on-error functionality. | Assembly | Class | .NET Framework | .NET CF | Mono on Windows | Mono on Unix |
|---|
| 1.0 | 1.1 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 |
|---|
| NLog.dll | NLog.Targets.Compound.FallbackTarget |  |  |  |  |  |  |  |  |  |
Parameters (blue fields are required):| Name | Type | Description |
|---|
| name | string | | | returnToFirstOnSuccess | boolean | | Whether to return to the first target after any successful write. |
|
Example:This example causes the messages to be written to server1, and if it fails, messages go to server2. To set up the target in the configuration file, use the following syntax: <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file" xsi:type="FallbackGroup" returnToFirstOnSuccess="false">
<target xsi:type="File" fileName="\\server1\share\file1.txt"/>
<target xsi:type="File" fileName="\\server2\share\file1.txt"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file"/>
</rules>
</nlog>
The above examples assume just one target and a single rule. See below for a programmatic configuration that's equivalent to the above config file: using System;
using NLog;
using NLog.Targets;
using NLog.Targets.Compound;
using System.Diagnostics;
class Example
{
static void Main(string[] args)
{
FileTarget file1 = new FileTarget();
file1.FileName = "\\\\server1\\share\\file1.txt";
FileTarget file2 = new FileTarget();
file2.FileName = "\\\\server2\\share\\file1.txt";
// write to server1, if it fails switch to server2
FallbackTarget target = new FallbackTarget();
target.ReturnToFirstOnSuccess = false;
target.Targets.Add(file1);
target.Targets.Add(file2);
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
}
}
Back to the target list. |