OUT-OF-PROCESS Semantic Logging V2 with Elasticsearch

This article shows how to use Microsoft patterns & practices OUT-OF-PROCESS Semantic Logging Version 2 with an Elasticsearch sink. Version 2 has breaking changes compared to version 1.1 for the Elasticsearch sink; this has been removed from the standard SLAB NuGet package… Due to this, a custom sink configuration is required and also some copy paste deployment in the windows service.

Step 1: Install Elasticsearch

When installed, you can check that it is running by browsing http://localhost:9200/ (default installation)

Step 2: Install EnterpriseLibrary Semantic Logging as a service

Download EnterpriseLibrary.SemanticLogging.Service from NuGet (at present version 2.0.1406.1), extract and copy the files to some folder on your PC and install the service using the install-packages.ps1 file in powershell.

Download EnterpriseLibrary.SemanticLogging.Elasticsearch from NuGet (at present version 2.2.5) and copy the files to the EnterpriseLibrary.SemanticLogging.Service/tools directory.

EL_SLAB_Elasticsearch_01

Step 3: Add the Elasticsearch custom sink to the XML configuration file.

Open the SemanticLogging-svc.xml file and add a custom sink like shown below.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="http://schemas.microsoft.com/practices/2013/entlib/semanticlogging/etw"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://schemas.microsoft.com/practices/2013/entlib/semanticlogging/etw SemanticLogging-svc.xsd">
  
  <traceEventService/>

  <sinks>
    <customSink name="MyElasticsearchSink" type ="FullScale180.SemanticLogging.Sinks.ElasticsearchSink, FullScale180.SemanticLogging.Elasticsearch">
      <sources>
        <eventSource name="TestEvents" level="LogAlways" />
      </sources>
      <parameters>
		<parameter name="instanceName" type="System.String" value="damienbod" />
		<parameter name="connectionString" type="System.String" value="http://localhost:9200" />
		<parameter name="index" type="System.String" value="outofprocessslab" />
		<parameter name="type" type="System.String" value="webapitracing" />
		<parameter name="flattenPayload" type="System.Boolean" value="true" />
		<parameter name="bufferInterval" type="System.TimeSpan" value="00:00:30" />		
		<parameter name="bufferingCount" type="System.Int32" value="10" />		
		<parameter name="maxBufferSize" type="System.Int32" value="500" />	
		<parameter name="onCompletedTimeout" type="System.TimeSpan" value="00:05:00" /> 
		<parameter name="jsonGlobalContextExtension" type="System.String" value="null" />	
      </parameters>
    </customSink>
  </sinks>

</configuration>

Step 4: Install and start the service

Open the command line as admin and execute the following:

SemanticLogging-svc.exe -install

Now the service should be running and ready to log. This can be checked in the services. (services.msc)

Step 5: Create a test application

Create a simple console application and add the package

“EnterpriseLibrary.SemanticLogging”: “2.0.1406.1”

Add an EventSource class which matches the configuration in the SLAB service.

[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);
	}
}

Add some log messages.

using System.Diagnostics.Tracing;
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging;

namespace Slab.Elasticsearch.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            OutOfProcessLogging();
            System.Console.ReadLine();
        }
        private static void OutOfProcessLogging()
        {
            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");
        }
    }
}

Run the application and you should see logs in Elasticsearch:

EL_SLAB_Elasticsearch_02

Links:

https://github.com/fullscale180/slab-sinks

https://github.com/mspnp/semantic-logging

https://www.nuget.org/packages/EnterpriseLibrary.SemanticLogging.Service/

https://www.nuget.org/packages/EnterpriseLibrary.SemanticLogging.Elasticsearch/

One comment

  1. Tapas Pal · · Reply

    semantic logging source code is pushed in Microsoft Archive folder in Git. These Nuget packages are still working ?
    https://github.com/microsoftarchive/semantic-logging

    While registering the service SemanticLogging-svc.exe -install I’m getting following errro

    System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

    After Nuget package EnterpriseLibrary.SemanticLogging.Elasticsearch installation all files shown in the screen shot are not present.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: