From 669511df0e9adfd80a49244971e3a80d9abae405 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Thu, 22 Jun 2023 00:58:51 +0200 Subject: [PATCH 01/12] removes classType & methodType --- .../FunctionLogging/FunctionLoggingTests.cs | 158 ++++++++++++++++++ .../util/DummyClass.cs | 59 +++++++ .../FunctionLogging/FunctionLogging.cs | 58 ++++++- ....Serilog.Extensions.Logging.Helpers.csproj | 3 +- ...one.Serilog.Extensions.Logging.Helpers.xml | 24 ++- 5 files changed, 289 insertions(+), 13 deletions(-) create mode 100644 src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs create mode 100644 src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs new file mode 100644 index 0000000..97a799f --- /dev/null +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs @@ -0,0 +1,158 @@ +using System; +using jjm.one.Microsoft.Extensions.Logging.Helpers.Tests.util; +using Microsoft.Extensions.Logging; +using Moq; + +namespace jjm.one.Microsoft.Extensions.Logging.Helpers.Tests.FunctionLogging; + +public class FunctionLoggingTests +{ + #region private members + + private readonly Mock _logger; + + private readonly DummyClass _sut; + + #endregion + + #region ctors + + public FunctionLoggingTests() + { + _logger = new Mock(); + + _sut = new DummyClass(_logger.Object); + } + + #endregion + + #region tests + + [Fact] + public void LogFctCallTest1() + { + // arrange + _logger.Setup(x => x.Log(LogLevel.Debug, 0, It.IsAny(), + It.IsAny(), It.IsAny>()!)).Verifiable(); + + // act + _sut.Test1(); + + // assert + _logger.Verify(logger => logger.Log( + It.Is(logLevel => logLevel == LogLevel.Debug), + 0, + It.Is((@o, @t) => + @o.ToString()!.Equals($"Function called: {nameof(DummyClass)} -> {nameof(_sut.Test1)}")), + It.IsAny(), + It.IsAny>()!), + Times.Once); + } + + [Fact] + public void LogFctCallTest2() + { + // arrange + _logger.Setup(x => x.Log(LogLevel.Debug, 0, It.IsAny(), + It.IsAny(), It.IsAny>()!)).Verifiable(); + + // act + _sut.Test2(); + + // assert + _logger.Verify(logger => logger.Log( + It.Is(logLevel => logLevel == LogLevel.Debug), + 0, + It.Is((@o, @t) => + @o.ToString()!.Equals($"Function called: {nameof(DummyClass)} -> {nameof(_sut.Test2)}")), + It.IsAny(), + It.IsAny>()!), + Times.Once); + } + + [Fact] + public void LogFctCallTest3() + { + // arrange + _logger.Setup(x => x.Log(LogLevel.Error, 0, It.IsAny(), + It.IsAny(), It.IsAny>()!)).Verifiable(); + + // act + _sut.Test3(); + + // assert + _logger.Verify(logger => logger.Log( + It.Is(logLevel => logLevel == LogLevel.Error), + 0, + It.Is((@o, @t) => + @o.ToString()!.Equals($"Exception thrown in: {nameof(DummyClass)} -> {nameof(_sut.Test3)}")), + It.IsAny(), + It.IsAny>()!), + Times.Once); + } + + [Fact] + public void LogFctCallTest4() + { + // arrange + _logger.Setup(x => x.Log(LogLevel.Error, 0, It.IsAny(), + It.IsAny(), It.IsAny>()!)).Verifiable(); + + // act + _sut.Test4(); + + // assert + _logger.Verify(logger => logger.Log( + It.Is(logLevel => logLevel == LogLevel.Error), + 0, + It.Is((@o, @t) => + @o.ToString()!.Equals($"Exception thrown in: {nameof(DummyClass)} -> {nameof(_sut.Test4)}\nTestMSG")), + It.IsAny(), + It.IsAny>()!), + Times.Once); + } + + [Fact] + public void LogFctCallTest5() + { + // arrange + _logger.Setup(x => x.Log(LogLevel.Error, 0, It.IsAny(), + It.IsAny(), It.IsAny>()!)).Verifiable(); + + // act + _sut.Test5(); + + // assert + _logger.Verify(logger => logger.Log( + It.Is(logLevel => logLevel == LogLevel.Error), + 0, + It.Is((@o, @t) => + @o.ToString()!.Equals($"Exception thrown in: {nameof(DummyClass)} -> {nameof(_sut.Test5)}")), + It.IsAny(), + It.IsAny>()!), + Times.Once); + } + + [Fact] + public void LogFctCallTest6() + { + // arrange + _logger.Setup(x => x.Log(LogLevel.Error, 0, It.IsAny(), + It.IsAny(), It.IsAny>()!)).Verifiable(); + + // act + _sut.Test6(); + + // assert + _logger.Verify(logger => logger.Log( + It.Is(logLevel => logLevel == LogLevel.Error), + 0, + It.Is((@o, @t) => + @o.ToString()!.Equals($"Exception thrown in: {nameof(DummyClass)} -> {nameof(_sut.Test6)}\nTestMSG")), + It.IsAny(), + It.IsAny>()!), + Times.Once); + } + + #endregion +} \ No newline at end of file diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs new file mode 100644 index 0000000..558b986 --- /dev/null +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs @@ -0,0 +1,59 @@ +using System; +using System.Reflection; +using Microsoft.Extensions.Logging; + +namespace jjm.one.Microsoft.Extensions.Logging.Helpers.Tests.util; + +public class DummyClass +{ + #region private members + + private readonly ILogger _logger; + + #endregion + + #region ctor + + public DummyClass(ILogger logger) + { + _logger = logger; + } + + #endregion + + #region public methods + + public void Test1() + { + _logger.LogFctCall(); + } + + public void Test2() + { + _logger.LogFctCall(GetType(), MethodBase.GetCurrentMethod()); + } + + public void Test3() + { + _logger.LogExcInFctCall(new Exception("Test")); + } + + public void Test4() + { + _logger.LogExcInFctCall(new Exception("Test"), "TestMSG"); + } + + public void Test5() + { + _logger.LogExcInFctCall(new Exception("Test"), GetType(), + MethodBase.GetCurrentMethod()); + } + + public void Test6() + { + _logger.LogExcInFctCall(new Exception("Test"), GetType(), + MethodBase.GetCurrentMethod(), "TestMSG"); + } + + #endregion +} \ No newline at end of file diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs b/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs index be9df22..5aef78a 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs @@ -1,6 +1,7 @@ using Serilog; using Serilog.Events; using System; +using System.Diagnostics; using System.Reflection; namespace jjm.one.Serilog.Extensions.Logging.Helpers @@ -12,24 +13,63 @@ public static class FunctionLogging { /// - /// Log a function call. + /// Log a function/method call. + /// + /// The logger instance. + /// The logging level. Default is . + public static void LogFctCall(this ILogger logger, LogEventLevel level = LogEventLevel.Debug) + { + var method = new StackTrace().GetFrame(1)?.GetMethod(); + + logger.Write(level, "Function called: {ClassName} -> {FctName}", + method?.DeclaringType?.Name, method?.Name); + } + + /// + /// Log a function/method call. /// /// The logger instance. /// The of the calling class. - /// The of the calling method/function. + /// The of the calling function/method. /// The logging level. Default is . - public static void LogFctCall(this ILogger logger, Type? classType, MethodBase? methodType, LogEventLevel level = LogEventLevel.Debug) + public static void LogFctCall(this ILogger logger, Type? classType, MethodBase? methodType, + LogEventLevel level = LogEventLevel.Debug) { - logger.Write(level, "Function called: {ClassName} -> {FctName}", classType?.Name, methodType?.Name); + logger.Write(level, "Function called: {ClassName} -> {FctName}", + classType?.Name, methodType?.Name); } /// - /// Log an exception within a function call. + /// Log an exception within a function/method call. + /// + /// The logger instance. + /// The exception. + /// The message of the exception. + /// The logging level. Default is . + public static void LogExcInFctCall(this ILogger logger, Exception exc, string? msg = null, + LogEventLevel level = LogEventLevel.Error) + { + var method = new StackTrace().GetFrame(1)?.GetMethod(); + + if (string.IsNullOrEmpty(msg)) + { + logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}", + method?.DeclaringType?.Name, method?.Name); + } + else + { + logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}\n" + msg, + method?.DeclaringType?.Name, method?.Name); + } + } + + /// + /// Log an exception within a function/method call. /// /// The logger instance. /// The exception. /// The of the calling class. - /// The of the calling method/function. + /// The of the calling function/method. /// The message of the exception. /// The logging level. Default is . public static void LogExcInFctCall(this ILogger logger, Exception exc, Type? classType, @@ -37,11 +77,13 @@ public static void LogExcInFctCall(this ILogger logger, Exception exc, Type? cla { if (string.IsNullOrEmpty(msg)) { - logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}", classType?.Name, methodType?.Name); + logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}", + classType?.Name, methodType?.Name); } else { - logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}\n" + msg, classType?.Name, methodType?.Name); + logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}\n" + msg, + classType?.Name, methodType?.Name); } } } diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers/jjm.one.Serilog.Extensions.Logging.Helpers.csproj b/src/jjm.one.Serilog.Extensions.Logging.Helpers/jjm.one.Serilog.Extensions.Logging.Helpers.csproj index bcbcbef..d5ec41b 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers/jjm.one.Serilog.Extensions.Logging.Helpers.csproj +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers/jjm.one.Serilog.Extensions.Logging.Helpers.csproj @@ -4,12 +4,13 @@ netstandard2.0;netstandard2.1;net6;net7 disable enable - 1.0.3 + 1.2.0 Jonas Merkle [JJM] © by Jonas Merkle [JJM], 2023. jjm.one.Serilog.Extensions.Logging.Helpers jjm.one.Serilog.Extensions.Logging.Helpers A collection of helper functions for the Serilog logging tool. + extension, csharp, logging, serilog, serilog-extension https://github.com/jjm-one/jjm.one.Serilog.Extensions.Logging.Helpers git https://github.com/jjm.one/jjm-one.Serilog.Extensions.Logging.Helpers.git diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers/jjm.one.Serilog.Extensions.Logging.Helpers.xml b/src/jjm.one.Serilog.Extensions.Logging.Helpers/jjm.one.Serilog.Extensions.Logging.Helpers.xml index e03b71b..2f9997f 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers/jjm.one.Serilog.Extensions.Logging.Helpers.xml +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers/jjm.one.Serilog.Extensions.Logging.Helpers.xml @@ -9,23 +9,39 @@ Static class for all function logging helper extensions. + + + Log a function/method call. + + The logger instance. + The logging level. Default is . + - Log a function call. + Log a function/method call. The logger instance. The of the calling class. - The of the calling method/function. + The of the calling function/method. The logging level. Default is . + + + Log an exception within a function/method call. + + The logger instance. + The exception. + The message of the exception. + The logging level. Default is . + - Log an exception within a function call. + Log an exception within a function/method call. The logger instance. The exception. The of the calling class. - The of the calling method/function. + The of the calling function/method. The message of the exception. The logging level. Default is . From ff5509a3a90cc745b9d1fd9729d5f7bc32dddde6 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Thu, 22 Jun 2023 00:59:27 +0200 Subject: [PATCH 02/12] adds rider files --- .../.idea/.gitignore | 13 +++++++++++++ .../.idea/.name | 1 + .../.idea/encodings.xml | 4 ++++ .../.idea/indexLayout.xml | 8 ++++++++ .../.idea/vcs.xml | 6 ++++++ 5 files changed, 32 insertions(+) create mode 100644 src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/.gitignore create mode 100644 src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/.name create mode 100644 src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/encodings.xml create mode 100644 src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/indexLayout.xml create mode 100644 src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/vcs.xml diff --git a/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/.gitignore b/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/.gitignore new file mode 100644 index 0000000..7c5c466 --- /dev/null +++ b/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/contentModel.xml +/projectSettingsUpdater.xml +/.idea.jjm.one.Serilog.Extensions.Logging.Helpers.iml +/modules.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/.name b/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/.name new file mode 100644 index 0000000..f4c7cd5 --- /dev/null +++ b/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/.name @@ -0,0 +1 @@ +jjm.one.Serilog.Extensions.Logging.Helpers \ No newline at end of file diff --git a/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/encodings.xml b/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/indexLayout.xml b/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/vcs.xml b/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/src/.idea/.idea.jjm.one.Serilog.Extensions.Logging.Helpers/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From de37dfc712a69535608129c12a54e23581613a61 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Thu, 22 Jun 2023 00:59:59 +0200 Subject: [PATCH 03/12] adds tests --- .../FunctionLogging/FunctionLoggingTests.cs | 91 +++++++----------- .../Usings.cs | 1 + ...tensions.Logging.Helpers.Tests.SignKey.snk | Bin 0 -> 596 bytes ...og.Extensions.Logging.Helpers.Tests.csproj | 50 ++++++++++ ...rilog.Extensions.Logging.Helpers.Tests.xml | 8 ++ .../util/DummyClass.cs | 4 +- ...one.Serilog.Extensions.Logging.Helpers.sln | 6 ++ 7 files changed, 103 insertions(+), 57 deletions(-) create mode 100644 src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/Usings.cs create mode 100644 src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.SignKey.snk create mode 100644 src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.csproj create mode 100644 src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs index 97a799f..7b7cb51 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs @@ -1,9 +1,10 @@ using System; -using jjm.one.Microsoft.Extensions.Logging.Helpers.Tests.util; -using Microsoft.Extensions.Logging; +using jjm.one.Serilog.Extensions.Logging.Helpers.Tests.util; using Moq; +using Serilog.Events; +using Serilog; -namespace jjm.one.Microsoft.Extensions.Logging.Helpers.Tests.FunctionLogging; +namespace jjm.one.Serilog.Extensions.Logging.Helpers.Tests.FunctionLogging; public class FunctionLoggingTests { @@ -32,41 +33,33 @@ public FunctionLoggingTests() public void LogFctCallTest1() { // arrange - _logger.Setup(x => x.Log(LogLevel.Debug, 0, It.IsAny(), - It.IsAny(), It.IsAny>()!)).Verifiable(); + _logger.Setup(x => x.Write(LogEventLevel.Debug, + It.IsAny(), It.IsAny())).Verifiable(); // act _sut.Test1(); - + // assert - _logger.Verify(logger => logger.Log( - It.Is(logLevel => logLevel == LogLevel.Debug), - 0, - It.Is((@o, @t) => - @o.ToString()!.Equals($"Function called: {nameof(DummyClass)} -> {nameof(_sut.Test1)}")), - It.IsAny(), - It.IsAny>()!), - Times.Once); + _logger.Verify(logger => logger.Write(LogEventLevel.Debug, + "Function called: {ClassName} -> {FctName}", + nameof(DummyClass), nameof(DummyClass.Test1)), + Times.Once); } [Fact] public void LogFctCallTest2() { // arrange - _logger.Setup(x => x.Log(LogLevel.Debug, 0, It.IsAny(), - It.IsAny(), It.IsAny>()!)).Verifiable(); + _logger.Setup(x => x.Write(LogEventLevel.Debug, + It.IsAny(), It.IsAny())).Verifiable(); // act _sut.Test2(); // assert - _logger.Verify(logger => logger.Log( - It.Is(logLevel => logLevel == LogLevel.Debug), - 0, - It.Is((@o, @t) => - @o.ToString()!.Equals($"Function called: {nameof(DummyClass)} -> {nameof(_sut.Test2)}")), - It.IsAny(), - It.IsAny>()!), + _logger.Verify(logger => logger.Write(LogEventLevel.Debug, + "Function called: {ClassName} -> {FctName}", + nameof(DummyClass), nameof(DummyClass.Test2)), Times.Once); } @@ -74,20 +67,17 @@ public void LogFctCallTest2() public void LogFctCallTest3() { // arrange - _logger.Setup(x => x.Log(LogLevel.Error, 0, It.IsAny(), - It.IsAny(), It.IsAny>()!)).Verifiable(); + _logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny(), + It.IsAny(), It.IsAny())).Verifiable(); // act _sut.Test3(); // assert - _logger.Verify(logger => logger.Log( - It.Is(logLevel => logLevel == LogLevel.Error), - 0, - It.Is((@o, @t) => - @o.ToString()!.Equals($"Exception thrown in: {nameof(DummyClass)} -> {nameof(_sut.Test3)}")), + _logger.Verify(logger => logger.Write(LogEventLevel.Error, It.IsAny(), - It.IsAny>()!), + "Exception thrown in: {ClassName} -> {FctName}", + nameof(DummyClass), nameof(DummyClass.Test3)), Times.Once); } @@ -95,20 +85,17 @@ public void LogFctCallTest3() public void LogFctCallTest4() { // arrange - _logger.Setup(x => x.Log(LogLevel.Error, 0, It.IsAny(), - It.IsAny(), It.IsAny>()!)).Verifiable(); + _logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny(), + It.IsAny(), It.IsAny())).Verifiable(); // act _sut.Test4(); // assert - _logger.Verify(logger => logger.Log( - It.Is(logLevel => logLevel == LogLevel.Error), - 0, - It.Is((@o, @t) => - @o.ToString()!.Equals($"Exception thrown in: {nameof(DummyClass)} -> {nameof(_sut.Test4)}\nTestMSG")), + _logger.Verify(logger => logger.Write(LogEventLevel.Error, It.IsAny(), - It.IsAny>()!), + "Exception thrown in: {ClassName} -> {FctName}\nTestMSG", + nameof(DummyClass), nameof(DummyClass.Test4)), Times.Once); } @@ -116,20 +103,17 @@ public void LogFctCallTest4() public void LogFctCallTest5() { // arrange - _logger.Setup(x => x.Log(LogLevel.Error, 0, It.IsAny(), - It.IsAny(), It.IsAny>()!)).Verifiable(); + _logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny(), + It.IsAny(), It.IsAny())).Verifiable(); // act _sut.Test5(); // assert - _logger.Verify(logger => logger.Log( - It.Is(logLevel => logLevel == LogLevel.Error), - 0, - It.Is((@o, @t) => - @o.ToString()!.Equals($"Exception thrown in: {nameof(DummyClass)} -> {nameof(_sut.Test5)}")), + _logger.Verify(logger => logger.Write(LogEventLevel.Error, It.IsAny(), - It.IsAny>()!), + "Exception thrown in: {ClassName} -> {FctName}", + nameof(DummyClass), nameof(DummyClass.Test5)), Times.Once); } @@ -137,20 +121,17 @@ public void LogFctCallTest5() public void LogFctCallTest6() { // arrange - _logger.Setup(x => x.Log(LogLevel.Error, 0, It.IsAny(), - It.IsAny(), It.IsAny>()!)).Verifiable(); + _logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny(), + It.IsAny(), It.IsAny())).Verifiable(); // act _sut.Test6(); // assert - _logger.Verify(logger => logger.Log( - It.Is(logLevel => logLevel == LogLevel.Error), - 0, - It.Is((@o, @t) => - @o.ToString()!.Equals($"Exception thrown in: {nameof(DummyClass)} -> {nameof(_sut.Test6)}\nTestMSG")), + _logger.Verify(logger => logger.Write(LogEventLevel.Error, It.IsAny(), - It.IsAny>()!), + "Exception thrown in: {ClassName} -> {FctName}\nTestMSG", + nameof(DummyClass), nameof(DummyClass.Test6)), Times.Once); } diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/Usings.cs b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.SignKey.snk b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.SignKey.snk new file mode 100644 index 0000000000000000000000000000000000000000..4bde0bb036f1339630514d26b31edfa4c219264a GIT binary patch literal 596 zcmV-a0;~N80ssI2Bme+XQ$aES1ONa50097@SWq<&rT;Qr$0uO;uHS$_mw=b;1qmKS zNq|{et^66lh9FBMcbU0pZv0eBp1ktLikexAG);LSltRjFU7tqxC3kBc!J&=}SAtvH z+i~J!$TKAANda5^#^F?i6wmx!7Y7xxeKyvdys|bHcG$r{3Gp!eir<|T=UFdQCPAoo z5myRJFXiT5{G=+^$>dq7-^cMe;N6R}v8-;U^;iy-?(TnrEjgon_1&C{F{5;i$;xK@ zgN;pq2%>cg6mGYttavd;W8dk9huLM}ZQOLa!}?WI;F(ima^>&s9b=eVRhKT-q)jC% zgKq(%H1bi3*yWNj9l6fuXa0EY71HXV0^RQQoXOX}qO`Dzipr0cAG2G$oS6948uVg%Wvuj$+$~h`&jQ>%+Wq95%9*~X6pU@22DkWqx zM|y7FXA?zT;BRXu7L-VYCd2QF>sP6V-(%vm57Eb{PrU^dg=`KFJp^R!B*)G4ilNyJ z`1n1546R@yEH)uO&%mMXKDNvA#qtzPFlOz;c>X4+K2^+mY3fQa*tjO$DdTUp + + + net7.0 + disable + enable + 1.2.0 + Jonas Merkle [JJM] + © by Jonas Merkle [JJM], 2023. + jjm.one.Serilog.Extensions.Logging.Helpers.Tests + jjm.one.Serilog.Extensions.Logging.Helpers.Tests + A collection of helper functions for the Serilog logging tool.(Unit-Tests) + extension, csharp, logging, serilog, serilog-extension + https://github.com/jjm-one/jjm.one.Serilog.Extensions.Logging.Helpers + git + https://github.com/jjm-one/jjm.one.Serilog.Extensions.Logging.Helpers.git + True + jjm.one.Serilog.Extensions.Logging.Helpers.Tests.SignKey.snk + false + true + + + + latestmajor + ./jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml + + + latestmajor + ./jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml new file mode 100644 index 0000000..7d224c2 --- /dev/null +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml @@ -0,0 +1,8 @@ + + + + jjm.one.Serilog.Extensions.Logging.Helpers.Tests + + + + diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs index 558b986..54aa77c 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs @@ -1,8 +1,8 @@ using System; using System.Reflection; -using Microsoft.Extensions.Logging; +using Serilog; -namespace jjm.one.Microsoft.Extensions.Logging.Helpers.Tests.util; +namespace jjm.one.Serilog.Extensions.Logging.Helpers.Tests.util; public class DummyClass { diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.sln b/src/jjm.one.Serilog.Extensions.Logging.Helpers.sln index faebebc..d73451f 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers.sln +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.4.33213.308 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "jjm.one.Serilog.Extensions.Logging.Helpers", "jjm.one.Serilog.Extensions.Logging.Helpers\jjm.one.Serilog.Extensions.Logging.Helpers.csproj", "{2F70667F-DFE5-4A77-9CD7-4BC0FA9BA3D6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "jjm.one.Serilog.Extensions.Logging.Helpers.Tests", "jjm.one.Serilog.Extensions.Logging.Helpers.Tests\jjm.one.Serilog.Extensions.Logging.Helpers.Tests.csproj", "{C4C6C89D-E6FA-403F-9FFE-34F3D7E4ED5A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {2F70667F-DFE5-4A77-9CD7-4BC0FA9BA3D6}.Debug|Any CPU.Build.0 = Debug|Any CPU {2F70667F-DFE5-4A77-9CD7-4BC0FA9BA3D6}.Release|Any CPU.ActiveCfg = Release|Any CPU {2F70667F-DFE5-4A77-9CD7-4BC0FA9BA3D6}.Release|Any CPU.Build.0 = Release|Any CPU + {C4C6C89D-E6FA-403F-9FFE-34F3D7E4ED5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C4C6C89D-E6FA-403F-9FFE-34F3D7E4ED5A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C4C6C89D-E6FA-403F-9FFE-34F3D7E4ED5A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C4C6C89D-E6FA-403F-9FFE-34F3D7E4ED5A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From cfd6c52691e53184cc572419346158226c037d4e Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Thu, 22 Jun 2023 01:07:22 +0200 Subject: [PATCH 04/12] updated workflow --- .github/workflows/dotnet.yml | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index e543b78..c2f5244 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -89,6 +89,13 @@ jobs: path: .\.sonar\scanner key: ${{ runner.os }}-sonar-scanner restore-keys: ${{ runner.os }}-sonar-scanner + - name: Cache dotnet-coverage + id: cache-dotnet-coverage + uses: actions/cache@v1 + with: + path: .\.dotnet-coverage\dotnet-coverage + key: ${{ runner.os }}-dotnet-coverage + restore-keys: ${{ runner.os }}-dotnet-coverage - name: Setup dotNET uses: actions/setup-dotnet@v3 with: @@ -99,6 +106,12 @@ jobs: run: | New-Item -Path .\.sonar\scanner -ItemType Directory dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner + - name: Install dotnet-coverage + if: steps.cache-dotnet-coverage.outputs.cache-hit != 'true' + shell: powershell + run: | + New-Item -Path .\.dotnet-coverage\dotnet-coverage -ItemType Directory + dotnet tool update dotnet-coverage --tool-path .\.dotnet-coverage\dotnet-coverage - name: Build and analyze env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any @@ -106,9 +119,11 @@ jobs: shell: powershell run: | cd .\src - .\..\.sonar\scanner\dotnet-sonarscanner begin /k:"jjm-one_jjm.one.Serilog.Extensions.Logging.Helpers" /o:"jjm-one" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" + .\..\.sonar\scanner\dotnet-sonarscanner begin /k:"jjm-one_jjm.one.Serilog.Extensions.Logging.Helpers" /o:"jjm-one" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml dotnet restore - dotnet build --no-restore + dotnet build --no-restore -c Debug + .\..\.dotnet-coverage\dotnet-coverage\dotnet-coverage collect "dotnet test --no-build --verbosity normal" -f xml -o "coverage.xml" + cat coverage.xml .\..\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}" #publish-nuget: From 9692a6743aae0f6d31690002b288cf9d5e0ef875 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Thu, 22 Jun 2023 01:35:13 +0200 Subject: [PATCH 05/12] ci cleanup --- .github/workflows/dotnet.yml | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index c2f5244..ba86e51 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -56,13 +56,6 @@ jobs: run: dotnet build --no-restore -c Release - name: Test run: dotnet test --no-build --verbosity normal - #- name: Upload artifact - # uses: actions/upload-artifact@v3 - # with: - # name: release-buid - # path: | - # ./jjm-one_jjm.one.Serilog.Extensions.Logging.Helpers/bin/Release/*.nupkg - # retention-days: 1 sonarcloud: name: Build and analyze with SonarCloud @@ -126,27 +119,6 @@ jobs: cat coverage.xml .\..\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}" - #publish-nuget: - # name: Publish the NuGet package - # needs: [build-and-test-debug, build-and-test-release, sonarcloud, publish-doc] - # if: github.ref_type == 'tag' && startsWith(github.event.ref, 'refs/tags/version-') - # runs-on: ubuntu-latest - # steps: - # - name: Download a single artifact - # uses: actions/download-artifact@v3 - # with: - # name: release-buid - # - name: Setup dotNET - # uses: actions/setup-dotnet@v3 - # with: - # dotnet-version: 7.x - # - name: Publish the package to nuget.org - # run: | - # ls -la - # #run: dotnet nuget push */bin/Release/*.nupkg -k $NUGET_AUTH_TOKEN -s https://api.nuget.org/v3/index.json - # #env: - # # NUGET_AUTH_TOKEN: ${{ secrets.NUGET_TOKEN }} - publish-doc: name: Publish Documentation needs: [build-and-test-debug] From f8f4fdcc6ebc7598e6b1508ba9a78fd5fa939844 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Thu, 22 Jun 2023 01:43:57 +0200 Subject: [PATCH 06/12] nuget update --- .../jjm.one.Serilog.Extensions.Logging.Helpers.Tests.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.csproj b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.csproj index 4522f75..6ea72e1 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.csproj +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.csproj @@ -30,14 +30,14 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all From 2f87872d9451516538e83212c99cbea972c95734 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Thu, 22 Jun 2023 10:41:39 +0200 Subject: [PATCH 07/12] fixes ci test config --- .github/workflows/dotnet.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index ba86e51..c5f255b 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -55,7 +55,7 @@ jobs: - name: Build run: dotnet build --no-restore -c Release - name: Test - run: dotnet test --no-build --verbosity normal + run: dotnet test --no-build -c Release --verbosity normal sonarcloud: name: Build and analyze with SonarCloud From 81082ff6700dd5d6cc941f45b7b582956ef38349 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Thu, 22 Jun 2023 10:48:19 +0200 Subject: [PATCH 08/12] adds more usage examples to README.md --- README.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1c61e23..80e658a 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ A collection of helper functions for the [Serilog.Extensions.Logging](https://ww - [Status](#status) - [Table of contents](#table-of-contents) - [Nuget Package](#nuget-package) + - [Installing the Nuget Package](#installing-the-nuget-package) - [Usage](#usage) - [Use function logging](#use-function-logging) - [Output of function logging](#output-of-function-logging) @@ -26,6 +27,18 @@ A collection of helper functions for the [Serilog.Extensions.Logging](https://ww You can get the latest version of this software as a nuget package form [nuget.org](https://www.nuget.org/packages/jjm.one.Serilog.Extensions.Logging.Helpers/) +### Installing the Nuget Package + +| Tool | Command/Code | +|----------------------|--------------| +| Package Manager | ```PM> Install-Package jjm.one.Serilog.Extensions.Logging.Helper -Version X.Y.Z``` | +| .NET CLI | ```> dotnet add package jjm.one.Serilog.Extensions.Logging.Helper --version X.Y.Z``` | +| PackageReference | `````` | +| Package CLI | ```> paket add jjm.one.Serilog.Extensions.Logging.Helper --version X.Y.Z``` | +| Script & Interactive | ```> #r "nuget: jjm.one.Serilog.Extensions.Logging.Helper, X.Y.Z"``` | +| Cake as Addin | ```#addin nuget:?package=jjm.one.Serilog.Extensions.Logging.Helper&version=X.Y.Z``` | +| Cake as Tool | ```#tool nuget:?package=jjm.one.Serilog.Extensions.Logging.Helper&version=X.Y.Z``` | + ## Usage ### Use function logging @@ -37,7 +50,10 @@ class MyClass { void MyFancyFunction() { - // log the function call + // log the function call (minimal parameters) + Log.Logger.LogFctCall(); + + // log the function call (full parameters) Log.Logger.LogFctCall(GetType(), MethodBase.GetCurrentMethod(), LogEventLevel.Debug); try { @@ -46,7 +62,10 @@ class MyClass { } catch (Exception exc) { - // Log the exception + // Log the exception (minimal parameters) + Log.Logger.LogExcInFctCall(exc); + + // Log the exception (full parameters) Log.Logger.LogExcInFctCall(exc, GetType(), MethodBase.GetCurrentMethod(), "My custom exception message!", LogEventLevel.Error); } } From ba14254d8f2e15ab346df0df45ac494eebe717d8 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Thu, 22 Jun 2023 11:44:31 +0200 Subject: [PATCH 09/12] updates logging template --- .../FunctionLogging/FunctionLoggingTests.cs | 102 +++++++++++------- ...rilog.Extensions.Logging.Helpers.Tests.xml | 40 +++++++ .../util/DummyClass.cs | 59 ---------- .../FunctionLogging/FunctionLogging.cs | 18 ++-- 4 files changed, 116 insertions(+), 103 deletions(-) delete mode 100644 src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs index 7b7cb51..6b759d4 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/FunctionLogging/FunctionLoggingTests.cs @@ -1,34 +1,39 @@ using System; -using jjm.one.Serilog.Extensions.Logging.Helpers.Tests.util; +using System.Reflection; using Moq; using Serilog.Events; using Serilog; namespace jjm.one.Serilog.Extensions.Logging.Helpers.Tests.FunctionLogging; +/// +/// This class contains the tests for the class. +/// public class FunctionLoggingTests { #region private members private readonly Mock _logger; - - private readonly DummyClass _sut; #endregion #region ctors + /// + /// The default constructor of the class. + /// public FunctionLoggingTests() { _logger = new Mock(); - - _sut = new DummyClass(_logger.Object); } #endregion #region tests + /// + /// 1. test of the LogFctCall function. + /// [Fact] public void LogFctCallTest1() { @@ -37,15 +42,18 @@ public void LogFctCallTest1() It.IsAny(), It.IsAny())).Verifiable(); // act - _sut.Test1(); - + _logger.Object.LogFctCall(); + // assert - _logger.Verify(logger => logger.Write(LogEventLevel.Debug, + _logger.Verify(x => x.Write(LogEventLevel.Debug, "Function called: {ClassName} -> {FctName}", - nameof(DummyClass), nameof(DummyClass.Test1)), + nameof(FunctionLoggingTests), nameof(LogFctCallTest1)), Times.Once); } + /// + /// 2. test of the LogFctCall function. + /// [Fact] public void LogFctCallTest2() { @@ -54,84 +62,106 @@ public void LogFctCallTest2() It.IsAny(), It.IsAny())).Verifiable(); // act - _sut.Test2(); + _logger.Object.LogFctCall(GetType(), MethodBase.GetCurrentMethod()); // assert - _logger.Verify(logger => logger.Write(LogEventLevel.Debug, + _logger.Verify(x => x.Write(LogEventLevel.Debug, "Function called: {ClassName} -> {FctName}", - nameof(DummyClass), nameof(DummyClass.Test2)), + nameof(FunctionLoggingTests), nameof(LogFctCallTest2)), Times.Once); } + /// + /// 1. test of the LogExcInFctCall function. + /// [Fact] - public void LogFctCallTest3() + public void LogExcInFctCallTest1() { // arrange + var exc = new Exception("Test"); _logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny(), It.IsAny(), It.IsAny())).Verifiable(); // act - _sut.Test3(); + _logger.Object.LogExcInFctCall(exc); // assert - _logger.Verify(logger => logger.Write(LogEventLevel.Error, - It.IsAny(), - "Exception thrown in: {ClassName} -> {FctName}", - nameof(DummyClass), nameof(DummyClass.Test3)), + _logger.Verify(x => x.Write(LogEventLevel.Error, + It.Is(e => e == exc), + "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}", + nameof(FunctionLoggingTests), nameof(LogExcInFctCallTest1), + string.Empty), Times.Once); } + /// + /// 2. test of the LogExcInFctCall function. + /// [Fact] - public void LogFctCallTest4() + public void LogExcInFctCallTest2() { // arrange + var exc = new Exception("Test"); _logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny(), It.IsAny(), It.IsAny())).Verifiable(); // act - _sut.Test4(); + _logger.Object.LogExcInFctCall(exc, "TestMSG"); // assert - _logger.Verify(logger => logger.Write(LogEventLevel.Error, - It.IsAny(), - "Exception thrown in: {ClassName} -> {FctName}\nTestMSG", - nameof(DummyClass), nameof(DummyClass.Test4)), + _logger.Verify(x => x.Write(LogEventLevel.Error, + It.Is(e => e == exc), + "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}", + nameof(FunctionLoggingTests), nameof(LogExcInFctCallTest2), + "\nTestMSG"), Times.Once); } + /// + /// 3. test of the LogExcInFctCall function. + /// [Fact] - public void LogFctCallTest5() + public void LogExcInFctCallTest3() { // arrange + var exc = new Exception("Test"); _logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny(), It.IsAny(), It.IsAny())).Verifiable(); // act - _sut.Test5(); + _logger.Object.LogExcInFctCall(exc, GetType(), + MethodBase.GetCurrentMethod()); // assert - _logger.Verify(logger => logger.Write(LogEventLevel.Error, - It.IsAny(), - "Exception thrown in: {ClassName} -> {FctName}", - nameof(DummyClass), nameof(DummyClass.Test5)), + _logger.Verify(x => x.Write(LogEventLevel.Error, + It.Is(e => e == exc), + "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}", + nameof(FunctionLoggingTests), nameof(LogExcInFctCallTest3), + string.Empty), Times.Once); } + /// + /// 4. test of the LogExcInFctCall function. + /// [Fact] - public void LogFctCallTest6() + public void LogExcInFctCallTest4() { // arrange + var exc = new Exception("Test"); _logger.Setup(x => x.Write(LogEventLevel.Debug, It.IsAny(), It.IsAny(), It.IsAny())).Verifiable(); // act - _sut.Test6(); + _logger.Object.LogExcInFctCall(exc, GetType(), + MethodBase.GetCurrentMethod(), "TestMSG"); // assert - _logger.Verify(logger => logger.Write(LogEventLevel.Error, - It.IsAny(), - "Exception thrown in: {ClassName} -> {FctName}\nTestMSG", - nameof(DummyClass), nameof(DummyClass.Test6)), + _logger.Verify(x => x.Write(LogEventLevel.Error, + It.Is(e => e == exc), + "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}", + nameof(FunctionLoggingTests), nameof(LogExcInFctCallTest4), + "\nTestMSG"), Times.Once); } diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml index 7d224c2..1418667 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/jjm.one.Serilog.Extensions.Logging.Helpers.Tests.xml @@ -4,5 +4,45 @@ jjm.one.Serilog.Extensions.Logging.Helpers.Tests + + + This class contains the tests for the class. + + + + + The default constructor of the class. + + + + + 1. test of the LogFctCall function. + + + + + 2. test of the LogFctCall function. + + + + + 1. test of the LogExcInFctCall function. + + + + + 2. test of the LogExcInFctCall function. + + + + + 3. test of the LogExcInFctCall function. + + + + + 4. test of the LogExcInFctCall function. + + diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs b/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs deleted file mode 100644 index 54aa77c..0000000 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers.Tests/util/DummyClass.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Reflection; -using Serilog; - -namespace jjm.one.Serilog.Extensions.Logging.Helpers.Tests.util; - -public class DummyClass -{ - #region private members - - private readonly ILogger _logger; - - #endregion - - #region ctor - - public DummyClass(ILogger logger) - { - _logger = logger; - } - - #endregion - - #region public methods - - public void Test1() - { - _logger.LogFctCall(); - } - - public void Test2() - { - _logger.LogFctCall(GetType(), MethodBase.GetCurrentMethod()); - } - - public void Test3() - { - _logger.LogExcInFctCall(new Exception("Test")); - } - - public void Test4() - { - _logger.LogExcInFctCall(new Exception("Test"), "TestMSG"); - } - - public void Test5() - { - _logger.LogExcInFctCall(new Exception("Test"), GetType(), - MethodBase.GetCurrentMethod()); - } - - public void Test6() - { - _logger.LogExcInFctCall(new Exception("Test"), GetType(), - MethodBase.GetCurrentMethod(), "TestMSG"); - } - - #endregion -} \ No newline at end of file diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs b/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs index 5aef78a..ec5ae69 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs @@ -53,14 +53,15 @@ public static void LogExcInFctCall(this ILogger logger, Exception exc, string? m if (string.IsNullOrEmpty(msg)) { - logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}", - method?.DeclaringType?.Name, method?.Name); + msg = string.Empty; } else { - logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}\n" + msg, - method?.DeclaringType?.Name, method?.Name); + msg = "\n" + msg; } + + logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}", + method?.DeclaringType?.Name, method?.Name, msg); } /// @@ -77,14 +78,15 @@ public static void LogExcInFctCall(this ILogger logger, Exception exc, Type? cla { if (string.IsNullOrEmpty(msg)) { - logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}", - classType?.Name, methodType?.Name); + msg = string.Empty; } else { - logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}\n" + msg, - classType?.Name, methodType?.Name); + msg = "\n" + msg; } + + logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}", + classType?.Name, methodType?.Name, msg); } } } From 7cfb9664b8c7bd6573ade9c33733ed757e2fb2c5 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Thu, 22 Jun 2023 11:47:47 +0200 Subject: [PATCH 10/12] updates namespace --- .../FunctionLogging/FunctionLogging.cs | 143 +++++++++--------- 1 file changed, 71 insertions(+), 72 deletions(-) diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs b/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs index ec5ae69..300d904 100644 --- a/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs +++ b/src/jjm.one.Serilog.Extensions.Logging.Helpers/FunctionLogging/FunctionLogging.cs @@ -4,89 +4,88 @@ using System.Diagnostics; using System.Reflection; -namespace jjm.one.Serilog.Extensions.Logging.Helpers +namespace jjm.one.Serilog.Extensions.Logging.Helpers; + +/// +/// Static class for all function logging helper extensions. +/// +public static class FunctionLogging { + /// - /// Static class for all function logging helper extensions. + /// Log a function/method call. /// - public static class FunctionLogging + /// The logger instance. + /// The logging level. Default is . + public static void LogFctCall(this ILogger logger, LogEventLevel level = LogEventLevel.Debug) { + var method = new StackTrace().GetFrame(1)?.GetMethod(); - /// - /// Log a function/method call. - /// - /// The logger instance. - /// The logging level. Default is . - public static void LogFctCall(this ILogger logger, LogEventLevel level = LogEventLevel.Debug) - { - var method = new StackTrace().GetFrame(1)?.GetMethod(); - - logger.Write(level, "Function called: {ClassName} -> {FctName}", - method?.DeclaringType?.Name, method?.Name); - } + logger.Write(level, "Function called: {ClassName} -> {FctName}", + method?.DeclaringType?.Name, method?.Name); + } - /// - /// Log a function/method call. - /// - /// The logger instance. - /// The of the calling class. - /// The of the calling function/method. - /// The logging level. Default is . - public static void LogFctCall(this ILogger logger, Type? classType, MethodBase? methodType, - LogEventLevel level = LogEventLevel.Debug) + /// + /// Log a function/method call. + /// + /// The logger instance. + /// The of the calling class. + /// The of the calling function/method. + /// The logging level. Default is . + public static void LogFctCall(this ILogger logger, Type? classType, MethodBase? methodType, + LogEventLevel level = LogEventLevel.Debug) + { + logger.Write(level, "Function called: {ClassName} -> {FctName}", + classType?.Name, methodType?.Name); + } + + /// + /// Log an exception within a function/method call. + /// + /// The logger instance. + /// The exception. + /// The message of the exception. + /// The logging level. Default is . + public static void LogExcInFctCall(this ILogger logger, Exception exc, string? msg = null, + LogEventLevel level = LogEventLevel.Error) + { + var method = new StackTrace().GetFrame(1)?.GetMethod(); + + if (string.IsNullOrEmpty(msg)) { - logger.Write(level, "Function called: {ClassName} -> {FctName}", - classType?.Name, methodType?.Name); + msg = string.Empty; } - - /// - /// Log an exception within a function/method call. - /// - /// The logger instance. - /// The exception. - /// The message of the exception. - /// The logging level. Default is . - public static void LogExcInFctCall(this ILogger logger, Exception exc, string? msg = null, - LogEventLevel level = LogEventLevel.Error) + else { - var method = new StackTrace().GetFrame(1)?.GetMethod(); - - if (string.IsNullOrEmpty(msg)) - { - msg = string.Empty; - } - else - { - msg = "\n" + msg; - } - - logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}", - method?.DeclaringType?.Name, method?.Name, msg); + msg = "\n" + msg; } + + logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}", + method?.DeclaringType?.Name, method?.Name, msg); + } - /// - /// Log an exception within a function/method call. - /// - /// The logger instance. - /// The exception. - /// The of the calling class. - /// The of the calling function/method. - /// The message of the exception. - /// The logging level. Default is . - public static void LogExcInFctCall(this ILogger logger, Exception exc, Type? classType, - MethodBase? methodType, string? msg = null, LogEventLevel level = LogEventLevel.Error) + /// + /// Log an exception within a function/method call. + /// + /// The logger instance. + /// The exception. + /// The of the calling class. + /// The of the calling function/method. + /// The message of the exception. + /// The logging level. Default is . + public static void LogExcInFctCall(this ILogger logger, Exception exc, Type? classType, + MethodBase? methodType, string? msg = null, LogEventLevel level = LogEventLevel.Error) + { + if (string.IsNullOrEmpty(msg)) { - if (string.IsNullOrEmpty(msg)) - { - msg = string.Empty; - } - else - { - msg = "\n" + msg; - } - - logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}", - classType?.Name, methodType?.Name, msg); + msg = string.Empty; } + else + { + msg = "\n" + msg; + } + + logger.Write(level, exc, "Exception thrown in: {ClassName} -> {FctName}{CustomMsg}", + classType?.Name, methodType?.Name, msg); } -} +} \ No newline at end of file From 71fd8e314f498ca8b48fb8c7fe4f64faa903cce9 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Tue, 4 Jul 2023 02:10:05 +0200 Subject: [PATCH 11/12] fixing version in doxygen file --- Doxyfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doxyfile b/Doxyfile index 26b91cb..b6231d7 100644 --- a/Doxyfile +++ b/Doxyfile @@ -48,7 +48,7 @@ PROJECT_NAME = jjm.one.Serilog.Extensions.Logging.Helpers # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 1.0.3 +PROJECT_NUMBER = 1.2.0 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a From 66c20b5b5a4780ff48fb9b01e86c71769bf2b925 Mon Sep 17 00:00:00 2001 From: "Jonas Merkle [JJM]" Date: Tue, 4 Jul 2023 02:10:28 +0200 Subject: [PATCH 12/12] removing not needed *.snk file --- ...Serilog.Extensions.Logging.Helpers.SignKey.snk | Bin 596 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/jjm.one.Serilog.Extensions.Logging.Helpers.SignKey.snk diff --git a/src/jjm.one.Serilog.Extensions.Logging.Helpers.SignKey.snk b/src/jjm.one.Serilog.Extensions.Logging.Helpers.SignKey.snk deleted file mode 100644 index be00999984b272404d8cefa1ae2ee22cf17391b9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 596 zcmV-a0;~N80ssI2Bme+XQ$aES1ONa50098m8A|+U1nkFEjTT}oavdtDW4{pWVVzoE zzXOE2{!__qaw{=<@ZkwOv!bQia|J$7jZ5(o#z@i~rqlRkUwxJdw3@d-h~Nxnm6t+BXk)5rb3Tibwo{$+T>E_Ot0SeUDqICc# z+f%ULCVA{hqk{Zb2UDRK%j36y!P#J@SM(Lw3J9DGzKluugG z^7=nFPv%F+C-$=IWZU*Fch@vP2}b-0T9Yr2bgXr{k9Nb|zl;rO#6qS)1`QHDM680D zn#to-9-K}b(u1+$AQ@Vlhfx2k85r?RL@eLzHuaHQo^sNce;lEFkUh?JS?N3H zY6dX%Ww9jL6$;{#a#&=qriR))$Lx9b2Ffk{xIqmQtq|M2UQ9BPY~xKyNrE)l1P1E` zeY$Q?S~K*?`|&AlmZGr~{i_yDGY<-XtYeLXf-jM2wceS5P&2;?Pl!2NUP(lKmCAs5 i;7OU@F8MOqL6vhC#0U9Q{k=~z{q6fHcOd**$d7Qmv=wy#