简体   繁体   中英

How can I log to Amazon ElasticSearch with NLog?

I am trying to get NLog working with Amazon ElasticSearch.

This is what I got at the moment:

// Step 1. Create configuration object 
var config = new LoggingConfiguration();

// Step 2. Create targets and add them to the configuration 
var awsTaget = new ElasticSearchTarget();
config.AddTarget("aws", awsTaget);

// Step 3. Set target properties 
awsTaget.Uri = "https://amazonendpoint.com";
awsTaget.Index = "myindex" + DateTime.Now.ToString("yyyy-MM-dd");
awsTaget.DocumentType = "logevent";
awsTaget.Layout = "${message}";

// Step 4. Define rules
var rule3 = new LoggingRule("*", LogLevel.Debug, awsTaget);
config.LoggingRules.Add(rule3);    

// Step 5. Activate the configuration
LogManager.Configuration = config;

// log
var _logger = LogManager.GetLogger("Example");
logger.Debug("debug log message");

I am using NLog and NLog.Targets.ElasticSearch. Is that the right packages?

You need to put a wrapper around ElasticSearchTarget because of the following bug:

https://github.com/ReactiveMarkets/NLog.Targets.ElasticSearch/issues/53

var awsTaget = new ElasticSearchTarget();
var awsTargetAsync = new AsyncTargetWrapper(awsTaget) { OverflowAction=AsyncTargetWrapperOverflowAction.Block, BatchSize=10, TimeToSleepBetweenBatches = 0 };

// Step 4. Define rules
var rule3 = new LoggingRule("*", LogLevel.Debug, awsTargetAsync);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM