-
Notifications
You must be signed in to change notification settings - Fork 2
/
TransformExtensions.cs
47 lines (39 loc) · 1.54 KB
/
TransformExtensions.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
using System;
using JetBrains.Annotations;
namespace Vostok.Logging.Abstractions
{
[PublicAPI]
public static class TransformExtensions
{
/// <summary>
/// <para>Returns a wrapper transforms log events.</para>
/// <para>Does not support <see cref="LogLevel"/> transformations.</para>
/// </summary>
[Pure]
public static ILog WithTransformation([NotNull] this ILog log, [NotNull] Func<LogEvent, LogEvent> transform)
=> new TransformLog(log, transform);
private class TransformLog : ILog
{
private readonly ILog baseLog;
private readonly Func<LogEvent, LogEvent> transform;
public TransformLog(ILog baseLog, Func<LogEvent, LogEvent> transform)
{
this.baseLog = baseLog ?? throw new ArgumentNullException(nameof(baseLog));
this.transform = transform ?? throw new ArgumentNullException(nameof(transform));
}
public void Log(LogEvent @event)
{
if (@event != null)
@event = transform(@event);
baseLog.Log(@event);
}
public bool IsEnabledFor(LogLevel level) =>
baseLog.IsEnabledFor(level);
public ILog ForContext(string context)
{
var baseLogForContext = baseLog.ForContext(context);
return ReferenceEquals(baseLogForContext, baseLog) ? this : new TransformLog(baseLogForContext, transform);
}
}
}
}