Skip to content

Commit a702c19

Browse files
authored
Merge pull request #79 from 0UserName/#74
#74 Console.WriteLine has been replaced with EventSource
2 parents 325550c + ed91864 commit a702c19

File tree

9 files changed

+439
-7
lines changed

9 files changed

+439
-7
lines changed

RabbitMQ.Stream.Client/Client.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public async Task<CloseResponse> Close(string reason)
462462
}
463463
catch (Exception e)
464464
{
465-
Console.WriteLine(e);
465+
LogEventSource.Log.LogError($"An error occurred while calling { nameof(connection.Dispose) }.", e);
466466
}
467467

468468
return result;

RabbitMQ.Stream.Client/Consumer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ public void Dispose()
138138
}
139139
catch (Exception e)
140140
{
141-
Console.WriteLine($"Error during disposing Consumer: {subscriberId}, " +
142-
$"error: {e}");
141+
LogEventSource.Log.LogError($"Error during disposing Consumer: {subscriberId}.", e);
143142
}
144143

145144
GC.SuppressFinalize(this);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 2.0.
3+
// Copyright (c) 2007-2020 VMware, Inc.
4+
5+
using System;
6+
7+
namespace RabbitMQ.Stream.Client
8+
{
9+
internal interface ILogEventSource
10+
{
11+
/// <summary>
12+
/// Writes an informational log message.
13+
/// </summary>
14+
///
15+
/// <param name="message">
16+
/// </param>
17+
ILogEventSource LogInformation(string message);
18+
19+
/// <summary>
20+
/// Writes an informational log message.
21+
/// </summary>
22+
///
23+
/// <param name="message">
24+
/// </param>
25+
///
26+
/// <param name="args">
27+
/// </param>
28+
ILogEventSource LogInformation(string message, params object[] args);
29+
30+
/// <summary>
31+
/// Writes a warning log message.
32+
/// </summary>
33+
///
34+
/// <param name="message">
35+
/// </param>
36+
ILogEventSource LogWarning(string message);
37+
38+
/// <summary>
39+
/// Writes a warning log message.
40+
/// </summary>
41+
///
42+
/// <param name="message">
43+
/// </param>
44+
///
45+
/// <param name="args">
46+
/// </param>
47+
ILogEventSource LogWarning(string message, params object[] args);
48+
49+
/// <summary>
50+
/// Writes an error log message.
51+
/// </summary>
52+
///
53+
/// <param name="message">
54+
/// </param>
55+
ILogEventSource LogError(string message);
56+
57+
/// <summary>
58+
/// Writes an error log message.
59+
/// </summary>
60+
///
61+
/// <param name="message">
62+
/// </param>
63+
///
64+
/// <param name="exception">
65+
/// The exception to log.
66+
/// </param>
67+
ILogEventSource LogError(string message, Exception exception);
68+
69+
/// <summary>
70+
/// Writes an error log message.
71+
/// </summary>
72+
///
73+
/// <param name="message">
74+
/// </param>
75+
///
76+
/// <param name="exception">
77+
/// The exception to log.
78+
/// </param>
79+
///
80+
/// <param name="args">
81+
/// </param>
82+
ILogEventSource LogError(string message, Exception exception, params object[] args);
83+
}
84+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 2.0.
3+
// Copyright (c) 2007-2020 VMware, Inc.
4+
5+
using System;
6+
using System.Diagnostics.Tracing;
7+
8+
namespace RabbitMQ.Stream.Client
9+
{
10+
public sealed class LogEventListener : EventListener, IDisposable
11+
{
12+
public LogEventListener()
13+
{
14+
EnableEvents((EventSource)LogEventSource.Log, EventLevel.Verbose, Keywords.Log);
15+
}
16+
17+
/// <summary>
18+
/// Performs application-defined tasks
19+
/// associated with freeing, releasing,
20+
/// or resetting unmanaged resources.
21+
/// </summary>
22+
public override void Dispose()
23+
{
24+
DisableEvents((EventSource)LogEventSource.Log);
25+
}
26+
27+
/// <summary>
28+
/// Called whenever an event has been written by
29+
/// an event source for which the event listener
30+
/// has enabled events.
31+
/// </summary>
32+
///
33+
/// <param name="eventData">
34+
/// The event arguments that describe the event.
35+
/// </param>
36+
protected override void OnEventWritten(EventWrittenEventArgs eventData)
37+
{
38+
for (var i = 0; i < eventData.Payload.Count; i++)
39+
{
40+
Console.WriteLine("{0}: {1}: {2}", eventData.Level, eventData.Message, eventData.Payload[i]);
41+
}
42+
43+
base.OnEventWritten(eventData);
44+
}
45+
}
46+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 2.0.
3+
// Copyright (c) 2007-2020 VMware, Inc.
4+
5+
using System;
6+
using System.Diagnostics.Tracing;
7+
8+
namespace RabbitMQ.Stream.Client
9+
{
10+
public static class Keywords
11+
{
12+
public const EventKeywords Log = (EventKeywords)1;
13+
}
14+
15+
[EventSource(Name = "rabbitmq-client-stream")]
16+
internal sealed class LogEventSource : EventSource, ILogEventSource
17+
{
18+
/// <summary>
19+
/// Default <see cref="LogEventSource" /> implementation for logging.
20+
/// </summary>
21+
public static readonly
22+
ILogEventSource Log = new
23+
LogEventSource
24+
();
25+
26+
private LogEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat)
27+
{ }
28+
29+
/// <summary>
30+
/// </summary>
31+
[NonEvent]
32+
private static string ConvertToString(Exception exception)
33+
{
34+
return exception == default ? default : $"{Environment.NewLine}{ exception?.ToString() }";
35+
}
36+
37+
/// <summary>
38+
/// Writes an informational log message.
39+
/// </summary>
40+
///
41+
/// <param name="message">
42+
/// </param>
43+
[Event(1, Message = "INFO", Keywords = Keywords.Log, Level = EventLevel.Informational)]
44+
public ILogEventSource LogInformation(string message)
45+
{
46+
if (IsEnabled())
47+
{
48+
WriteEvent(1, message);
49+
}
50+
51+
return this;
52+
}
53+
54+
/// <summary>
55+
/// Writes an informational log message.
56+
/// </summary>
57+
///
58+
/// <param name="message">
59+
/// </param>
60+
///
61+
/// <param name="args">
62+
/// </param>
63+
[NonEvent]
64+
public ILogEventSource LogInformation(string message, params object[] args)
65+
{
66+
return LogInformation(string.Format(message, args));
67+
}
68+
69+
/// <summary>
70+
/// Writes a warning log message.
71+
/// </summary>
72+
///
73+
/// <param name="message">
74+
/// </param>
75+
[Event(2, Message = "WARN", Keywords = Keywords.Log, Level = EventLevel.Warning)]
76+
public ILogEventSource LogWarning(string message)
77+
{
78+
if (IsEnabled())
79+
{
80+
WriteEvent(2, message);
81+
}
82+
83+
return this;
84+
}
85+
86+
/// <summary>
87+
/// Writes a warning log message.
88+
/// </summary>
89+
///
90+
/// <param name="message">
91+
/// </param>
92+
///
93+
/// <param name="args">
94+
/// </param>
95+
public ILogEventSource LogWarning(string message, params object[] args)
96+
{
97+
return LogWarning(string.Format(message, args));
98+
}
99+
100+
/// <summary>
101+
/// Writes an error log message.
102+
/// </summary>
103+
///
104+
/// <param name="message">
105+
/// </param>
106+
[Event(3, Message = "ERROR", Keywords = Keywords.Log, Level = EventLevel.Error)]
107+
public ILogEventSource LogError(string message)
108+
{
109+
if (IsEnabled())
110+
{
111+
WriteEvent(3, message);
112+
}
113+
114+
return this;
115+
}
116+
117+
/// <summary>
118+
/// Writes an error log message.
119+
/// </summary>
120+
///
121+
/// <param name="message">
122+
/// </param>
123+
///
124+
/// <param name="exception">
125+
/// The exception to log.
126+
/// </param>
127+
[NonEvent]
128+
public ILogEventSource LogError(string message, Exception exception)
129+
{
130+
LogError($"{ message }{ ConvertToString(exception) }");
131+
132+
return this;
133+
}
134+
135+
/// <summary>
136+
/// Writes an error log message.
137+
/// </summary>
138+
///
139+
/// <param name="message">
140+
/// </param>
141+
///
142+
/// <param name="exception">
143+
/// The exception to log.
144+
/// </param>
145+
///
146+
/// <param name="args">
147+
/// </param>
148+
[NonEvent]
149+
public ILogEventSource LogError(string message, Exception exception, params object[] args)
150+
{
151+
return LogError(string.Format(message, args), exception);
152+
}
153+
}
154+
}

RabbitMQ.Stream.Client/Message.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Buffers;
7+
78
using RabbitMQ.Stream.Client.AMQP;
89

910
namespace RabbitMQ.Stream.Client
@@ -129,7 +130,7 @@ public static Message From(ReadOnlySequence<byte> amqpData)
129130
offset += AmqpWireFormatting.ReadAny(amqpData.Slice(offset), out amqpValue);
130131
break;
131132
default:
132-
Console.WriteLine($"dataCode: {dataCode} not handled. Please open an issue.");
133+
LogEventSource.Log.LogError($"dataCode: {dataCode} not handled. Please open an issue.");
133134
throw new ArgumentOutOfRangeException($"dataCode: {dataCode} not handled");
134135
}
135136
}

RabbitMQ.Stream.Client/Producer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private async Task SemaphoreWait()
137137
// Nope, we have maxed our In-Flight messages, let's asynchronously wait for confirms
138138
if (!await semaphore.WaitAsync(1000).ConfigureAwait(false))
139139
{
140-
Console.WriteLine("Semaphore Wait timeout during publishing.");
140+
LogEventSource.Log.LogWarning("Semaphore Wait timeout during publishing.");
141141
}
142142
}
143143
}
@@ -240,8 +240,7 @@ public void Dispose()
240240
}
241241
catch (Exception e)
242242
{
243-
Console.WriteLine($"Error during disposing producer: {publisherId}, " +
244-
$"error: {e}");
243+
LogEventSource.Log.LogError($"Error during disposing Consumer: {publisherId}.", e);
245244
}
246245

247246
GC.SuppressFinalize(this);

Tests/ApiApproval.Approve.verified.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,23 @@ namespace RabbitMQ.Stream.Client
452452
bool ValidateDns { get; set; }
453453
RabbitMQ.Stream.Client.IClient CreateClient(RabbitMQ.Stream.Client.ClientParameters clientParameters);
454454
}
455+
public static class Keywords
456+
{
457+
public const System.Diagnostics.Tracing.EventKeywords Log = 1;
458+
}
455459
public readonly struct LeaderLocator
456460
{
457461
public static RabbitMQ.Stream.Client.LeaderLocator ClientLocal { get; }
458462
public static RabbitMQ.Stream.Client.LeaderLocator LeastLeaders { get; }
459463
public static RabbitMQ.Stream.Client.LeaderLocator Random { get; }
460464
public override string ToString() { }
461465
}
466+
public sealed class LogEventListener : System.Diagnostics.Tracing.EventListener, System.IDisposable
467+
{
468+
public LogEventListener() { }
469+
public override void Dispose() { }
470+
protected override void OnEventWritten(System.Diagnostics.Tracing.EventWrittenEventArgs eventData) { }
471+
}
462472
public sealed class ManualResetValueTaskSource<T> : System.Threading.Tasks.Sources.IValueTaskSource, System.Threading.Tasks.Sources.IValueTaskSource<T>
463473
{
464474
public ManualResetValueTaskSource() { }

0 commit comments

Comments
 (0)