From 1d3543d089f4e52064e073a246891f0d8fc772c5 Mon Sep 17 00:00:00 2001 From: Ahmed Elsawalhy Date: Sat, 2 Oct 2021 22:40:40 +0300 Subject: [PATCH] Added: log execution end attribute. --- CrmLogger.Fody/CrmLogger.Fody.nuspec | 5 +++-- CrmLogger.Fody/ModuleWeaver.cs | 19 +++++++++++++++++++ README.md | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CrmLogger.Fody/CrmLogger.Fody.nuspec b/CrmLogger.Fody/CrmLogger.Fody.nuspec index 080b688..eb392e3 100644 --- a/CrmLogger.Fody/CrmLogger.Fody.nuspec +++ b/CrmLogger.Fody/CrmLogger.Fody.nuspec @@ -3,7 +3,7 @@ CrmLogger.Fody - 1.5.1 + 1.5.2 Ahmed Elsawalhy Ahmed Elsawalhy https://yagasoft.com @@ -20,7 +20,8 @@ - * 1.5.1 + * 1.5.2 + Added: log execution end attribute Update: upgraded NuGet packages to latest supported * 1.4.4 Fixed: validate that the log object is accessible diff --git a/CrmLogger.Fody/ModuleWeaver.cs b/CrmLogger.Fody/ModuleWeaver.cs index faca168..f8c1056 100644 --- a/CrmLogger.Fody/ModuleWeaver.cs +++ b/CrmLogger.Fody/ModuleWeaver.cs @@ -33,6 +33,7 @@ public class ModuleWeaver public MethodReference ParamsMethod; public MethodReference ExceptionMethod; public MethodReference EndMethod; + public MethodReference EndExecMethod; public TypeReference String => ModuleDefinition.TypeSystem.String; public TypeReference Object => ModuleDefinition.TypeSystem.Object; @@ -237,6 +238,10 @@ public void Init() .FindMethod("LogFunctionEnd", "LogEntry", "IExecutionContext", "String", "String", "String", "Int32")); EndMethod = ModuleDefinition.ImportReference(EndMethod); + EndExecMethod = CommonModuleDefinition.ImportReference(LoggerTypeDefinition + .FindMethod("LogExecutionEndSuccessFlush", "LogEntry", "IExecutionContext", "String", "String", "String", "Int32")); + EndExecMethod = ModuleDefinition.ImportReference(EndExecMethod); + exceptionVariable = new VariableDefinition(ModuleDefinition.ImportReference(ExceptionType)); } @@ -366,6 +371,15 @@ private void Inject() var endInstructions = new List(); endInstructions.AddRange(GetEndInstructions()); + + var isMethodLogExecEndAttributeExist = Method.CustomAttributes.ContainsAttribute("Yagasoft.Libraries.Common.LogExecEndAttribute"); + + if (isMethodLogExecEndAttributeExist) + { + LogInfo($">> Method is set to end execution."); + endInstructions.AddRange(GetExecEndInstructions()); + } + var finallyInstruction = Instruction.Create(OpCodes.Endfinally); endInstructions.Add(finallyInstruction); endInstructions.Reverse(); @@ -560,6 +574,11 @@ private IEnumerable AddWriteStartEnd(MethodReference writeMethod, b yield return Instruction.Create(IsInstanceLogger ? OpCodes.Callvirt : OpCodes.Call, writeMethod); } + private IEnumerable GetExecEndInstructions() + { + return AddWriteStartEnd(EndExecMethod, false); + } + protected List GetReturnValueInstructions(VariableDefinition returnVariable) { var instructions = new List(); diff --git a/README.md b/README.md index 73653af..ffeaa3e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Join the chat at https://gitter.im/yagasoft/DynamicsCrm-CrmLogger.Fody](https://badges.gitter.im/yagasoft/DynamicsCrm-CrmLogger.Fody.svg)](https://gitter.im/yagasoft/DynamicsCrm-CrmLogger.Fody?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -### Version: 1.5.1 +### Version: 1.5.2 --- NuGet package that extremely simplifies logging function entry, exit, parameter and return values. Used with [DynamicsCrm-CrmLogger](https://github.com/yagasoft/DynamicsCrm-CrmLogger) solution.