0

I need to enable logging in reactiveui. I implemented the ILogger interface:

public class Logger : Splat.ILogger
{
    public LogLevel Level { get; private set; }

    public Logger(LogLevel level)
    {
        Level = level;
    }

    public void Write([Localizable(false)] string message, LogLevel logLevel)
    {
        if (logLevel >= Level)
        Serilog.Log.Error(message);
    }

    public void Write(Exception exception, [Localizable(false)] string message, LogLevel logLevel)
    {
    if (logLevel >= Level)
        Serilog.Log.Error(exception, message);
    }

    public void Write([Localizable(false)] string message, [Localizable(false)] Type type, LogLevel logLevel)
    {
    if (logLevel >= Level)
        Serilog.Log.Error(message);
    }

    public void Write(Exception exception, [Localizable(false)] string message, [Localizable(false)] Type type, LogLevel logLevel)
    {
    if (logLevel >= Level)
        Serilog.Log.Error(exception, message);
    }
}

and register it:

var logger = new Logging.Logger(LogLevel.Info) { };
Locator.CurrentMutable.RegisterConstant(logger, typeof(ILogger));

And trying to use it like this:

LogHost.Default.Error("TestTestTest");

this.WhenAnyValue(x => x.IsHierarchical).Log(this, "ClusterableDictionaryValueSelector IsHierarchical exception")
.BindTo(this, x => x.ViewModel.IsHierarchical).DisposeWith(disposables);

But I can't found the log file. And when I call LogHost.Default no method is called. What's wrong?

KevinLamb
  • 634
  • 8
  • 18
Igor. A
  • 73
  • 10

1 Answers1

3

We have a premade Serilog logger that is made for Splat.

You should be able to register it using

using Splat.Serilog;

// Then in your service locator initialisation
locator.CurrentMutable.UseSerilogWithWrappingFullLogger();

See https://github.com/reactiveui/splat#serilog for further details.

Then you would just register the logger as per normal.

The Splat Serilog also supports semantic logging out of the box, while the ILogger would strip that information.

Glenn Watson
  • 2,758
  • 1
  • 20
  • 30
  • It's working, but how to enable this: ```csharp this.WhenAnyValue(x => x.IsHierarchical).Log(this, "ClusterableDictionaryValueSelector IsHierarchical exception") .BindTo(this, x => x.ViewModel.IsHierarchical).DisposeWith(disposables); ``` – Igor. A Jul 16 '19 at 14:47
  • Thanks, it works. LoggedCatch will call if there is an error in the ReactiveUI, right? – Igor. A Jul 16 '19 at 15:53
  • Will capture any exceptions passed into your observable stream, log them, and also allow you to pass it on in a new stream. – Glenn Watson Jul 16 '19 at 15:54