Skip to content

Commit ba14254

Browse files
committed
updates logging template
1 parent 81082ff commit ba14254

File tree

4 files changed

+116
-103
lines changed

4 files changed

+116
-103
lines changed

src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
using System;
2-
using jjm.one.Serilog.Extensions.Logging.Helpers.Tests.util;
2+
using System.Reflection;
33
using Moq;
44
using Serilog.Events;
55
using Serilog;
66

77
namespace jjm.one.Serilog.Extensions.Logging.Helpers.Tests.FunctionLogging;
88

9+
/// <summary>
10+
/// This class contains the tests for the <see cref="FunctionLogging"/> class.
11+
/// </summary>
912
public class FunctionLoggingTests
1013
{
1114
#region private members
1215

1316
private readonly Mock<ILogger> _logger;
14-
15-
private readonly DummyClass _sut;
1617

1718
#endregion
1819

1920
#region ctors
2021

22+
/// <summary>
23+
/// The default constructor of the <see cref="FunctionLoggingTests"/> class.
24+
/// </summary>
2125
public FunctionLoggingTests()
2226
{
2327
_logger = new Mock<ILogger>();
24-
25-
_sut = new DummyClass(_logger.Object);
2628
}
2729

2830
#endregion
2931

3032
#region tests
3133

34+
/// <summary>
35+
/// 1. test of the LogFctCall function.
36+
/// </summary>
3237
[Fact]
3338
public void LogFctCallTest1()
3439
{
@@ -37,15 +42,18 @@ public void LogFctCallTest1()
3742
It.IsAny<string>(), It.IsAny<object?[]?>())).Verifiable();
3843

3944
// act
40-
_sut.Test1();
41-
45+
_logger.Object.LogFctCall();
46+
4247
// assert
43-
_logger.Verify(logger => logger.Write(LogEventLevel.Debug,
48+
_logger.Verify(x => x.Write(LogEventLevel.Debug,
4449
"Function called: {ClassName} -> {FctName}",
45-
nameof(DummyClass), nameof(DummyClass.Test1)),
50+
nameof(FunctionLoggingTests), nameof(LogFctCallTest1)),
4651
Times.Once);
4752
}
4853

54+
/// <summary>
55+
/// 2. test of the LogFctCall function.
56+
/// </summary>
4957
[Fact]
5058
public void LogFctCallTest2()
5159
{
@@ -54,84 +62,106 @@ public void LogFctCallTest2()
5462
It.IsAny<string>(), It.IsAny<object?[]?>())).Verifiable();
5563

5664
// act
57-
_sut.Test2();
65+
_logger.Object.LogFctCall(GetType(), MethodBase.GetCurrentMethod());
5866

5967
// assert
60-
_logger.Verify(logger => logger.Write(LogEventLevel.Debug,
68+
_logger.Verify(x => x.Write(LogEventLevel.Debug,
6169
"Function called: {ClassName} -> {FctName}",
62-
nameof(DummyClass), nameof(DummyClass.Test2)),
70+
nameof(FunctionLoggingTests), nameof(LogFctCallTest2)),
6371
Times.Once);
6472
}
6573

74+
/// <summary>
75+
/// 1. test of the LogExcInFctCall function.
76+
/// </summary>
6677
[Fact]
67-
public void LogFctCallTest3()
78+
public void LogExcInFctCallTest1()
6879
{
6980
// arrange
81+
var exc = new Exception("Test");
7082
_logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny<Exception>(),
7183
It.IsAny<string>(), It.IsAny<object?[]?>())).Verifiable();
7284

7385
// act
74-
_sut.Test3();
86+
_logger.Object.LogExcInFctCall(exc);
7587

7688
// assert
77-
_logger.Verify(logger => logger.Write(LogEventLevel.Error,
78-
It.IsAny<Exception>(),
79-
"Exception thrown in: {ClassName} -> {FctName}",
80-
nameof(DummyClass), nameof(DummyClass.Test3)),
89+
_logger.Verify(x => x.Write(LogEventLevel.Error,
90+
It.Is<Exception>(e => e == exc),
91+
"Exception thrown in: {ClassName} -> {FctName}{CustomMsg}",
92+
nameof(FunctionLoggingTests), nameof(LogExcInFctCallTest1),
93+
string.Empty),
8194
Times.Once);
8295
}
8396

97+
/// <summary>
98+
/// 2. test of the LogExcInFctCall function.
99+
/// </summary>
84100
[Fact]
85-
public void LogFctCallTest4()
101+
public void LogExcInFctCallTest2()
86102
{
87103
// arrange
104+
var exc = new Exception("Test");
88105
_logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny<Exception>(),
89106
It.IsAny<string>(), It.IsAny<object?[]?>())).Verifiable();
90107

91108
// act
92-
_sut.Test4();
109+
_logger.Object.LogExcInFctCall(exc, "TestMSG");
93110

94111
// assert
95-
_logger.Verify(logger => logger.Write(LogEventLevel.Error,
96-
It.IsAny<Exception>(),
97-
"Exception thrown in: {ClassName} -> {FctName}\nTestMSG",
98-
nameof(DummyClass), nameof(DummyClass.Test4)),
112+
_logger.Verify(x => x.Write(LogEventLevel.Error,
113+
It.Is<Exception>(e => e == exc),
114+
"Exception thrown in: {ClassName} -> {FctName}{CustomMsg}",
115+
nameof(FunctionLoggingTests), nameof(LogExcInFctCallTest2),
116+
"\nTestMSG"),
99117
Times.Once);
100118
}
101119

120+
/// <summary>
121+
/// 3. test of the LogExcInFctCall function.
122+
/// </summary>
102123
[Fact]
103-
public void LogFctCallTest5()
124+
public void LogExcInFctCallTest3()
104125
{
105126
// arrange
127+
var exc = new Exception("Test");
106128
_logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny<Exception>(),
107129
It.IsAny<string>(), It.IsAny<object?[]?>())).Verifiable();
108130

109131
// act
110-
_sut.Test5();
132+
_logger.Object.LogExcInFctCall(exc, GetType(),
133+
MethodBase.GetCurrentMethod());
111134

112135
// assert
113-
_logger.Verify(logger => logger.Write(LogEventLevel.Error,
114-
It.IsAny<Exception>(),
115-
"Exception thrown in: {ClassName} -> {FctName}",
116-
nameof(DummyClass), nameof(DummyClass.Test5)),
136+
_logger.Verify(x => x.Write(LogEventLevel.Error,
137+
It.Is<Exception>(e => e == exc),
138+
"Exception thrown in: {ClassName} -> {FctName}{CustomMsg}",
139+
nameof(FunctionLoggingTests), nameof(LogExcInFctCallTest3),
140+
string.Empty),
117141
Times.Once);
118142
}
119143

144+
/// <summary>
145+
/// 4. test of the LogExcInFctCall function.
146+
/// </summary>
120147
[Fact]
121-
public void LogFctCallTest6()
148+
public void LogExcInFctCallTest4()
122149
{
123150
// arrange
151+
var exc = new Exception("Test");
124152
_logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny<Exception>(),
125153
It.IsAny<string>(), It.IsAny<object?[]?>())).Verifiable();
126154

127155
// act
128-
_sut.Test6();
156+
_logger.Object.LogExcInFctCall(exc, GetType(),
157+
MethodBase.GetCurrentMethod(), "TestMSG");
129158

130159
// assert
131-
_logger.Verify(logger => logger.Write(LogEventLevel.Error,
132-
It.IsAny<Exception>(),
133-
"Exception thrown in: {ClassName} -> {FctName}\nTestMSG",
134-
nameof(DummyClass), nameof(DummyClass.Test6)),
160+
_logger.Verify(x => x.Write(LogEventLevel.Error,
161+
It.Is<Exception>(e => e == exc),
162+
"Exception thrown in: {ClassName} -> {FctName}{CustomMsg}",
163+
nameof(FunctionLoggingTests), nameof(LogExcInFctCallTest4),
164+
"\nTestMSG"),
135165
Times.Once);
136166
}
137167

src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ public static void LogExcInFctCall(this ILogger logger, Exception exc, string? m
5353

5454
if (string.IsNullOrEmpty(msg))
5555
{
56-
logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}",
57-
method?.DeclaringType?.Name, method?.Name);
56+
msg = string.Empty;
5857
}
5958
else
6059
{
61-
logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}\n" + msg,
62-
method?.DeclaringType?.Name, method?.Name);
60+
msg = "\n" + msg;
6361
}
62+
63+
logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}",
64+
method?.DeclaringType?.Name, method?.Name, msg);
6465
}
6566

6667
/// <summary>
@@ -77,14 +78,15 @@ public static void LogExcInFctCall(this ILogger logger, Exception exc, Type? cla
7778
{
7879
if (string.IsNullOrEmpty(msg))
7980
{
80-
logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}",
81-
classType?.Name, methodType?.Name);
81+
msg = string.Empty;
8282
}
8383
else
8484
{
85-
logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}\n" + msg,
86-
classType?.Name, methodType?.Name);
85+
msg = "\n" + msg;
8786
}
87+
88+
logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}",
89+
classType?.Name, methodType?.Name, msg);
8890
}
8991
}
9092
}

0 commit comments

Comments
 (0)