Send logs to Splunk with log4net UdpAppender
Last week I deployed a new Api hosted as an Azure WebApp, we wanted some reporting events to come out of it into our Splunk instance so we could keep an eye on whether it is working as expected. I started off by using the Splunk C# SDK as it looked nice and easy to add into our app.
A very trivial example of using the Splunk SDK would look like this:
|
|
The Splunk SDK should have worked, but since there was A LOT of traffic at different times throughout the day the vast majority of the HTTP requests to the Splunk API just timed out.
|
|
After I discovered that it constantly just timed out I started to look around for an alternative to using the Splunk REST Api endpoint.
I’ve always noticed that Splunk lets you send events data to it via TCP/UDP but have never had cause or reason to use it. However given that our API was hosted as an Azure Web App and logging to the file system wasn’t an option I thought this would be the perfect time to try it out. As it turns out I was not disappointed.
Setting up Splunk
The first thing you will need to do is configure your Splunk instance to listen out for events from a Udp port.
-
From the Splunk settings, select Data Inputs
-
Click “Add new” UDP input
-
This will bring up the Add Data wizard, enter the port you want Splunk to listen on (this will also need to go into your log4net config)
-
The next page “Input Settings” let you (optionally) specify various things about the data you are going to be sending. There are two particularly useful settings here, the sourcetype and index
-
Click Review and then Submit
Splunk is now listening on that UDP port.
Important Note: Make sure that all firewall ports between your producer and Splunk are open to allow UDP traffic on the specified port! Otherwise it will silently fail.
Introducing the log4net UdpAppender
The first step of getting this working from your application is adding the log4net NuGet package to your project, next up open your log4net configuration and add the following
|
|
Now from your c# code you use log4net as you normally would:
|
|
And you should start seeing your events in Splunk.
If you need to verify that log4net is sending events over UDP you can use Wireshark. Here I have set it up to capture packets from my wifi adapter and monitoring port 9977
🍪 I use Disqus for comments
Because Disqus requires cookies this site doesn't automatically load comments.
I don't mind about cookies - Show me the comments from now on (and set a cookie to remember my preference)