Skip to content

Commit bdbf9a8

Browse files
Merge pull request #111 from progaudi/feature/naming-logging
Add name to logging
2 parents d9374f3 + 87e3e7c commit bdbf9a8

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/progaudi.tarantool/Model/ClientOptions.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ public ClientOptions(string replicationSource, ILog log = null, MsgPackContext c
1717
private ClientOptions(ConnectionOptions options, ILog log, MsgPackContext context)
1818
{
1919
ConnectionOptions = options;
20-
LogWriter = log;
2120
MsgPackContext = context ?? new MsgPackContext();
21+
if (log != null)
22+
{
23+
LogWriter = new LogWriterWrapper(this, log);
24+
}
2225
}
2326

2427
public ILog LogWriter { get; }
@@ -28,5 +31,27 @@ private ClientOptions(ConnectionOptions options, ILog log, MsgPackContext contex
2831
public ConnectionOptions ConnectionOptions { get; }
2932

3033
public string Name { get; set; }
34+
35+
private class LogWriterWrapper : ILog
36+
{
37+
private readonly ClientOptions _options;
38+
private readonly ILog _log;
39+
40+
public LogWriterWrapper(ClientOptions options, ILog log)
41+
{
42+
_options = options;
43+
_log = log;
44+
}
45+
46+
public void WriteLine(string message)
47+
{
48+
_log.WriteLine($"[{_options.Name}] {message}");
49+
}
50+
51+
public void Flush()
52+
{
53+
_log.Flush();
54+
}
55+
}
3156
}
3257
}

src/progaudi.tarantool/ResponseWriter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void BeginWriting()
3838
throw new ObjectDisposedException(nameof(ResponseReader));
3939
}
4040

41-
_clientOptions?.LogWriter?.WriteLine($"Starting {_thread.Name}.");
41+
_clientOptions?.LogWriter?.WriteLine("Starting thread");
4242
_thread.Start();
4343
}
4444

@@ -51,7 +51,7 @@ public Task Write(ArraySegment<byte> header, ArraySegment<byte> body)
5151
throw new ObjectDisposedException(nameof(ResponseReader));
5252
}
5353

54-
_clientOptions?.LogWriter?.WriteLine($"{_thread.Name} is enqueuing request: headers {header.Count} bytes, body {body.Count} bytes.");
54+
_clientOptions?.LogWriter?.WriteLine($"Enqueuing request: headers {header.Count} bytes, body {body.Count} bytes.");
5555
bool shouldSignal;
5656
lock (_lock)
5757
{
@@ -120,12 +120,12 @@ Tuple<ArraySegment<byte>, ArraySegment<byte>> GetRequest()
120120
var count = 0;
121121
while ((request = GetRequest()) != null)
122122
{
123-
_clientOptions?.LogWriter?.WriteLine($"{_thread.Name} is writing request: headers {request.Item1.Count} bytes, body {request.Item2.Count} bytes.");
123+
_clientOptions?.LogWriter?.WriteLine($"Writing request: headers {request.Item1.Count} bytes, body {request.Item2.Count} bytes.");
124124

125125
WriteBuffer(request.Item1);
126126
WriteBuffer(request.Item2);
127127

128-
_clientOptions?.LogWriter?.WriteLine($"{_thread.Name} wrote request: headers {request.Item1.Count} bytes, body {request.Item2.Count} bytes.");
128+
_clientOptions?.LogWriter?.WriteLine($"Wrote request: headers {request.Item1.Count} bytes, body {request.Item2.Count} bytes.");
129129

130130
count++;
131131
if (limit > 0 && count > limit)

0 commit comments

Comments
 (0)