advapi
advapi

Reputation: 3907

Can't use my custom Sink with config

I'm tring to use my custom sink for serilog via app.config but with no luck. I've a class library with a TestSink class defined as

public class TestSink : ILogEventSink
{
    public TestSink()
    {

    }
    public void Emit(LogEvent logEvent)
    {

    }
}

In a console application I've referenced this assembly (called Test.dll) and added this

<add key="serilog:using" value="Test"/>

My program.cs is

 class Program
{
    static void Main(string[] args)
    {
        try
        {
            var log = new LoggerConfiguration()
                .ReadFrom.AppSettings().CreateLogger();

            Log.Logger = log;

            Log.Information("");
        }
        catch (Exception ex)
        {

            int t = 0;
        }
    }
}

What I got is "Configuration system failed to initialize"

enter image description here

What am I doing wrong? Thanks

Upvotes: 1

Views: 272

Answers (1)

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31832

You need to do two things here.

  1. The <add> element needs to be nested under <appSettings> (the immediate configuration error here)
  2. You need to provide an extension method alongside your custom sink so that WriteTo.Test() is available on the logger configuration

The config system works against the extension methods rather than class names so that the code and XML configuration APIs of Serilog are (forcibly) kept in sync.

Upvotes: 1

Related Questions