Serilog财产丰富,但仍需要拨打实际写入电话?

时间:2016-04-14 05:18:20

标签: c# .net serilog

我将Serilog添加到我们的日志记录基础架构中,并且对以下内容感到困惑:

public class OzCpWriteEntryExInput : OzCpAppServiceInputBase
{
    private readonly ILogger _Logger;

    /// <summary>
    ///     Class constructor
    /// </summary>
    public OzCpWriteEntryExInput(ILogger aLogger)
    {
        //Save params supplied
        _Logger = aLogger;

        //Create resources required
        Entry = new OzCpLogEntryContextInput();
    }

    public OzCpLogEntryContextInput Entry { get; set; }

    public ILogger Logger => _Logger;
}

public class OzCpLoggingService : OzCruisingPlatformAppServiceBase, IOzCpLoggingService
{
    private const string MESSAGE_TEMPLATE =
        "{LogVersion} {DateTimeUtc:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{LevelRange}] [{ApplicationHostName}] [{ApplicationName}] [{ApplicationVersion}] [{Location}] {Message}{NewLine}{Exception}{NewLine}{StackTrace}{NewLine}{UserDataKind}{@UserData}";

    public OzCpBuildLoggerOutput BuildLogger(OzCpBuildLoggerInput aParams)
    {
        OzCpBuildLoggerOutput result = new OzCpBuildLoggerOutput();
        //#TODO (DE) Convert logger hard coded values into settings
        bool logToUdp = true;

        //By default logging will include all events
        LoggerConfiguration loggerConfiguration = new LoggerConfiguration()
            .Enrich.WithProperty("LogVersion", "01.00")
            .MinimumLevel.Verbose();

        if (logToUdp)
        {
            loggerConfiguration.WriteTo.Udp(IPAddress.Loopback, 11000, 0, LogEventLevel.Verbose, MESSAGE_TEMPLATE);
        }

        //Finally we can build the logger
        result.Logger = loggerConfiguration.CreateLogger();

        return result;
    }

    /// <summary>
    ///     Writes an entry to the supplied logger and associated log sinks.
    /// </summary>
    /// <param name="aParams">Parameters defining the entry to be written.</param>
    /// <returns>Returns whether the entry was written to the logger.</returns>
    public OzCpWriteEntryExOutput WriteEntryEx(OzCpWriteEntryExInput aParams)
    {
        OzCpWriteEntryExOutput result = new OzCpWriteEntryExOutput();

        //These variable values are only the ones out of the entry as the logger "enriches" the other values based on
        //what has been passed in when it was configured
        object[] messageTemplateVariableValues =
        {
            "***",   //Log Version is actually an enriched property!!!!
            aParams.Entry.DateTimeUtc,
            aParams.Entry.LevelRange,
            aParams.Entry.Location,
            aParams.Entry.StackTrace,
            aParams.Entry.UserDataKind,
            aParams.Entry.UserData
        };

        switch (aParams.Entry.EntryKind)
        {
            case OzCpLoggingEntryKindEnum.Verbose:
                aParams.Logger.Verbose(aParams.Entry.Exception, MESSAGE_TEMPLATE, messageTemplateVariableValues);
                break;
        }

        return result;
    }
}

最终,WriteLogEx()的调用者可以提供自己的Serilog.ILogger。我不会直截了当地说明为什么会这样,只是假设这是一个要求。

作为示例,我包括BuildLogger(),它设置ILogger以使用自定义消息模板写入UDP,并通过浓缩配置构建器设置一些其他属性。

一切都很直接。但是现在当我使用ILogger.Verbose()写入ILogger时,我需要在消息模板中传递令牌的参数。

然而,这似乎是序数,所以第一个进入 {LogVersion} ,它应该通过浓缩器完成先前为记录器设置的配置。

如果我没有LogVersion格式化的消息模板被一个人关闭,自然如果我包含它然后我得到:

  

*** 2016-04-14 14:53:57.677 +10:00 [信息] ......

而不是我想要的是:

  

01.00 2016-04-14 14:53:57.677 +10:00 [信息] ......

那么如何从ILogger调用日志记录方法并按名称而不是位置传递令牌值?

1 个答案:

答案 0 :(得分:0)

不要在body{ margin: 0; padding: 0; } .phone-container { width: 100%; } .phone { float: left; width: 33.3%; } 语句中传递这些内容,只需在那里传递特定于事件的数据。可以通过对记录器进行上下文化来添加其他输出模板属性:

.Verbose()

var ctx = logger.ForContext("DateTimeUtc", aParams.Entry.DateTimeUtc) .ForContext("LevelRange", aParams.Entry.LevelRange) // and so-on. ctx.Verbose("Message here..."); 重载会接受ForContext()个数组,您可以用它来整理我相信的内容。