Connecting the Enterprise with Azure Event Grid and Serilog
In all companies, large and small, there is no One app to rule them all. The reality is that there are often dozens, sometimes hundreds, of apps/services/tools/etc that we use to help ourselves, or to provide services to our clients. So how do you get all of your services and tools working in concert to meet your goals, objectives, and strategies? How do you take that "legacy" service that a client has been using for 10 years and make it play well with your shiny new micro service? The answer you want to give is "re-write it all!" or let's change our tool to "X", however more often than not, it's expensive or impractical for the business. In this post, I'll focus on leveraging Azure's new Event Grid service to help answer some of those questions.
Event Grid is designed to be a light weight, low dependency, and large scale reliable messaging solution for reactive or event driven workflows. What makes it different from other reliable message offerings in Azure are it's integration points. You send events as a JSON payload through a simple HTTP Post which in turn can be received by any number of registered HTTPS endpoints. The filtering is also straight forward, each subscriber can filter on the event subject and/or the event type strings that come in with every event payload. HTTP JSON in, reliable and filtered HTTP JSON out, doesn't get much simpler than that. More on Azure Event Grid.
Now that I know how my events are going to get around, the question then becomes, how can I send them across a wide variety of apps in a simple and uniform way? Typically, you embed the code directly, you build your own library, or you leverage/extend an existing library. I explored most options while prototyping, but settled on Serilog as my solution. Serilog is a successful and highly extensible open source logging framework for .NET. which focuses specifically on supporting structured data in log events. The primary way to extend Serilog is through Sinks, which are custom coded destinations for your log events. Many of the sinks come directly from and are maintained by the github community, which continues to thrive. For those reasons and the fact that I just like the project and contribute to it often, I decided to build a sink for Event Grid. The Serilog Event Grid sink project.
From your .NET app, install and set up your logger
Install the main sink from nuget along with any enrichers you may want included with your event.
Install-Package Serilog.Sinks.EventGrid # optional enrichers Install-Package Serilog.Enrichers.Environment Install-Package Serilog.Enrichers.Process
There are quite a few options for setting up the logger with the Event Grid sink. Though in this example, I'm defining the Event Grid topic configuration in my
app.config, and setting up my event payload to always contain my transformed
UserContext along with the machine & process information.
Add your log event code
The resulting event through your registered subscriber endpoint
That's up to you. You are now emitting rich custom events from your new or existing app with just a few lines of code. Custom client billing, real-time notifications in slack, extended processing in a serverless function...all sounds pretty good to me :)