Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Changes to Logging API
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy committed Jan 27, 2016
1 parent a6b0cd5 commit 4528bdb
Show file tree
Hide file tree
Showing 28 changed files with 799 additions and 1,086 deletions.
38 changes: 38 additions & 0 deletions src/Microsoft.Extensions.Logging.Abstractions/EventId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Extensions.Logging
{
public struct EventId
{
private int _id;
private string _name;

public EventId(int id, string name = null)
{
_id = id;
_name = name;
}

public int Id
{
get
{
return _id;
}
}

public string Name
{
get
{
return _name;
}
}

public static implicit operator EventId(int i)
{
return new EventId(i);
}
}
}
15 changes: 0 additions & 15 deletions src/Microsoft.Extensions.Logging.Abstractions/ILogValues.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Microsoft.Extensions.Logging.Abstractions/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface ILogger
/// <param name="exception"></param>
/// <param name="formatter"></param>
/// <returns></returns>
void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter);
void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter);

/// <summary>
/// Checks if the given LogLevel is enabled.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;

Expand All @@ -10,26 +12,58 @@ namespace Microsoft.Extensions.Logging.Internal
/// LogValues to enable formatting options supported by <see cref="string.Format"/>.
/// This also enables using {NamedformatItem} in the format string.
/// </summary>
public class FormattedLogValues : ILogValues
public class FormattedLogValues : IReadOnlyList<KeyValuePair<string, object>>
{
private static ConcurrentDictionary<string, LogValuesFormatter> _formatters = new ConcurrentDictionary<string, LogValuesFormatter>();
private readonly LogValuesFormatter _formatter;
private readonly object[] _values;

public FormattedLogValues(string format, params object[] values)
{
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
_formatter = _formatters.GetOrAdd(format, f => new LogValuesFormatter(f));
_values = values;
}

public IEnumerable<KeyValuePair<string, object>> GetValues()
public KeyValuePair<string, object> this[int index]
{
return _formatter.GetValues(_values);
get
{
if (index > Count)
{
throw new IndexOutOfRangeException(nameof(index));
}
return _formatter.GetValue(_values, index);
}
}

public int Count
{
get
{
return _formatter.ValueNames.Count + 1;
}
}

public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
for (int i = 0; i < Count; ++i)
{
yield return this[i];
}
}

public override string ToString()
{
return _formatter.Format(_values);
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -144,6 +145,21 @@ public string Format(object[] values)
return string.Format(CultureInfo.InvariantCulture, _format, values);
}

public KeyValuePair<string, object> GetValue(object[] values, int index)
{
if (index < 0 || index > _valueNames.Count + 1)
{
throw new IndexOutOfRangeException(nameof(index));
}

if (_valueNames.Count > index)
{
return new KeyValuePair<string, object>(_valueNames[index], values[index]);
}

return new KeyValuePair<string, object>("{OriginalFormat}", OriginalFormat);
}

public IEnumerable<KeyValuePair<string, object>> GetValues(object[] values)
{
var valueArray = new KeyValuePair<string, object>[values.Length + 1];
Expand Down
124 changes: 0 additions & 124 deletions src/Microsoft.Extensions.Logging.Abstractions/LogFormatter.cs

This file was deleted.

Loading

0 comments on commit 4528bdb

Please sign in to comment.