Umbraco 中基于代码的 Serilog 配置

Code based Serilog configuration in Umbraco

我正在关注 guidelines 通过 C# 而不是通过配置文件配置 Serilog。这适用于 Umbraco 8 应用程序。

指南指定我需要从 UmbracoApplication 继承,然后覆盖 GetLogger() 方法:

public class FineTuneLogging : UmbracoApplication
{
    protected override ILogger GetLogger()
    {
        var loggerConfig = new LoggerConfiguration();
        loggerConfig
            .Enrich.WithProperty("MyProperty", "whatIWant")
            .MinimalConfiguration()
            .OutputDefaultTextFile(LogEventLevel.Error)
            .OutputDefaultJsonFile(LogEventLevel.Information)
            .ReadFromConfigFile()
            .ReadFromUserConfigFile();

        return new SerilogLogger(loggerConfig);
    }
}

但是,在继承树中找不到 GetLogger()。

文档是否过时或者我遗漏了什么?

你应该覆盖 GetRuntime

public class FineTuneLogging : UmbracoApplication
{
    protected override IRuntime GetRuntime()
    {
        return new FineTuneLoggingRuntime(this);
    }
}

public class FineTuneLoggingRuntime : WebRuntime
{
    public FineTuneLoggingRuntime(UmbracoApplicationBase umbracoApplication) : base(umbracoApplication) { }

    protected override ILogger GetLogger()
    {
        var loggerConfig = new LoggerConfiguration();
        loggerConfig
            .Enrich.WithProperty("MyProperty", "whatIWant")
            .MinimalConfiguration()
            .OutputDefaultTextFile(LogEventLevel.Error)
            .OutputDefaultJsonFile(LogEventLevel.Information)
            .ReadFromConfigFile()
            .ReadFromUserConfigFile();

        return new SerilogLogger(loggerConfig);
    }
}