Mags
Mags

Reputation: 57

How to setup Serilog with AzureEventHub

I want to use Serilog with the sink AzureEventHub, but I can't find any documentation on how to set it up.

I am running an ASP.NET MVC Core project.

Anyone got an idea or can give a clue to the solution?

Upvotes: 1

Views: 2646

Answers (1)

C. Augusto Proiete
C. Augusto Proiete

Reputation: 27878

It would be great to have more documentation on that sink, for sure, but overall sinks configurations are pretty standard, as most sinks usually expose extension methods on Serilog's LoggerSinkConfiguration class, which together with intellisense in Visual Studio, can give you everything you need from the point of view of configuration.

Just make sure you install Serilog and the Serilog.Sinks.AzureEventHub sink.

  <ItemGroup>
    <PackageReference Include="Serilog" Version="2.8.0" />
    <PackageReference Include="Serilog.Sinks.AzureEventHub" Version="4.1.0" />
  </ItemGroup>

Then at the beginning of your app, create an EventHubClient that you can pass through to WriteTo.AzureEventHub... You just need a valid connection string.

using Microsoft.Azure.EventHubs;
using Serilog;

namespace SerilogAndAzureEventHub
{
    class Program
    {
        static void Main(string[] args)
        {
            EventHubClient eventHubClient =
                EventHubClient.CreateFromConnectionString(
                    "Endpoint=sb://a;SharedAccessKeyName=b;SharedAccessKey=c;EntityPath=d");

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Verbose()
                .WriteTo.AzureEventHub(eventHubClient)
                .CreateLogger();
        }
    }
}

Upvotes: 2

Related Questions