Click or drag to resize

AsyncTaskTarget Class

Abstract Target with async Task support
Inheritance Hierarchy
SystemObject
  NLog.TargetsTarget
    NLog.TargetsTargetWithLayout
      NLog.TargetsTargetWithContext
        NLog.TargetsAsyncTaskTarget

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 5.3.4+73d83d3161d31288ca5c554cc50d27b6bed5f28b
Syntax
public abstract class AsyncTaskTarget : TargetWithContext

The AsyncTaskTarget type exposes the following members.

Constructors
  NameDescription
Protected methodAsyncTaskTarget
Constructor
Top
Properties
  NameDescription
Public propertyBatchSize
Gets or sets the number of log events that should be processed in a batch by the lazy writer thread.
Public propertyContextProperties
Gets the array of custom attributes to be passed into the logevent context
(Inherited from TargetWithContext.)
Public propertyExcludeProperties
List of property names to exclude when IncludeEventProperties is true
(Inherited from TargetWithContext.)
Public propertyForceLockingQueue
Gets or sets whether to use the locking queue, instead of a lock-free concurrent queue The locking queue is less concurrent when many logger threads, but reduces memory allocation
Public propertyIncludeCallSite
Gets or sets a value indicating whether to include call site (class and method name) in the LogEventInfo
(Inherited from TargetWithContext.)
Public propertyIncludeCallSiteStackTrace
Gets or sets a value indicating whether to include source info (file name and line number) in the LogEventInfo
(Inherited from TargetWithContext.)
Public propertyIncludeEventProperties
Gets or sets the option to include all properties from the log events
(Inherited from TargetWithContext.)
Public propertyIncludeGdc
Gets or sets a value indicating whether to include contents of the GlobalDiagnosticsContext dictionary
(Inherited from TargetWithContext.)
Public propertyIncludeScopeNested
Gets or sets whether to include the contents of the ScopeContext nested-state-stack.
(Inherited from TargetWithContext.)
Public propertyIncludeScopeProperties
Gets or sets whether to include the contents of the ScopeContext properties-dictionary.
(Inherited from TargetWithContext.)
Protected propertyIsInitialized
Gets a value indicating whether the target has been initialized.
(Inherited from Target.)
Public propertyLayout
Gets or sets the layout used to format log messages.
(Inherited from TargetWithContext.)
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 propertyOverflowAction
Gets or sets the action to be taken when the lazy writer thread request queue count exceeds the set limit.
Public propertyQueueLimit
Gets or sets the limit on the number of requests in the lazy writer thread request queue.
Public propertyRetryCount
How many attempts to retry the same Task, before it is aborted
Public propertyRetryDelayMilliseconds
How many milliseconds to wait before next retry (will double with each retry)
Protected propertySyncRoot
Gets the object which can be used to synchronize asynchronous operations that must rely on the .
(Inherited from Target.)
Public propertyTaskDelayMilliseconds
How many milliseconds to delay the actual write operation to optimize for batching
Protected propertyTaskScheduler
Task Scheduler used for processing async Tasks
Public propertyTaskTimeoutSeconds
How many seconds a Task is allowed to run before it is cancelled.
Top
Methods
  NameDescription
Protected methodCaptureContextGdc
Takes snapshot of GlobalDiagnosticsContext for the LogEventInfo
(Inherited from TargetWithContext.)
Protected methodCaptureScopeContextNested
Takes snapshot of nested states from ScopeContext for the LogEventInfo
(Inherited from TargetWithContext.)
Protected methodCaptureScopeContextProperties
Takes snapshot of ScopeContext dictionary for the LogEventInfo
(Inherited from TargetWithContext.)
Protected methodCloseTarget
Closes Target by updating CancellationToken
(Overrides TargetCloseTarget.)
Public methodDispose
Closes the target.
(Inherited from Target.)
Protected methodDispose(Boolean)
Releases any managed resources
(Overrides TargetDispose(Boolean).)
Public methodFlush
Flush any pending log messages (in case of asynchronous targets).
(Inherited from Target.)
Protected methodFlushAsync
Schedules notification of when all messages has been written
(Overrides TargetFlushAsync(AsyncContinuation).)
Protected methodGenerateUniqueItemName
Generates a new unique name, when duplicate names are detected
(Inherited from TargetWithContext.)
Protected methodGetAllProperties(LogEventInfo)
Creates combined dictionary of all configured properties for logEvent
(Inherited from TargetWithContext.)
Protected methodGetAllProperties(LogEventInfo, IDictionaryString, Object)
Creates combined dictionary of all configured properties for logEvent
(Inherited from TargetWithContext.)
Protected methodGetContextProperties(LogEventInfo)
Checks if any context properties, and if any returns them as a single dictionary
(Inherited from TargetWithContext.)
Protected methodGetContextProperties(LogEventInfo, IDictionaryString, Object)
Checks if any context properties, and if any returns them as a single dictionary
(Inherited from TargetWithContext.)
Protected methodGetScopeContextNested
Returns the captured snapshot of nested states from ScopeContext for the LogEventInfo
(Inherited from TargetWithContext.)
Protected methodGetScopeContextProperties
Returns the captured snapshot of ScopeContext dictionary for the LogEventInfo
(Inherited from TargetWithContext.)
Protected methodInitializeTarget
Initializes the target before writing starts
(Overrides TargetInitializeTarget.)
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.)
Protected methodRetryFailedAsyncTask
Handle cleanup after failed write operation
Protected methodSerializeItemValue
Take snapshot of a single object value
(Inherited from TargetWithContext.)
Protected methodSerializeScopeContextNestedState
Take snapshot of a single object value from ScopeContext nested states
(Inherited from TargetWithContext.)
Protected methodSerializeScopeContextProperty
Take snapshot of a single object value from ScopeContext dictionary
(Inherited from TargetWithContext.)
Protected methodShouldIncludeProperties
Check if logevent has properties (or context properties)
(Inherited from TargetWithContext.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Target.)
Protected methodWrite(AsyncLogEventInfo)
Writes async log event to the log target.
(Overrides TargetWrite(AsyncLogEventInfo).)
Protected methodWrite(IListAsyncLogEventInfo) (Overrides TargetWrite(IListAsyncLogEventInfo).)
Protected methodWrite(LogEventInfo)
Block for override. Instead override WriteAsyncTask(LogEventInfo, CancellationToken)
(Overrides TargetWrite(LogEventInfo).)
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 methodWriteAsyncTask(IListLogEventInfo, CancellationToken)
Override this to provide async task for writing a batch of logevents.
Protected methodCode exampleWriteAsyncTask(LogEventInfo, CancellationToken)
Override this to provide async task for writing a single logevent.
Examples
Example of how to override this method, and call custom async method
1protected override Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken token)
2{
3   return CustomWriteAsync(logEvent, token);
4}
5
6private async Task CustomWriteAsync(LogEventInfo logEvent, CancellationToken token)
7{
8    await MyLogMethodAsync(logEvent, token).ConfigureAwait(false);
9}
Protected methodWriteAsyncThreadSafe(AsyncLogEventInfo)
Write to queue without locking SyncRoot
(Overrides TargetWriteAsyncThreadSafe(AsyncLogEventInfo).)
Protected methodWriteAsyncThreadSafe(IListAsyncLogEventInfo) (Overrides TargetWriteAsyncThreadSafe(IListAsyncLogEventInfo).)
Protected methodWriteFailedNotInitialized
LogEvent is written to target, but target failed to successfully initialize Enqueue logevent for later processing when target failed to initialize because of unresolved service dependency.
(Overrides TargetWriteFailedNotInitialized(AsyncLogEventInfo, Exception).)
Top
Remarks
Examples
 1[Target("MyFirst")]
 2public sealed class MyFirstTarget : AsyncTaskTarget
 3{
 4   public MyFirstTarget()
 5   {
 6       this.Host = "localhost";
 7   }
 8
 9   [RequiredParameter]
10   public Layout Host { get; set; }
11
12   protected override Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken token)
13   {
14       string logMessage = this.RenderLogEvent(this.Layout, logEvent);
15       string hostName = this.RenderLogEvent(this.Host, logEvent);
16       return SendTheMessageToRemoteHost(hostName, logMessage);
17   }
18
19   private async Task SendTheMessageToRemoteHost(string hostName, string message)
20   {
21       // To be implemented
22   }
23}
See Also