markmcdowell / NLog.Targets.ElasticSearch

NLog target for Elasticsearch
MIT License
176 stars 89 forks source link

Setting Tags/Properties #70

Closed sganz closed 6 years ago

sganz commented 6 years ago

Trying to to set the Elastic Search Tags (In Kibana) for the event being logged. Need to do this so can search and filter on a few specific tags. Trying the code in the lowest level of the logger right before sending it. The tags/properties are applied prior to sending the message as they can't be set when I configure the ElasticSearchTarget when I set up the logger.

I should add that I saw some of this as part of another Issue https://github.com/ReactiveMarkets/NLog.Targets.ElasticSearch/issues/30 but may need to do more in the config but not really sure if this is something that I should even try.

Snip of code in the method that ships out the log message

            // calling with the tags dictionary set with an element "abc"="def"

            _logger = LogManager.GetLogger("Logger");

            LogEventInfo theEvent = new LogEventInfo(LogLevel.Info, _logger.Name, "The Log Message");

            foreach (KeyValuePair<string, string> tag in tags)
            {
                theEvent.Properties[tag.Key] = tag.Value;
            }

            _logger.Info(theEvent);

Should see the regular tags with the addition of the "abc"="def"

_id : 0pboOmUBS7H8XvOnpoir
_index : logstash-2018.08.15#
_score : - 
_type : logevent
abc : def
app : myApp
level : Info
message : {The Log Message}

Hope this makes sense. Not even really sure this is the right way to do it, but trying to apply this as properties in NLog so it would be not be specific to the ElasticSearch target, but would work if it is.

NLog 4.5.6 and NLog.Target.ElasticSearch 5.0.0 being used.

Thanks for any help or direction!

Sandy

sganz commented 6 years ago

Looked at the code and the older issue a bunch more and finally realized I did not set the IncludeAllProperties in my config. After that, worked!

Closing this out.

Thanks again for your work on the package.