Click or drag to resize

FallbackGroupTarget Class

Provides fallback-on-error.
Inheritance Hierarchy
SystemObject
  NLog.TargetsTarget
    NLog.Targets.WrappersCompoundTargetBase
      NLog.Targets.WrappersFallbackGroupTarget

Namespace:  NLog.Targets.Wrappers
Assembly:  NLog (in NLog.dll) Version: 5.3.4+73d83d3161d31288ca5c554cc50d27b6bed5f28b
Syntax
public class FallbackGroupTarget : CompoundTargetBase

The FallbackGroupTarget type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyEnableBatchWrite
Gets or sets whether to enable batching, but fallback will be handled individually
Protected propertyIsInitialized
Gets a value indicating whether the target has been initialized.
(Inherited from Target.)
Protected propertyLoggingConfiguration
Gets the logging configuration this target is part of.
(Inherited from Target.)
Public propertyName
Gets or sets the name of the target.
(Inherited from Target.)
Public propertyReturnToFirstOnSuccess
Gets or sets a value indicating whether to return to the first target after any successful write.
Protected propertySyncRoot
Gets the object which can be used to synchronize asynchronous operations that must rely on the .
(Inherited from Target.)
Public propertyTargets
Gets the collection of targets managed by this compound target.
(Inherited from CompoundTargetBase.)
Top
Methods
  NameDescription
Protected methodCloseTarget
Closes the target to release any initialized resources
(Inherited from Target.)
Public methodDispose
Closes the target.
(Inherited from Target.)
Protected methodDispose(Boolean)
Releases unmanaged and - optionally - managed resources.
(Inherited from Target.)
Public methodFlush
Flush any pending log messages (in case of asynchronous targets).
(Inherited from Target.)
Protected methodFlushAsync
Flush any pending log messages for all wrapped targets.
(Inherited from CompoundTargetBase.)
Protected methodInitializeTarget
Initializes the target before writing starts
(Inherited from Target.)
Public methodPrecalculateVolatileLayouts
Calls the Precalculate(LogEventInfo) on each volatile layout used by this target. This method won't prerender if all layouts in this target are thread-agnostic.
(Inherited from Target.)
Protected methodRenderLogEvent(Layout, LogEventInfo)
Renders the logevent into a string-result using the provided layout
(Inherited from Target.)
Protected methodRenderLogEventT(LayoutT, LogEventInfo, T)
Renders the logevent into a result-value by using the provided layout
(Inherited from Target.)
Protected methodResolveServiceT
Resolve from DI ServiceRepository
(Inherited from Target.)
Public methodToString
Returns a string that represents the current object.
(Inherited from CompoundTargetBase.)
Protected methodWrite(AsyncLogEventInfo)
Forwards the log event to the sub-targets until one of them succeeds.
(Overrides TargetWrite(AsyncLogEventInfo).)
Protected methodWrite(IListAsyncLogEventInfo)
Writes an array of logging events to the log target. By default it iterates on all events and passes them to "Write" method. Inheriting classes can use this method to optimize batch writes.
(Inherited from Target.)
Protected methodWrite(LogEventInfo)
Writes logging event to the target destination
(Inherited from CompoundTargetBase.)
Public methodWriteAsyncLogEvent
Writes the log to the target.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(AsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(IListAsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Protected methodWriteAsyncThreadSafe(AsyncLogEventInfo)
Writes a log event to the log target, in a thread safe manner. Any override of this method has to provide their own synchronization mechanism. !WARNING! Custom targets should only override this method if able to provide their own synchronization mechanism. Layout-objects are not guaranteed to be thread-safe, so using them without a SyncRoot-object can be dangerous.
(Overrides TargetWriteAsyncThreadSafe(AsyncLogEventInfo).)
Protected methodWriteAsyncThreadSafe(IListAsyncLogEventInfo)
Forwards the log event to the sub-targets until one of them succeeds.
(Overrides TargetWriteAsyncThreadSafe(IListAsyncLogEventInfo).)
Protected methodWriteFailedNotInitialized
LogEvent is written to target, but target failed to successfully initialize
(Inherited from Target.)
Top
Remarks
Examples

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:

XML
 1<?xml version="1.0" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4    <targets>
 5        <target name="file" xsi:type="FallbackGroup" returnToFirstOnSuccess="false">
 6            <target xsi:type="File" fileName="\\server1\share\file1.txt" />
 7            <target xsi:type="File" fileName="\\server2\share\file1.txt" />
 8        </target>
 9    </targets>
10
11    <rules>
12        <logger name="*" minlevel="Debug" writeTo="file" />
13    </rules>
14</nlog>

To set up the log target programmatically use code like this:

C#
 1using System;
 2
 3using NLog;
 4using NLog.Targets;
 5using NLog.Targets.Compound;
 6using System.Diagnostics;
 7
 8class Example
 9{
10    static void Main(string[] args)
11    {
12        FileTarget file1 = new FileTarget();
13        file1.FileName = "\\\\server1\\share\\file1.txt";
14
15        FileTarget file2 = new FileTarget();
16        file2.FileName = "\\\\server2\\share\\file1.txt";
17
18
19        // write to server1, if it fails switch to server2
20        FallbackTarget target = new FallbackTarget();
21
22        target.ReturnToFirstOnSuccess = false;
23        target.Targets.Add(file1);
24        target.Targets.Add(file2);
25
26        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
27
28        Logger logger = LogManager.GetLogger("Example");
29        logger.Debug("log message");
30    }
31}
See Also