Daniel
Daniel

Reputation: 47

Custom Serilog sink dont work using AppSettings

I am trying to setup a custom sink for Serilog and cant make it work..

I am using the Logger like:

Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger();

It does work if i do it like this:

//Log.Logger = new LoggerConfiguration().WriteTo.LogSenderSink().CreateLogger();

So its only when using AppSettings it wont trigger correctly.

I have two classes, one for the sink and one for the extension.

The sink:

using System;
using Serilog.Core;
using Serilog.Events;

namespace Serilog.Sinks.LogSender
{
    public class LogSenderSink : ILogEventSink, IDisposable
    {
        private readonly IFormatProvider _formatProvider;

        public LogSenderSink(IFormatProvider formatProvider, LogEventLevel restrictedToMinimumLevel)
        {
            _formatProvider = formatProvider;
        }

        public void Dispose()
        {

        }

        public void Emit(LogEvent logEvent)
        {
            var message = logEvent.RenderMessage(_formatProvider);
            Console.WriteLine(DateTimeOffset.Now.ToString() + " " + message);

            try
            {
                System.IO.File.WriteAllText(@"C:\Logs\test.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            }
            catch { }
        }
    }
}

And the extension method:

using Serilog.Configuration;
using Serilog.Events;
using Serilog.Sinks.LogSender;
using System;

namespace Serilog
{
    public static class LogSinkExtensions
    {
        public static LoggerConfiguration LogSenderSink(
                this LoggerSinkConfiguration loggerConfiguration,
                LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
                  IFormatProvider formatProvider = null)
        {
            return loggerConfiguration.Sink(new LogSenderSink(formatProvider, restrictedToMinimumLevel));
        }
    }
}

My App.config in the project where i test from looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <appSettings>
    <add key="serilog:minimum-level" value="Verbose"/>

    <add key="serilog:using:LogSender" value="Serilog.Sinks.LogSender" />
  </appSettings>
</configuration>

What am i missing here??

I am calling the log like this:

Log.Information("Debug was just sent");

And nothing happens at all...

Thanks for your time!

Upvotes: 2

Views: 1662

Answers (1)

FantasticFiasco
FantasticFiasco

Reputation: 1193

Are you not missing the serilog:write-to:LogSenderSink setting in your configuration file?

Upvotes: 4

Related Questions