Nlog: How to register async target programmatically?

Created on 29 Apr 2015  路  6Comments  路  Source: NLog/NLog

I'm trying to register a custom _async_ target and associated rule, programmatically.

When do I reference the target and when do I reference the async wrapper?

``` c#
var target = new MyCustomTarget();
var wrapper = new AsyncTargetWrapper(target, 5000, AsyncTargetWrapperOverflowAction.Discard);

var rule = new LoggingRule("*", LogLevel.Trace, target); // use "target" or "wrapper" here?

LogManager.Configuration.AddTarget("foo", wrapper); // use "target" or "wrapper" here?
LogManager.Configuration.LoggingRules.Add(rule);

LogManager.ReconfigExistingLoggers();
```

question

Most helpful comment

You code example should work. So, addTarget with the Wrapper and addRule with the target.

All 6 comments

You code example should work. So, addTarget with the Wrapper and addRule with the target.

@migig Have your question been answered?

@Xharze @304NotModified What is the implication of addRule with wrapper instead of target?

So use wrapper for both addTarget and addRule.

I ask because it works both ways, but I'm worried it will lead to problems?

You should use the wrapper. The reason is that the asynchronous features are implemented in the wrapper not the target. If you pass in the target, you'll effectively bypass the asynchronous features of the wrapper. Both AddRule and AddTarget must use the wrapper.
The reason the rule must use the wrapper is that, when a logevent matches this rule it should be written to the wrapper.
The reason the target must use the wrapper is basically the same.

@Xharze Thanks that makes sense! :smile:

when you are instantiating rule you should pass wrapper instead of target to LoggingRule constructor.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

linmasaki picture linmasaki  路  3Comments

FaMouZx3 picture FaMouZx3  路  3Comments

vasumsit picture vasumsit  路  3Comments

Jerefeny picture Jerefeny  路  3Comments

ErcinDedeoglu picture ErcinDedeoglu  路  3Comments