Semanic Logging with Elasticsearch using SemanticLogging.Elasticsearch NuGet package

The Enterprise Library team have released a new Semantic Logging Application Block which now includes an Elasticsearch sink. I have also created a Slab.Elasticsearch NuGet package but do not see the point in supporting this anymore, now that it is part of the SLAB itself. One of the major features which my package was missing, was the support for bulk or buffered inserts. This is included in the new package. This BufferedEventPublisher can also be used for any custom sinks which makes the Semantic.Logging very easy to extend now.

How to use the Elasticsearch sink:

Create a console application and install the following packages:
EnterpriseLibrary.SemanticLogging version=1.1.1403.1
EnterpriseLibrary.SemanticLogging.Elasticsearch version=1.1.1403.1

Required just for Out-of-Process logging.
EnterpriseLibrary.SemanticLogging.Service version=1.1.1403.2


Add an EventSource class to your project:

using System.Diagnostics.Tracing;

namespace SemanticLogging.Elasticsearch.Console
    [EventSource(Name = "TestEvents")]
    public class TestEvents : EventSource
        public static readonly TestEvents Log = new TestEvents();

        [Event(1, Message = "TestEvents Critical: {0}", Level = EventLevel.Critical)]
        public void Critical(string message)
            if (IsEnabled()) WriteEvent(1, message);

        [Event(2, Message = "TestEvents Error {0}", Level = EventLevel.Error)]
        public void Error(string message)
            if (IsEnabled()) WriteEvent(2, message);

        [Event(3, Message = "TestEvents Informational {0}", Level = EventLevel.Informational)]
        public void Informational(string message)
            if (IsEnabled()) WriteEvent(3, message);

        [Event(4, Message = "TestEvents LogAlways {0}", Level = EventLevel.LogAlways)]
        public void LogAlways(string message)
            if (IsEnabled()) WriteEvent(4, message);

        [Event(5, Message = "TestEvents Verbose {0}", Level = EventLevel.Verbose)]
        public void Verbose(string message)
            if (IsEnabled()) WriteEvent(5, message);

        [Event(6, Message = "TestEvents Warning {0}", Level = EventLevel.Warning)]
        public void Warning(string message)
            if (IsEnabled()) WriteEvent(6, message);

The following code demonstates how to create in-process logging for your application. The code adds a listener with logs to the Elasticsearch engine found at http://localhost:9200. If your server is running, logs will be written to the myindex-date index in Elasticsearch. The problem with this solution, is that per default the buffer is set to a 1000 records and added with a bulk insert. When the application stops, logs will/can be lost. The buffer size can be reduced to 1 and the delay to one second. This causes more traffic, but less logs will be lost. A flush function would be nice for in-process logging.

var listener = new ObservableEventListener();
listener.EnableEvents(TestEvents.Log, EventLevel.LogAlways, Keywords.All);

listener.LogToElasticsearch("SLABEL","http://localhost:9200", "myindex", "mytype", bufferingCount:2);

TestEvents.Log.Critical("Hello world In-Process Critical");
TestEvents.Log.Error("Hello world In-Process Error");
TestEvents.Log.Informational("Hello world In-Process Informational");

Another solution would be to use OUT-OF-PROCESS logging. First remove the in-process logging and just add 3 log events.

 TestEvents.Log.Critical("Hello world Out-Of-Process Critical");
TestEvents.Log.Error("Hello world Out-Of-Process Error");
TestEvents.Log.Informational("Hello world Out-Of-Process Informational");

Now install the service which was installed using NuGet. NOTE: it is important that this is installed close to the root directory, otherwise it will not run correctly due to too many characters in the path. When he service is installed, add the following configuration to the SemanticLogging-svc.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns=""
               xsi:schemaLocation=" SemanticLogging-svc.xsd">
    <elasticsearchSink instanceName="slabtest" connectionString="http://localhost:9200" name="out" index="outofprocessslab" type="test">
        <eventSource name="TestEvents" level="LogAlways"/>


When the application is started and stopped, all logs will be written to the Elasticsearch search and analytics engine.


The new Elasticsearch sink is a great feature and now you have the power of high performance logging with high performance searching. What could be better.


One comment

  1. Does the listener.LogToElasticsearch support basic auth (login/password)?

Leave a Reply to AndMed Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: