Skip to content

Commit 1a1ea99

Browse files
committed
more tests
1 parent 12b7ee1 commit 1a1ea99

8 files changed

Lines changed: 113 additions & 136 deletions

File tree

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ using MsLogLevel = Microsoft.Extensions.Logging.LogLevel;
101101
using MsLoggerFactory = Microsoft.Extensions.Logging.ILoggerFactory;
102102

103103
[DesignerCategory("Code")]
104+
[SupportedOSPlatform("windows")]
104105
class ProgramService :
105106
ServiceBase
106107
{
@@ -156,7 +157,7 @@ class ProgramService :
156157
}
157158
}
158159
```
159-
<sup><a href='/src/Tests/Snippets/ProgramService.cs#L7-L67' title='Snippet source file'>snippet source</a> | <a href='#snippet-MsLoggingInService' title='Start of snippet'>anchor</a></sup>
160+
<sup><a href='/src/Tests/Snippets/ProgramService.cs#L8-L69' title='Snippet source file'>snippet source</a> | <a href='#snippet-MsLoggingInService' title='Start of snippet'>anchor</a></sup>
160161
<!-- endSnippet -->
161162

162163

src/NServiceBus.MicrosoftLogging.Hosting/NamedLogger.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,51 @@ public NamedLogger(string name, DeferredLoggerFactory defaultLoggerFactory)
1515
public bool IsErrorEnabled { get; internal set; }
1616
public bool IsFatalEnabled { get; internal set; }
1717

18-
public void Debug(string message) =>
19-
defaultLoggerFactory.Write(name, LogLevel.Debug, message);
18+
public void Debug(string? message) =>
19+
defaultLoggerFactory.Write(name, LogLevel.Debug, message ?? "");
2020

21-
public void Debug(string message, Exception exception) =>
22-
defaultLoggerFactory.Write(name, LogLevel.Debug, message + Environment.NewLine + exception);
21+
public void Debug(string? message, Exception? exception) =>
22+
defaultLoggerFactory.Write(name, LogLevel.Debug, (message ?? "") + Environment.NewLine + exception);
2323

24-
public void DebugFormat(string format, params object[] args) =>
24+
public void DebugFormat(string format, params object?[] args) =>
2525
defaultLoggerFactory.Write(name, LogLevel.Debug, string.Format(format, args));
2626

27-
public void Info(string message) =>
28-
defaultLoggerFactory.Write(name, LogLevel.Info, message);
27+
public void Info(string? message) =>
28+
defaultLoggerFactory.Write(name, LogLevel.Info, message ?? "");
2929

30-
public void Info(string message, Exception exception) =>
31-
defaultLoggerFactory.Write(name, LogLevel.Info, message + Environment.NewLine + exception);
30+
public void Info(string? message, Exception? exception) =>
31+
defaultLoggerFactory.Write(name, LogLevel.Info, (message ?? "") + Environment.NewLine + exception);
3232

33-
public void InfoFormat(string format, params object[] args) =>
33+
public void InfoFormat(string format, params object?[] args) =>
3434
defaultLoggerFactory.Write(name, LogLevel.Info, string.Format(format, args));
3535

36-
public void Warn(string message) =>
37-
defaultLoggerFactory.Write(name, LogLevel.Warn, message);
36+
public void Warn(string? message) =>
37+
defaultLoggerFactory.Write(name, LogLevel.Warn, message ?? "");
3838

39-
public void Warn(string message, Exception exception) =>
40-
defaultLoggerFactory.Write(name, LogLevel.Warn, message + Environment.NewLine + exception);
39+
public void Warn(string? message, Exception? exception) =>
40+
defaultLoggerFactory.Write(name, LogLevel.Warn, (message ?? "") + Environment.NewLine + exception);
4141

42-
public void WarnFormat(string format, params object[] args) =>
42+
public void WarnFormat(string format, params object?[] args) =>
4343
defaultLoggerFactory.Write(name, LogLevel.Warn, string.Format(format, args));
4444

45-
public void Error(string message) =>
46-
defaultLoggerFactory.Write(name, LogLevel.Error, message);
45+
public void Error(string? message) =>
46+
defaultLoggerFactory.Write(name, LogLevel.Error, message ?? "");
4747

48-
public void Error(string message, Exception exception) =>
49-
defaultLoggerFactory.Write(name, LogLevel.Error, message + Environment.NewLine + exception);
48+
public void Error(string? message, Exception? exception) =>
49+
defaultLoggerFactory.Write(name, LogLevel.Error, (message ?? "") + Environment.NewLine + exception);
5050

51-
public void ErrorFormat(string format, params object[] args) =>
51+
public void ErrorFormat(string format, params object?[] args) =>
5252
defaultLoggerFactory.Write(name, LogLevel.Error, string.Format(format, args));
5353

54-
public void Fatal(string message) =>
55-
defaultLoggerFactory.Write(name, LogLevel.Fatal, message);
54+
public void Fatal(string? message) =>
55+
defaultLoggerFactory.Write(name, LogLevel.Fatal, message ?? "");
5656

57-
public void Fatal(string message, Exception exception) =>
58-
defaultLoggerFactory.Write(name, LogLevel.Error, message + Environment.NewLine + exception);
57+
public void Fatal(string? message, Exception? exception) =>
58+
defaultLoggerFactory.Write(name, LogLevel.Fatal, (message ?? "") + Environment.NewLine + exception);
5959

60-
public void FatalFormat(string format, params object[] args) =>
60+
public void FatalFormat(string format, params object?[] args) =>
6161
defaultLoggerFactory.Write(name, LogLevel.Fatal, string.Format(format, args));
6262

6363
DeferredLoggerFactory defaultLoggerFactory;
6464
string name;
65-
}
65+
}
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.Extensions.Logging;
1+
using Microsoft.Extensions.Logging;
22
using NServiceBus.Logging;
33

44
class Logger :
@@ -9,54 +9,54 @@ class Logger :
99
public Logger(ILogger logger) =>
1010
this.logger = logger;
1111

12-
public void Debug(string message) =>
12+
public void Debug(string? message) =>
1313
logger.LogDebug(message);
1414

15-
public void Debug(string message, Exception exception) =>
15+
public void Debug(string? message, Exception? exception) =>
1616
logger.LogDebug(exception, message);
1717

18-
public void DebugFormat(string format, params object[] args) =>
18+
public void DebugFormat(string format, params object?[] args) =>
1919
logger.LogDebug(format, args);
2020

21-
public void Info(string message) =>
21+
public void Info(string? message) =>
2222
logger.LogInformation(message);
2323

24-
public void Info(string message, Exception exception) =>
24+
public void Info(string? message, Exception? exception) =>
2525
logger.LogInformation(exception, message);
2626

27-
public void InfoFormat(string format, params object[] args) =>
27+
public void InfoFormat(string format, params object?[] args) =>
2828
logger.LogInformation(format, args);
2929

30-
public void Warn(string message) =>
30+
public void Warn(string? message) =>
3131
logger.LogWarning(message);
3232

33-
public void Warn(string message, Exception exception) =>
33+
public void Warn(string? message, Exception? exception) =>
3434
logger.LogWarning(exception, message);
3535

36-
public void WarnFormat(string format, params object[] args) =>
36+
public void WarnFormat(string format, params object?[] args) =>
3737
logger.LogWarning(format, args);
3838

39-
public void Error(string message) =>
39+
public void Error(string? message) =>
4040
logger.LogError(message);
4141

42-
public void Error(string message, Exception exception) =>
42+
public void Error(string? message, Exception? exception) =>
4343
logger.LogError(exception, message);
4444

45-
public void ErrorFormat(string format, params object[] args) =>
45+
public void ErrorFormat(string format, params object?[] args) =>
4646
logger.LogError(format, args);
4747

48-
public void Fatal(string message) =>
48+
public void Fatal(string? message) =>
4949
logger.LogCritical(message);
5050

51-
public void Fatal(string message, Exception exception) =>
51+
public void Fatal(string? message, Exception? exception) =>
5252
logger.LogCritical(exception, message);
5353

54-
public void FatalFormat(string format, params object[] args) =>
54+
public void FatalFormat(string format, params object?[] args) =>
5555
logger.LogCritical(format, args);
5656

5757
public bool IsDebugEnabled => logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug);
5858
public bool IsInfoEnabled => logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Information);
5959
public bool IsWarnEnabled => logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning);
6060
public bool IsErrorEnabled => logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Error);
6161
public bool IsFatalEnabled => logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Critical);
62-
}
62+
}

src/Tests/DeferredLoggerFactoryTests.cs

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,17 @@ public void Logger_writes_through_factory()
123123
/// <summary>
124124
/// Test implementation of DeferredLoggerFactory that mirrors the production code.
125125
/// </summary>
126-
class TestDeferredLoggerFactory : NServiceBus.Logging.ILoggerFactory
126+
class TestDeferredLoggerFactory(LogLevel level) :
127+
ILoggerFactory
127128
{
128-
LogLevel level;
129-
bool isDebugEnabled;
130-
bool isInfoEnabled;
131-
bool isWarnEnabled;
132-
bool isErrorEnabled;
133-
bool isFatalEnabled;
129+
bool isDebugEnabled = LogLevel.Debug >= level;
130+
bool isInfoEnabled = LogLevel.Info >= level;
131+
bool isWarnEnabled = LogLevel.Warn >= level;
132+
bool isErrorEnabled = LogLevel.Error >= level;
133+
bool isFatalEnabled = LogLevel.Fatal >= level;
134134

135135
public ConcurrentDictionary<string, ConcurrentQueue<(LogLevel level, string message)>> DeferredLogs { get; } = [];
136136

137-
public TestDeferredLoggerFactory(LogLevel level)
138-
{
139-
this.level = level;
140-
isDebugEnabled = LogLevel.Debug >= level;
141-
isInfoEnabled = LogLevel.Info >= level;
142-
isWarnEnabled = LogLevel.Warn >= level;
143-
isErrorEnabled = LogLevel.Error >= level;
144-
isFatalEnabled = LogLevel.Fatal >= level;
145-
}
146-
147137
public ILog GetLogger(Type type) =>
148138
GetLogger(type.FullName!);
149139

@@ -171,65 +161,57 @@ public void Write(string name, LogLevel messageLevel, string message)
171161
/// <summary>
172162
/// Test implementation of NamedLogger that mirrors the production code.
173163
/// </summary>
174-
class TestNamedLogger : ILog
164+
class TestNamedLogger(string name, TestDeferredLoggerFactory factory) :
165+
ILog
175166
{
176-
string name;
177-
TestDeferredLoggerFactory factory;
178-
179-
public TestNamedLogger(string name, TestDeferredLoggerFactory factory)
180-
{
181-
this.name = name;
182-
this.factory = factory;
183-
}
184-
185167
public bool IsDebugEnabled { get; internal set; }
186168
public bool IsInfoEnabled { get; internal set; }
187169
public bool IsWarnEnabled { get; internal set; }
188170
public bool IsErrorEnabled { get; internal set; }
189171
public bool IsFatalEnabled { get; internal set; }
190172

191-
public void Debug(string message) =>
192-
factory.Write(name, LogLevel.Debug, message);
173+
public void Debug(string? message) =>
174+
factory.Write(name, LogLevel.Debug, message ?? "");
193175

194-
public void Debug(string message, Exception exception) =>
195-
factory.Write(name, LogLevel.Debug, message + Environment.NewLine + exception);
176+
public void Debug(string? message, Exception? exception) =>
177+
factory.Write(name, LogLevel.Debug, (message ?? "") + Environment.NewLine + exception);
196178

197-
public void DebugFormat(string format, params object[] args) =>
179+
public void DebugFormat(string format, params object?[] args) =>
198180
factory.Write(name, LogLevel.Debug, string.Format(format, args));
199181

200-
public void Info(string message) =>
201-
factory.Write(name, LogLevel.Info, message);
182+
public void Info(string? message) =>
183+
factory.Write(name, LogLevel.Info, message ?? "");
202184

203-
public void Info(string message, Exception exception) =>
204-
factory.Write(name, LogLevel.Info, message + Environment.NewLine + exception);
185+
public void Info(string? message, Exception? exception) =>
186+
factory.Write(name, LogLevel.Info, (message ?? "") + Environment.NewLine + exception);
205187

206-
public void InfoFormat(string format, params object[] args) =>
188+
public void InfoFormat(string format, params object?[] args) =>
207189
factory.Write(name, LogLevel.Info, string.Format(format, args));
208190

209-
public void Warn(string message) =>
210-
factory.Write(name, LogLevel.Warn, message);
191+
public void Warn(string? message) =>
192+
factory.Write(name, LogLevel.Warn, message ?? "");
211193

212-
public void Warn(string message, Exception exception) =>
213-
factory.Write(name, LogLevel.Warn, message + Environment.NewLine + exception);
194+
public void Warn(string? message, Exception? exception) =>
195+
factory.Write(name, LogLevel.Warn, (message ?? "") + Environment.NewLine + exception);
214196

215-
public void WarnFormat(string format, params object[] args) =>
197+
public void WarnFormat(string format, params object?[] args) =>
216198
factory.Write(name, LogLevel.Warn, string.Format(format, args));
217199

218-
public void Error(string message) =>
219-
factory.Write(name, LogLevel.Error, message);
200+
public void Error(string? message) =>
201+
factory.Write(name, LogLevel.Error, message ?? "");
220202

221-
public void Error(string message, Exception exception) =>
222-
factory.Write(name, LogLevel.Error, message + Environment.NewLine + exception);
203+
public void Error(string? message, Exception? exception) =>
204+
factory.Write(name, LogLevel.Error, (message ?? "") + Environment.NewLine + exception);
223205

224-
public void ErrorFormat(string format, params object[] args) =>
206+
public void ErrorFormat(string format, params object?[] args) =>
225207
factory.Write(name, LogLevel.Error, string.Format(format, args));
226208

227-
public void Fatal(string message) =>
228-
factory.Write(name, LogLevel.Fatal, message);
209+
public void Fatal(string? message) =>
210+
factory.Write(name, LogLevel.Fatal, message ?? "");
229211

230-
public void Fatal(string message, Exception exception) =>
231-
factory.Write(name, LogLevel.Fatal, message + Environment.NewLine + exception);
212+
public void Fatal(string? message, Exception? exception) =>
213+
factory.Write(name, LogLevel.Fatal, (message ?? "") + Environment.NewLine + exception);
232214

233-
public void FatalFormat(string format, params object[] args) =>
215+
public void FatalFormat(string format, params object?[] args) =>
234216
factory.Write(name, LogLevel.Fatal, string.Format(format, args));
235217
}

src/Tests/LoggerFactoryTests.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Microsoft.Extensions.Logging;
21
using NServiceBus.Logging;
32

43
/// <summary>
@@ -43,7 +42,7 @@ public void GetLogger_with_type_creates_logger_with_type_name()
4342
factory.UseMsFactory(mockFactory);
4443

4544
var loggerFactory = GetLoggingFactory(factory);
46-
var log = loggerFactory.GetLogger(typeof(LoggerFactoryTests));
45+
loggerFactory.GetLogger(typeof(LoggerFactoryTests));
4746

4847
Assert.Single(mockFactory.Loggers);
4948
Assert.True(mockFactory.Loggers.ContainsKey(typeof(LoggerFactoryTests).FullName!));
@@ -57,7 +56,7 @@ public void GetLogger_with_string_creates_logger_with_given_name()
5756
factory.UseMsFactory(mockFactory);
5857

5958
var loggerFactory = GetLoggingFactory(factory);
60-
var log = loggerFactory.GetLogger("MyCustomLogger");
59+
loggerFactory.GetLogger("MyCustomLogger");
6160

6261
Assert.Single(mockFactory.Loggers);
6362
Assert.True(mockFactory.Loggers.ContainsKey("MyCustomLogger"));
@@ -117,25 +116,25 @@ public void GetLogger_called_multiple_times_with_different_types_creates_differe
117116
factory.UseMsFactory(mockFactory);
118117

119118
var loggerFactory = GetLoggingFactory(factory);
120-
var log1 = loggerFactory.GetLogger(typeof(LoggerFactoryTests));
121-
var log2 = loggerFactory.GetLogger(typeof(string));
119+
loggerFactory.GetLogger(typeof(LoggerFactoryTests));
120+
loggerFactory.GetLogger(typeof(string));
122121

123122
Assert.Equal(2, mockFactory.Loggers.Count);
124123
}
125124

126125
// Helper to invoke protected GetLoggingFactory method
127-
static NServiceBus.Logging.ILoggerFactory GetLoggingFactory(MicrosoftLogFactory factory)
126+
static ILoggerFactory GetLoggingFactory(MicrosoftLogFactory factory)
128127
{
129128
var method = typeof(MicrosoftLogFactory).GetMethod(
130129
"GetLoggingFactory",
131-
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
130+
BindingFlags.NonPublic | BindingFlags.Instance);
132131
try
133132
{
134-
return (NServiceBus.Logging.ILoggerFactory)method!.Invoke(factory, null)!;
133+
return (ILoggerFactory)method!.Invoke(factory, null)!;
135134
}
136-
catch (System.Reflection.TargetInvocationException ex)
135+
catch (TargetInvocationException exception)
137136
{
138-
throw ex.InnerException!;
137+
throw exception.InnerException!;
139138
}
140139
}
141140
}

0 commit comments

Comments
 (0)