【问题标题】:How to set up logging in splat ReactiveUI?如何在 splat ReactiveUI 中设置日志记录?
【发布时间】:2019-11-25 07:09:52
【问题描述】:

我需要启用响应式登录。我实现了ILogger接口:

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);
    }
}

并注册:

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

并尝试像这样使用它:

LogHost.Default.Error("TestTestTest");

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

但是我找不到日志文件。当我调用LogHost.Default 时,没有调用任何方法。怎么了?

【问题讨论】:

标签: c# .net-core reactiveui splat


【解决方案1】:

我们有一个为 Splat 制作的预制 Serilog 记录器。

您应该可以使用

注册它
using Splat.Serilog;

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

更多详情请参阅https://github.com/reactiveui/splat#serilog

然后您只需照常注册记录器即可。

Splat Serilog 还支持开箱即用的语义日志记录,而 ILogger 会删除该信息。

【讨论】:

  • 它正在工作,但是如何启用它:csharp this.WhenAnyValue(x => x.IsHierarchical).Log(this, "ClusterableDictionaryValueSelector IsHierarchical exception") .BindTo(this, x => x.ViewModel.IsHierarchical).DisposeWith(disposables);
  • 谢谢,它有效。如果 ReactiveUI 出现错误,LoggedCatch 会调用,对吧?
  • 将捕获传递到可观察流中的任何异常,记录它们,并允许您将其传递到新流中。
猜你喜欢
  • 2012-09-24
  • 1970-01-01
  • 2023-03-24
  • 2012-09-03
  • 1970-01-01
  • 2013-08-04
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多