Umbraco中基于代码的Serilog配置

时间:2019-05-03 08:50:12

标签: umbraco serilog

我遵循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()。

文档过时了还是我缺少什么?

1 个答案:

答案 0 :(得分:1)

您应该覆盖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);
    }
}