@@ -15,6 +15,8 @@ public class EventLogLogger : ILogger
15
15
{
16
16
private readonly string _name ;
17
17
private readonly EventLogSettings _settings ;
18
+ private readonly IExternalScopeProvider _externalScopeProvider ;
19
+
18
20
private const string ContinuationString = "..." ;
19
21
private readonly int _beginOrEndMessageSegmentSize ;
20
22
private readonly int _intermediateMessageSegmentSize ;
@@ -34,9 +36,22 @@ public EventLogLogger(string name)
34
36
/// <param name="name">The name of the logger.</param>
35
37
/// <param name="settings">The <see cref="EventLogSettings"/>.</param>
36
38
public EventLogLogger ( string name , EventLogSettings settings )
39
+ : this ( name , settings , new LoggerExternalScopeProvider ( ) )
40
+ {
41
+
42
+ }
43
+
44
+ /// <summary>
45
+ /// Initializes a new instance of the <see cref="EventLogLogger"/> class.
46
+ /// </summary>
47
+ /// <param name="name">The name of the logger.</param>
48
+ /// <param name="settings">The <see cref="EventLogSettings"/>.</param>
49
+ /// <param name="externalScopeProvider">The <see cref="IExternalScopeProvider"/>.</param>
50
+ public EventLogLogger ( string name , EventLogSettings settings , IExternalScopeProvider externalScopeProvider )
37
51
{
38
52
_name = string . IsNullOrEmpty ( name ) ? nameof ( EventLogLogger ) : name ;
39
53
_settings = settings ;
54
+ _externalScopeProvider = externalScopeProvider ;
40
55
41
56
var logName = string . IsNullOrEmpty ( settings . LogName ) ? "Application" : settings . LogName ;
42
57
var sourceName = string . IsNullOrEmpty ( settings . SourceName ) ? "Application" : settings . SourceName ;
@@ -63,7 +78,7 @@ public EventLogLogger(string name, EventLogSettings settings)
63
78
/// <inheritdoc />
64
79
public IDisposable BeginScope < TState > ( TState state )
65
80
{
66
- return NoopDisposable . Instance ;
81
+ return _externalScopeProvider ? . Push ( state ) ;
67
82
}
68
83
69
84
/// <inheritdoc />
@@ -102,9 +117,11 @@ public void Log<TState>(
102
117
103
118
if ( exception != null )
104
119
{
105
- message += Environment . NewLine + Environment . NewLine + exception . ToString ( ) ;
120
+ message += Environment . NewLine + Environment . NewLine + exception ;
106
121
}
107
122
123
+ _externalScopeProvider ? . ForEachScope < object > ( ( scope , _ ) => message += Environment . NewLine + scope , null ) ;
124
+
108
125
WriteMessage ( message , GetEventLogEntryType ( logLevel ) , eventId . Id ) ;
109
126
}
110
127
0 commit comments