public LogMessageProperties(LogMessageTemplate template, object[] values) { _template = template; _values = values; Count = values.Length; }
/// <summary> /// Logs a message and/or exception with the specified <see cref="LogLevel"/>, <see cref="LogEventId"/> /// and properties. /// </summary> /// <param name="logger">The <see cref="ILogger"/>.</param> /// <param name="level">The <see cref="LogLevel"/>.</param> /// <param name="id">The unique identifier of the log event.</param> /// <param name="exception">The <see cref="Exception"/> that occured, may be <c>null</c>.</param> /// <param name="format">The format of the log message.</param> /// <param name="args">The format arguments used to format the message.</param> public static void Log( this ILogger logger, LogLevel level, LogEventId id, Exception exception, string format, params object[] args) { Ensure.Arg.NotNull(logger, nameof(logger)); if (logger.IsEnabled(level)) { LogMessageTemplate template = LogMessageTemplateCache.Get(format); if (template.VariableNames.Count != args?.Length) { throw new ArgumentException( "Number of arguments does not match the number of format variables.", nameof(args)); } IReadOnlyList <LogProperty> properties = args?.Length == 0 ? LogMessageProperties.Empty : new LogMessageProperties(template, args); Log(logger, level, id, exception, template, properties); } }
/// <summary> /// Defines a delegate which writes log event without any properties. /// </summary> /// <param name="level">The <see cref="LogLevel"/>.</param> /// <param name="id">The <see cref="LogEventId"/> which uniquely identifies the event.</param> /// <param name="format">The log message format.</param> /// <returns>The delegate which must be invoked to write the log event.</returns> /// <exception cref="ArgumentNullException">Argument <paramref name="format"/> is <c>null</c>.</exception> public static LoggerEventDelegate Define(LogLevel level, LogEventId id, string format) { LogMessageTemplate template = CreateTemplate(format, 0); return((logger, exception, properties) => { if (logger.IsEnabled(level)) { logger.Log(level, id, exception, template, properties); } }); }
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local private static LogMessageTemplate CreateTemplate(string format, int expectedParameterCount) { Ensure.Arg.NotNull(format, nameof(format)); var template = new LogMessageTemplate(format); if (template.VariableNames.Count < expectedParameterCount) { throw new ArgumentException("Number of format variables is less than the number of arguments.", nameof(format)); } return(template); }
/// <summary> /// Initializes a new instance of the <see cref="LogEvent"/> struct. /// </summary> /// <param name="timestamp">The timestamp of the event.</param> /// <param name="level">The log level of the event</param> /// <param name="id">The unique identifier of the event</param> /// <param name="messageTemplate">The <see cref="LogMessageTemplate"/> used to render the event.</param> /// <param name="properties">The properties that are logged.</param> /// <param name="exception">The <see cref="System.Exception"/> that will be logged.</param> public LogEvent( DateTimeOffset timestamp, LogLevel level, LogEventId id, LogMessageTemplate messageTemplate, IReadOnlyList <LogProperty> properties = null, Exception exception = null ) { Timestamp = timestamp; Level = level; Id = id; MessageTemplate = messageTemplate ?? LogMessageTemplate.Empty; Properties = properties ?? LogMessageProperties.Empty; Exception = exception; }
/// <summary> /// Defines a delegate which writes log event with two properties. /// </summary> /// <param name="level">The <see cref="LogLevel"/>.</param> /// <param name="id">The <see cref="LogEventId"/> which uniquely identifies the event.</param> /// <param name="format">The log message format.</param> /// <returns>The delegate which must be invoked to write the log event.</returns> /// <exception cref="ArgumentNullException">Argument <paramref name="format"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">Number of format variables is less than the number of arguments.</exception> public static LoggerEventDelegate <T0, T1> Define <T0, T1>(LogLevel level, LogEventId id, string format) { LogMessageTemplate template = CreateTemplate(format, 2); return((logger, arg0, arg1, exception, properties) => { if (logger.IsEnabled(level)) { logger.Log( level, id, exception, template, new LogMessageProperties <T0, T1>(template, arg0, arg1, properties)); } }); }
internal static void Log( this ILogger logger, LogLevel level, LogEventId id, Exception exception, LogMessageTemplate messageTemplate, IReadOnlyList <LogProperty> properties) { logger.Log( new LogEvent( GetTimestamp(), level, id, messageTemplate, properties, exception)); }
public void Ignores_VariableNames_In_DoubleCurlyBraces() { var template = new LogMessageTemplate("abc {value1} defg {{value2}}"); Assert.Equal(new[] { "value1" }, template.VariableNames); }
public void Parses_VariablesNames_From_CurlyBraces() { var template = new LogMessageTemplate("abc {value1} defg {value2}"); Assert.Equal(new[] { "value1", "value2" }, template.VariableNames); }