By   September 7, 2014

Event Source API was introduced in .net framework 4.5 to support semantic logs. It allows an application to author events. These events are generated as ETW events which can be consumed in-proc or out-proc. For a general in-proc consumption, the API has introduced EventSourceListener type.

Semantic Logging Application Block (SLAB) has developed on top of EventSource API with more focus on event’s consumption. It has introduced sinks for consumption which are utilized by ETW event listener in the application block. These events are destination specific. The Pattern & Practices (p&p) team, at Microsoft, has also provided some example sinks. They are for destinations including console, text file (including rolling files), sql server and Windows Azure Table Storage.

The API also allows custom sinks. We can create these sinks for our custom sinks and plug it with Semantic Logging Service for out-proc consumption. In this post, we are going to create a sink for Amazon Simple Storage Service i.e. S3. We would assume a text file in a specified s3 bucket. During instantiation we would be reading contents of this file. We would continue to add on this content. When the service shuts down, we should be uploading the new contents to the same file. Isn’t that easy?

s3sink

If you don’t already have AWS SDK for .net yet, download and install it from here [http://aws.amazon.com/sdk-for-net/]. It provides the necessary project templates to create S3 C# project used in this post. Here we are using Empty Project template for Amazon S3.

AmazonS3Sink

The extension opens up a dialog next asking about user credentials. Here you can add the credentials for your AWS account. It should be adding the following keys in application configuration file. You can delete them from here as the service wouldn’t be using the configuration from the assembly.

Here we are adding the assembly name and default namespace for the types in the assembly. Let’s add a new folder Sinks for holding the sink type. You can verify that the template has already added the required assembly for AWSSDK.

updateproject

Now we need to add the nuget package for Semantic Logging Application Block. Make sure that you use the nuget package with the same version used by the Semantic Logging Service used in your environment. This is to avoid the assembly conflicts.

packageinstall

Now we can add the sink definition to the project. Here we are adding it to the Sink folder. As it is apparent, all sinks must inherit from IObserver<EventEntry>. Here we are picking up the bucket and file names from the app config. In this simple implementation we are reading the contents of the file. We keep appending to the contents. When the service shuts down, we are uploading the updated contents to the same file in S3 bucket. You would definitely have a little more details required to be logged in your environment.

Now build the assembly. We can copy the assembly to the semantic logging service folder. Please don’t forget to copy AWSSDK.dll assembly as well.

copyDlls

We also need to update the exe config for the semantic logging service (SemanticLogging-svc.exe.config) with the following app settings:

Now we just need to update SemanticLogging-svc.xml and use the above sink. Here we can simply use customSink element.

Get the code from GitHub

GithubAmazonS3