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?
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.