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

Commit 4528bdb

Browse files
committed
Changes to Logging API
1 parent a6b0cd5 commit 4528bdb

File tree

28 files changed

+799
-1086
lines changed

28 files changed

+799
-1086
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.Extensions.Logging
5+
{
6+
public struct EventId
7+
{
8+
private int _id;
9+
private string _name;
10+
11+
public EventId(int id, string name = null)
12+
{
13+
_id = id;
14+
_name = name;
15+
}
16+
17+
public int Id
18+
{
19+
get
20+
{
21+
return _id;
22+
}
23+
}
24+
25+
public string Name
26+
{
27+
get
28+
{
29+
return _name;
30+
}
31+
}
32+
33+
public static implicit operator EventId(int i)
34+
{
35+
return new EventId(i);
36+
}
37+
}
38+
}

src/Microsoft.Extensions.Logging.Abstractions/ILogValues.cs

-15
This file was deleted.

src/Microsoft.Extensions.Logging.Abstractions/ILogger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface ILogger
1919
/// <param name="exception"></param>
2020
/// <param name="formatter"></param>
2121
/// <returns></returns>
22-
void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter);
22+
void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter);
2323

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

4+
using System;
5+
using System.Collections;
46
using System.Collections.Concurrent;
57
using System.Collections.Generic;
68

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

1921
public FormattedLogValues(string format, params object[] values)
2022
{
23+
if (format == null)
24+
{
25+
throw new ArgumentNullException(nameof(format));
26+
}
2127
_formatter = _formatters.GetOrAdd(format, f => new LogValuesFormatter(f));
2228
_values = values;
2329
}
2430

25-
public IEnumerable<KeyValuePair<string, object>> GetValues()
31+
public KeyValuePair<string, object> this[int index]
2632
{
27-
return _formatter.GetValues(_values);
33+
get
34+
{
35+
if (index > Count)
36+
{
37+
throw new IndexOutOfRangeException(nameof(index));
38+
}
39+
return _formatter.GetValue(_values, index);
40+
}
41+
}
42+
43+
public int Count
44+
{
45+
get
46+
{
47+
return _formatter.ValueNames.Count + 1;
48+
}
49+
}
50+
51+
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
52+
{
53+
for (int i = 0; i < Count; ++i)
54+
{
55+
yield return this[i];
56+
}
2857
}
2958

3059
public override string ToString()
3160
{
3261
return _formatter.Format(_values);
3362
}
63+
64+
IEnumerator IEnumerable.GetEnumerator()
65+
{
66+
return GetEnumerator();
67+
}
3468
}
3569
}

src/Microsoft.Extensions.Logging.Abstractions/Internal/LogValuesFormatter.cs

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -144,6 +145,21 @@ public string Format(object[] values)
144145
return string.Format(CultureInfo.InvariantCulture, _format, values);
145146
}
146147

148+
public KeyValuePair<string, object> GetValue(object[] values, int index)
149+
{
150+
if (index < 0 || index > _valueNames.Count + 1)
151+
{
152+
throw new IndexOutOfRangeException(nameof(index));
153+
}
154+
155+
if (_valueNames.Count > index)
156+
{
157+
return new KeyValuePair<string, object>(_valueNames[index], values[index]);
158+
}
159+
160+
return new KeyValuePair<string, object>("{OriginalFormat}", OriginalFormat);
161+
}
162+
147163
public IEnumerable<KeyValuePair<string, object>> GetValues(object[] values)
148164
{
149165
var valueArray = new KeyValuePair<string, object>[values.Length + 1];

src/Microsoft.Extensions.Logging.Abstractions/LogFormatter.cs

-124
This file was deleted.

0 commit comments

Comments
 (0)