Skip to content

Commit ed91864

Browse files
committed
fixup! #74 Console.WriteLine has been replaced with EventSource
1 parent 322e2aa commit ed91864

File tree

5 files changed

+111
-16
lines changed

5 files changed

+111
-16
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/ILogEventSource.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ internal interface ILogEventSource
1616
/// </param>
1717
ILogEventSource LogInformation(string message);
1818

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+
1930
/// <summary>
2031
/// Writes a warning log message.
2132
/// </summary>
@@ -24,6 +35,17 @@ internal interface ILogEventSource
2435
/// </param>
2536
ILogEventSource LogWarning(string message);
2637

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+
2749
/// <summary>
2850
/// Writes an error log message.
2951
/// </summary>
@@ -43,5 +65,20 @@ internal interface ILogEventSource
4365
/// The exception to log.
4466
/// </param>
4567
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);
4683
}
4784
}

RabbitMQ.Stream.Client/LogEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
3737
{
3838
for (var i = 0; i < eventData.Payload.Count; i++)
3939
{
40-
Console.WriteLine("{0}: {1}: {2}", eventData.Level, eventData.PayloadNames[i], eventData.Payload[i]);
40+
Console.WriteLine("{0}: {1}: {2}", eventData.Level, eventData.Message, eventData.Payload[i]);
4141
}
4242

4343
base.OnEventWritten(eventData);

RabbitMQ.Stream.Client/LogEventSource.cs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public static class Keywords
1515
[EventSource(Name = "rabbitmq-client-stream")]
1616
internal sealed class LogEventSource : EventSource, ILogEventSource
1717
{
18-
private static readonly char[] _newLineChars = Environment.NewLine.ToCharArray();
19-
2018
/// <summary>
2119
/// Default <see cref="LogEventSource" /> implementation for logging.
2220
/// </summary>
@@ -33,7 +31,7 @@ private LogEventSource() : base(EventSourceSettings.EtwSelfDescribingEventFormat
3331
[NonEvent]
3432
private static string ConvertToString(Exception exception)
3533
{
36-
return exception?.ToString();
34+
return exception == default ? default : $"{Environment.NewLine}{ exception?.ToString() }";
3735
}
3836

3937
/// <summary>
@@ -42,7 +40,7 @@ private static string ConvertToString(Exception exception)
4240
///
4341
/// <param name="message">
4442
/// </param>
45-
[Event(1, Level = EventLevel.Informational)]
43+
[Event(1, Message = "INFO", Keywords = Keywords.Log, Level = EventLevel.Informational)]
4644
public ILogEventSource LogInformation(string message)
4745
{
4846
if (IsEnabled())
@@ -53,13 +51,28 @@ public ILogEventSource LogInformation(string message)
5351
return this;
5452
}
5553

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+
5669
/// <summary>
5770
/// Writes a warning log message.
5871
/// </summary>
5972
///
6073
/// <param name="message">
6174
/// </param>
62-
[Event(2, Level = EventLevel.Warning)]
75+
[Event(2, Message = "WARN", Keywords = Keywords.Log, Level = EventLevel.Warning)]
6376
public ILogEventSource LogWarning(string message)
6477
{
6578
if (IsEnabled())
@@ -70,13 +83,27 @@ public ILogEventSource LogWarning(string message)
7083
return this;
7184
}
7285

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+
73100
/// <summary>
74101
/// Writes an error log message.
75102
/// </summary>
76103
///
77104
/// <param name="message">
78105
/// </param>
79-
[Event(3, Level = EventLevel.Error)]
106+
[Event(3, Message = "ERROR", Keywords = Keywords.Log, Level = EventLevel.Error)]
80107
public ILogEventSource LogError(string message)
81108
{
82109
if (IsEnabled())
@@ -100,9 +127,28 @@ public ILogEventSource LogError(string message)
100127
[NonEvent]
101128
public ILogEventSource LogError(string message, Exception exception)
102129
{
103-
LogError($"{message}{Environment.NewLine}{ConvertToString(exception)}".Trim(_newLineChars));
130+
LogError($"{ message }{ ConvertToString(exception) }");
104131

105132
return this;
106133
}
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+
}
107153
}
108154
}

Tests/EventSourceTests.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ public void GenerateInfoEvent()
4949
resultException = Record.Exception(() => args.ValidateEventData(EventLevel.Informational, ExpectedPayloadName, new string[] { nameof(GenerateInfoEvent) }));
5050
};
5151

52+
// Simple message.
5253
LogEventSource.Log.LogInformation(nameof(GenerateInfoEvent));
5354

55+
// Simple message formatted with string.Format().
56+
LogEventSource.Log.LogInformation("{0}{1}{2}", "Generate", "Info", "Event");
57+
5458
Assert.Null(resultException);
5559
}
5660

@@ -69,8 +73,12 @@ public void GenerateWarningEvent()
6973
resultException = Record.Exception(() => args.ValidateEventData(EventLevel.Warning, ExpectedPayloadName, new string[] { nameof(GenerateWarningEvent) }));
7074
};
7175

76+
// Simple message.
7277
LogEventSource.Log.LogWarning(nameof(GenerateWarningEvent));
7378

79+
// Simple message formatted with string.Format().
80+
LogEventSource.Log.LogWarning("{0}{1}{2}", "Generate", "Warning", "Event");
81+
7482
Assert.Null(resultException);
7583
}
7684

@@ -89,9 +97,15 @@ public void GenerateErrorEvent()
8997
resultException = Record.Exception(() => args.ValidateEventData(EventLevel.Error, ExpectedPayloadName, new string[] { nameof(GenerateErrorEvent) }));
9098
};
9199

100+
// Simple message.
92101
LogEventSource.Log.LogError(nameof(GenerateErrorEvent));
102+
103+
// Simple message with null exception.
93104
LogEventSource.Log.LogError(nameof(GenerateErrorEvent), default);
94105

106+
// Simple message formatted with string.Format().
107+
LogEventSource.Log.LogError("{0}{1}{2}", default, "Generate", "Error", "Event");
108+
95109
Assert.Null(resultException);
96110
}
97111

@@ -103,20 +117,18 @@ public void GenerateErrorEvent()
103117
[Fact]
104118
public void GenerateErrorWithExceptionEvent()
105119
{
106-
const string exception1Message = "TextExceptionMessage1";
107-
const string exception2Message = "TextExceptionMessage2";
108-
109-
var message = $"GenerateErrorEvent{ Environment.NewLine }System.Exception: { exception2Message }{ Environment.NewLine } ---> System.Exception: { exception1Message }{ Environment.NewLine } --- End of inner exception stack trace ---";
120+
const string Exception1Message = "TextExceptionMessage1";
121+
const string Exception2Message = "TextExceptionMessage2";
110122

111123
Exception resultException = default;
112124

113125
var exception =
114-
new Exception(exception2Message,
115-
new Exception(exception1Message));
126+
new Exception(Exception2Message,
127+
new Exception(Exception1Message));
116128

117129
new LogEventListener().EventWritten += (sender, args) =>
118130
{
119-
resultException = Record.Exception(() => args.ValidateEventData(EventLevel.Error, ExpectedPayloadName, new string[] { message }));
131+
resultException = Record.Exception(() => args.ValidateEventData(EventLevel.Error, ExpectedPayloadName, new string[] { nameof(GenerateErrorEvent), Exception1Message, Exception2Message }));
120132
};
121133

122134
LogEventSource.Log.LogError(nameof(GenerateErrorEvent), exception);

0 commit comments

Comments
 (0)