Skip to content

Commit a50b87b

Browse files
Merge pull request #14 from StuartFerguson/task/#13_loggerredesign
Logger updates
2 parents aad2f59 + 55e6038 commit a50b87b

10 files changed

Lines changed: 352 additions & 74 deletions

Shared.Tests/ExceptionHandlerMiddewareUnitTest.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Shared.Middleware;
1212
using Shouldly;
1313
using Xunit;
14+
using NullLogger = Shared.Logger.NullLogger;
1415

1516
namespace Shared.Tests
1617
{
@@ -21,7 +22,7 @@ public class ExceptionHandlerMiddewareUnitTest
2122
[Fact]
2223
public async void ExceptionHandlerMiddleware_ArgumentNullExceptionThrown_BadRequestHttpStatusCodeReturned()
2324
{
24-
Logger.Initialise(NullLogger.Instance);
25+
Logger.Logger.Initialise(NullLogger.Instance);
2526

2627
ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware((innerHttpContext) =>
2728
throw new ArgumentNullException("TestParam",ExceptionHandlerMiddewareUnitTest.ExceptionMessage));
@@ -40,7 +41,7 @@ public async void ExceptionHandlerMiddleware_ArgumentNullExceptionThrown_BadRequ
4041
[Fact]
4142
public async void ExceptionHandlerMiddleware_InvalidDataExceptionThrown_BadRequestHttpStatusCodeReturned()
4243
{
43-
Logger.Initialise(NullLogger.Instance);
44+
Logger.Logger.Initialise(NullLogger.Instance);
4445

4546
ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware((innerHttpContext) =>
4647
throw new InvalidDataException(ExceptionHandlerMiddewareUnitTest.ExceptionMessage));
@@ -59,7 +60,7 @@ public async void ExceptionHandlerMiddleware_InvalidDataExceptionThrown_BadReque
5960
[Fact]
6061
public async void ExceptionHandlerMiddleware_InvalidOperationExceptionThrown_BadRequestHttpStatusCodeReturned()
6162
{
62-
Logger.Initialise(NullLogger.Instance);
63+
Logger.Logger.Initialise(NullLogger.Instance);
6364

6465
ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware((innerHttpContext) =>
6566
throw new InvalidOperationException(ExceptionHandlerMiddewareUnitTest.ExceptionMessage));
@@ -78,7 +79,7 @@ public async void ExceptionHandlerMiddleware_InvalidOperationExceptionThrown_Bad
7879
[Fact]
7980
public async void ExceptionHandlerMiddleware_FormatExceptionThrown_BadRequestHttpStatusCodeReturned()
8081
{
81-
Logger.Initialise(NullLogger.Instance);
82+
Logger.Logger.Initialise(NullLogger.Instance);
8283

8384
ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware((innerHttpContext) =>
8485
throw new FormatException(ExceptionHandlerMiddewareUnitTest.ExceptionMessage));
@@ -97,7 +98,7 @@ public async void ExceptionHandlerMiddleware_FormatExceptionThrown_BadRequestHtt
9798
[Fact]
9899
public async void ExceptionHandlerMiddleware_NotSupportedExceptionThrown_BadRequestHttpStatusCodeReturned()
99100
{
100-
Logger.Initialise(NullLogger.Instance);
101+
Logger.Logger.Initialise(NullLogger.Instance);
101102

102103
ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware((innerHttpContext) =>
103104
throw new NotSupportedException(ExceptionHandlerMiddewareUnitTest.ExceptionMessage));
@@ -116,7 +117,7 @@ public async void ExceptionHandlerMiddleware_NotSupportedExceptionThrown_BadRequ
116117
[Fact]
117118
public async void ExceptionHandlerMiddleware_NotFoundExceptionThrown_NotFoundHttpStatusCodeReturned()
118119
{
119-
Logger.Initialise(NullLogger.Instance);
120+
Logger.Logger.Initialise(NullLogger.Instance);
120121

121122
ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware((innerHttpContext) =>
122123
throw new NotFoundException(ExceptionHandlerMiddewareUnitTest.ExceptionMessage));
@@ -135,7 +136,7 @@ public async void ExceptionHandlerMiddleware_NotFoundExceptionThrown_NotFoundHtt
135136
[Fact]
136137
public async void ExceptionHandlerMiddleware_NotImplementedExceptionThrown_NotImplementedHttpStatusCodeReturned()
137138
{
138-
Logger.Initialise(NullLogger.Instance);
139+
Logger.Logger.Initialise(NullLogger.Instance);
139140

140141
ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware((innerHttpContext) =>
141142
throw new NotImplementedException(ExceptionHandlerMiddewareUnitTest.ExceptionMessage));
@@ -154,7 +155,7 @@ public async void ExceptionHandlerMiddleware_NotImplementedExceptionThrown_NotIm
154155
[Fact]
155156
public async void ExceptionHandlerMiddleware_OtherExceptionThrown_InternalServerErrorHttpStatusCodeReturned()
156157
{
157-
Logger.Initialise(NullLogger.Instance);
158+
Logger.Logger.Initialise(NullLogger.Instance);
158159

159160
ExceptionHandlerMiddleware middleware = new ExceptionHandlerMiddleware((innerHttpContext) =>
160161
throw new Exception(ExceptionHandlerMiddewareUnitTest.ExceptionMessage));

Shared.Tests/LoggingTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
using System;
22
using System.Collections.Generic;
33
using Microsoft.Extensions.Logging;
4-
using Microsoft.Extensions.Logging.Abstractions;
54
using Moq;
65
using Shared.General;
76
using Shouldly;
87
using Xunit;
98

109
namespace Logging.Tests
1110
{
11+
using Shared.Logger;
12+
1213
public class LoggingTests
1314
{
1415
[Fact]

Shared/Logger/ILogger.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
namespace Shared.Logger
2+
{
3+
using System;
4+
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
5+
6+
/// <summary>
7+
///
8+
/// </summary>
9+
public interface ILogger
10+
{
11+
#region Properties
12+
13+
/// <summary>
14+
/// Gets or sets a value indicating whether this instance is initialised.
15+
/// </summary>
16+
/// <value>
17+
/// <c>true</c> if this instance is initialised; otherwise, <c>false</c>.
18+
/// </value>
19+
Boolean IsInitialised { get; set; }
20+
21+
#endregion
22+
23+
#region Methods
24+
25+
/// <summary>
26+
/// Logs the critical.
27+
/// </summary>
28+
/// <param name="exception">The exception.</param>
29+
void LogCritical(Exception exception);
30+
31+
/// <summary>
32+
/// Logs the debug.
33+
/// </summary>
34+
/// <param name="message">The message.</param>
35+
void LogDebug(String message);
36+
37+
/// <summary>
38+
/// Logs the error.
39+
/// </summary>
40+
/// <param name="exception">The exception.</param>
41+
void LogError(Exception exception);
42+
43+
/// <summary>
44+
/// Logs the information.
45+
/// </summary>
46+
/// <param name="message">The message.</param>
47+
void LogInformation(String message);
48+
49+
/// <summary>
50+
/// Logs the trace.
51+
/// </summary>
52+
/// <param name="message">The message.</param>
53+
void LogTrace(String message);
54+
55+
/// <summary>
56+
/// Logs the warning.
57+
/// </summary>
58+
/// <param name="message">The message.</param>
59+
void LogWarning(String message);
60+
61+
#endregion
62+
}
63+
}
Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,138 @@
1-
using Microsoft.Extensions.Logging;
2-
using System;
3-
4-
namespace Shared.General
1+
namespace Shared.Logger
52
{
3+
using System;
4+
5+
/// <summary>
6+
///
7+
/// </summary>
68
public static class Logger
79
{
8-
#region Private Properties
10+
#region Fields
11+
912
/// <summary>
1013
/// The logger object
1114
/// </summary>
1215
private static ILogger LoggerObject;
16+
1317
#endregion
1418

15-
#region Public Properties
19+
#region Properties
20+
1621
/// <summary>
1722
/// Gets or sets a value indicating whether this instance is initialised.
1823
/// </summary>
1924
/// <value>
2025
/// <c>true</c> if this instance is initialised; otherwise, <c>false</c>.
2126
/// </value>
2227
public static Boolean IsInitialised { get; set; }
28+
2329
#endregion
2430

25-
#region Public Methods
31+
#region Methods
32+
33+
/// <summary>
34+
/// Initialises the specified logger object.
35+
/// </summary>
36+
/// <param name="loggerObject">The logger object.</param>
37+
/// <param name="fileName">Name of the file.</param>
38+
public static void Initialise(NLog.Logger loggerObject,
39+
String fileName)
40+
{
41+
NlogLogger logger = new NlogLogger();
42+
logger.Initialise(loggerObject, fileName);
43+
Logger.Initialise(logger);
44+
}
2645

27-
#region public static void Initialise(ILogger loggerObject)
2846
/// <summary>
2947
/// Initialises the specified logger object.
3048
/// </summary>
3149
/// <param name="loggerObject">The logger object.</param>
50+
/// <exception cref="ArgumentNullException">loggerObject</exception>
3251
public static void Initialise(ILogger loggerObject)
3352
{
34-
LoggerObject = loggerObject ?? throw new ArgumentNullException(nameof(loggerObject));
53+
Logger.LoggerObject = loggerObject ?? throw new ArgumentNullException(nameof(loggerObject));
3554

3655
Logger.IsInitialised = true;
3756
}
38-
#endregion
3957

40-
#region public static void LogTrace(String message)
4158
/// <summary>
42-
/// Logs the trace.
59+
/// Logs the critical.
4360
/// </summary>
44-
/// <param name="message">The message.</param>
45-
public static void LogTrace(String message)
61+
/// <param name="exception">The exception.</param>
62+
public static void LogCritical(Exception exception)
4663
{
47-
ValidateLoggerObject();
64+
Logger.ValidateLoggerObject();
4865

49-
LoggerObject.LogTrace(new EventId(), message);
66+
Logger.LoggerObject.LogCritical(exception);
5067
}
51-
#endregion
5268

53-
#region public static void LogDebug(String message)
5469
/// <summary>
5570
/// Logs the debug.
5671
/// </summary>
5772
/// <param name="message">The message.</param>
5873
public static void LogDebug(String message)
5974
{
60-
ValidateLoggerObject();
75+
Logger.ValidateLoggerObject();
6176

62-
LoggerObject.LogDebug(new EventId(), message);
77+
Logger.LoggerObject.LogDebug(message);
6378
}
64-
#endregion
6579

66-
#region public static void LogInformation(String message)
6780
/// <summary>
68-
/// Logs the information.
81+
/// Logs the error.
6982
/// </summary>
70-
/// <param name="message">The message.</param>
71-
public static void LogInformation(String message)
83+
/// <param name="exception">The exception.</param>
84+
public static void LogError(Exception exception)
7285
{
73-
ValidateLoggerObject();
86+
Logger.ValidateLoggerObject();
7487

75-
LoggerObject.LogInformation(new EventId(), message);
88+
Logger.LoggerObject.LogError(exception);
7689
}
77-
#endregion
7890

79-
#region public static void LogWarning(String message)
8091
/// <summary>
81-
/// Logs the warning.
92+
/// Logs the information.
8293
/// </summary>
8394
/// <param name="message">The message.</param>
84-
public static void LogWarning(String message)
95+
public static void LogInformation(String message)
8596
{
86-
ValidateLoggerObject();
97+
Logger.ValidateLoggerObject();
8798

88-
LoggerObject.LogWarning(new EventId(), message);
99+
Logger.LoggerObject.LogInformation(message);
89100
}
90-
#endregion
91101

92-
#region public static void LogError(Exception exception)
93102
/// <summary>
94-
/// Logs the error.
103+
/// Logs the trace.
95104
/// </summary>
96-
/// <param name="exception">The exception.</param>
97-
public static void LogError(Exception exception)
105+
/// <param name="message">The message.</param>
106+
public static void LogTrace(String message)
98107
{
99-
ValidateLoggerObject();
108+
Logger.ValidateLoggerObject();
100109

101-
LoggerObject.LogError(new EventId(), exception, exception.Message);
110+
Logger.LoggerObject.LogTrace(message);
102111
}
103-
#endregion
104112

105-
#region public static void LogCritical(Exception exception)
106113
/// <summary>
107-
/// Logs the critical.
114+
/// Logs the warning.
108115
/// </summary>
109-
/// <param name="exception">The exception.</param>
110-
public static void LogCritical(Exception exception)
116+
/// <param name="message">The message.</param>
117+
public static void LogWarning(String message)
111118
{
112-
ValidateLoggerObject();
119+
Logger.ValidateLoggerObject();
113120

114-
LoggerObject.LogCritical(new EventId(), exception, exception.Message);
121+
Logger.LoggerObject.LogWarning(message);
115122
}
116-
#endregion
117123

118-
#endregion
119-
120-
#region Private Methods
121-
122-
#region private static void ValidateLoggerObject()
123124
/// <summary>
124125
/// Validates the logger object.
125126
/// </summary>
126127
/// <exception cref="InvalidOperationException">Logger has not been initialised</exception>
127128
private static void ValidateLoggerObject()
128129
{
129-
if (LoggerObject == null)
130+
if (Logger.LoggerObject == null)
130131
{
131132
throw new InvalidOperationException("Logger has not been initialised");
132133
}
133134
}
134-
#endregion
135135

136136
#endregion
137-
138137
}
139-
140-
}
138+
}

0 commit comments

Comments
 (0)