/
LogMessage.cs
380 lines (325 loc) · 9.99 KB
/
LogMessage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//-----------------------------------------------------------------------
// <copyright file="LogMessage.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Akka.Annotations;
namespace Akka.Event
{
/// <summary>
/// Extension methods for creating <see cref="LogMessage"/> instances.
/// </summary>
public static class LogMessageExtensions{
}
/// <summary>
/// Represents a log message which is composed of a format string and format args.
/// </summary>
/// <remarks>
/// Call ToString to get the formatted output.
/// </remarks>
public abstract class LogMessage
{
protected readonly ILogMessageFormatter Formatter;
/// <summary>
/// Gets the format string of this log message.
/// </summary>
public string Format { get; private set; }
/// <summary>
/// Initializes an instance of the LogMessage with the specified formatter, format and args.
/// </summary>
/// <param name="formatter">The formatter for the LogMessage.</param>
/// <param name="format">The string format of the LogMessage.</param>
public LogMessage(ILogMessageFormatter formatter, string format)
{
Formatter = formatter;
Format = format;
}
/// <summary>
/// INTERNAL API
/// </summary>
/// <returns>An unformatted copy of the state string - used for debugging bad logging templates</returns>
[InternalApi]
public abstract string Unformatted();
/// <summary>
/// INTERNAL API
/// </summary>
/// <returns>The unformatted log arguments - used during debugging and by third-party logging libraries</returns>
[InternalApi]
public abstract IEnumerable<object> Parameters();
}
/// <summary>
/// Generic version of the argument.
/// </summary>
/// <typeparam name="T"></typeparam>
internal sealed class LogMessage<T> : LogMessage where T:IEnumerable<object>
{
public LogMessage(ILogMessageFormatter formatter, string format, T arg) : base(formatter, format)
{
Arg = arg;
}
public T Arg { get; }
public override string ToString()
{
return Formatter.Format(Format, Arg);
}
public override string Unformatted()
{
return Arg.ToString();
}
public override IEnumerable<object> Parameters() => Arg;
}
/// <summary>
/// Works akin to the original <see cref="LogMessage"/> class with an array of objects as the format args.
/// </summary>
internal sealed class DefaultLogMessage : LogMessage
{
public DefaultLogMessage(ILogMessageFormatter formatter, string format, params object[] args) : base(formatter, format)
{
Args = args;
}
public object[] Args { get; }
public override string ToString()
{
return Formatter.Format(Format, Args);
}
public override string Unformatted()
{
return string.Join(",", Args);
}
public override IEnumerable<object> Parameters()
{
return Args;
}
}
internal readonly struct LogValues<T1> : IReadOnlyList<object>
{
private readonly T1 _value1;
public LogValues(T1 value1)
{
_value1 = value1;
}
public IEnumerator<object> GetEnumerator()
{
yield return this[0];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count => 1;
public object this[int index]
{
get
{
if(index == 0)
return _value1;
throw new IndexOutOfRangeException(nameof(index));
}
}
}
internal readonly struct LogValues<T1, T2> : IReadOnlyList<object>
{
private readonly T1 _value1;
private readonly T2 _value2;
public LogValues(T1 value1, T2 value2)
{
_value1 = value1;
_value2 = value2;
}
public IEnumerator<object> GetEnumerator()
{
yield return this[0];
yield return this[1];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count => 2;
public object this[int index]
{
get
{
return index switch
{
0 => _value1,
1 => _value2,
_ => throw new IndexOutOfRangeException(nameof(index))
};
}
}
}
internal readonly struct LogValues<T1, T2, T3> : IReadOnlyList<object>
{
private readonly T1 _value1;
private readonly T2 _value2;
private readonly T3 _value3;
public LogValues(T1 value1, T2 value2, T3 value3)
{
_value1 = value1;
_value2 = value2;
_value3 = value3;
}
public IEnumerator<object> GetEnumerator()
{
yield return this[0];
yield return this[1];
yield return this[2];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count => 3;
public object this[int index]
{
get
{
return index switch
{
0 => _value1,
1 => _value2,
2 => _value3,
_ => throw new IndexOutOfRangeException(nameof(index))
};
}
}
}
internal readonly struct LogValues<T1, T2, T3, T4> : IReadOnlyList<object>
{
private readonly T1 _value1;
private readonly T2 _value2;
private readonly T3 _value3;
private readonly T4 _value4;
public LogValues(T1 value1, T2 value2, T3 value3, T4 value4)
{
_value1 = value1;
_value2 = value2;
_value3 = value3;
_value4 = value4;
}
public IEnumerator<object> GetEnumerator()
{
yield return this[0];
yield return this[1];
yield return this[2];
yield return this[3];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count => 4;
public object this[int index]
{
get
{
return index switch
{
0 => _value1,
1 => _value2,
2 => _value3,
3 => _value4,
_ => throw new IndexOutOfRangeException(nameof(index))
};
}
}
}
internal readonly struct LogValues<T1, T2, T3, T4, T5> : IReadOnlyList<object>
{
private readonly T1 _value1;
private readonly T2 _value2;
private readonly T3 _value3;
private readonly T4 _value4;
private readonly T5 _value5;
public LogValues(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
{
_value1 = value1;
_value2 = value2;
_value3 = value3;
_value4 = value4;
_value5 = value5;
}
public IEnumerator<object> GetEnumerator()
{
yield return this[0];
yield return this[1];
yield return this[2];
yield return this[3];
yield return this[4];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count => 5;
public object this[int index]
{
get
{
return index switch
{
0 => _value1,
1 => _value2,
2 => _value3,
3 => _value4,
4 => _value5,
_ => throw new IndexOutOfRangeException(nameof(index))
};
}
}
}
internal readonly struct LogValues<T1, T2, T3, T4, T5, T6> : IReadOnlyList<object>
{
private readonly T1 _value1;
private readonly T2 _value2;
private readonly T3 _value3;
private readonly T4 _value4;
private readonly T5 _value5;
private readonly T6 _value6;
public LogValues(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
{
_value1 = value1;
_value2 = value2;
_value3 = value3;
_value4 = value4;
_value5 = value5;
_value6 = value6;
}
public IEnumerator<object> GetEnumerator()
{
yield return this[0];
yield return this[1];
yield return this[2];
yield return this[3];
yield return this[4];
yield return this[5];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public int Count => 6;
public object this[int index]
{
get
{
return index switch
{
0 => _value1,
1 => _value2,
2 => _value3,
3 => _value4,
4 => _value5,
5 => _value6,
_ => throw new IndexOutOfRangeException(nameof(index))
};
}
}
}
}