-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathCustomBeginScopeTest.cs
144 lines (127 loc) · 4.9 KB
/
CustomBeginScopeTest.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Xunit;
namespace NLog.Extensions.Logging.Tests
{
public class CustomBeginScopeTest : NLogTestBase
{
[Fact]
public void TestNonSerializableSayHello()
{
var runner = GetRunner<CustomBeginScopeTestRunner>();
var target = new NLog.Targets.MemoryTarget() { Layout = "${message} ${mdlc:World}. Welcome ${ndlc}" };
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target);
runner.SayHello().Wait();
Assert.Single(target.Logs);
Assert.Equal("Hello Earth. Welcome Earth People", target.Logs[0]);
}
[Fact]
public void TestNonSerializableSayHelloWithScope()
{
var runner = GetRunner<CustomBeginScopeTestRunner>(new NLogProviderOptions() { IncludeScopes = false });
var target = new NLog.Targets.MemoryTarget() { Layout = "${message} ${mdlc:World}. Welcome ${ndlc}" };
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target);
runner.SayHello().Wait();
Assert.Single(target.Logs);
Assert.Equal("Hello . Welcome ", target.Logs[0]);
}
[Fact]
public void TestNonSerializableSayHi()
{
var runner = GetRunner<CustomBeginScopeTestRunner>();
var target = new NLog.Targets.MemoryTarget() { Layout = "${message} ${mdlc:World}. Welcome ${ndlc}" };
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target);
var scopeString = runner.SayHi().Result;
Assert.Single(target.Logs);
Assert.Equal("Hi Earth. Welcome Earth People", target.Logs[0]);
Assert.Equal("Earth People", scopeString);
}
[Fact]
public void TestNonSerializableSayNothing()
{
var runner = GetRunner<CustomBeginScopeTestRunner>();
var target = new NLog.Targets.MemoryTarget() { Layout = "${message}" };
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target);
runner.SayNothing().Wait();
Assert.Single(target.Logs);
Assert.Equal("Nothing", target.Logs[0]);
}
public class CustomBeginScopeTestRunner
{
private readonly ILogger<CustomBeginScopeTestRunner> _logger;
public CustomBeginScopeTestRunner(ILogger<CustomBeginScopeTestRunner> logger)
{
_logger = logger;
}
public async Task SayHello()
{
using (_logger.BeginScope(new ActionLogScope("Earth")))
{
await Task.Yield();
_logger.LogInformation("Hello");
}
}
public async Task<string> SayHi()
{
using (var scopeState = _logger.BeginScope("{World} People", "Earth"))
{
await Task.Yield();
_logger.LogInformation("Hi");
return scopeState.ToString();
}
}
public async Task SayNothing()
{
using (var scopeState = _logger.BeginScope(new Dictionary<string,string>()))
{
await Task.Yield();
_logger.LogInformation("Nothing");
}
}
}
private class ActionLogScope : IReadOnlyList<KeyValuePair<string, object>>
{
private readonly string _world;
public ActionLogScope(string world)
{
if (world == null)
{
throw new ArgumentNullException(nameof(world));
}
_world = world;
}
public KeyValuePair<string, object> this[int index]
{
get
{
if (index == 0)
{
return new KeyValuePair<string, object>("World", _world);
}
throw new IndexOutOfRangeException(nameof(index));
}
}
public int Count => 1;
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
for (var i = 0; i < Count; ++i)
{
yield return this[i];
}
}
public override string ToString()
{
// We don't include the _action.Id here because it's just an opaque guid, and if
// you have text logging, you can already use the requestId for correlation.
return string.Concat(_world, " People");
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
}