📜 ⬆️ ⬇️

MS Dynamics CRM and PostSharp

Greetings, Dear Habrochityateli

In this article I want to talk about the results of my research on the applicability of PostSharp to MS Dynamics CRM in terms of logging.


So, there is:

')
What I want: I want logging (trite, but logging).

I decided that I will try to apply logging to the plugin for MS Dynamics CRM.

From logging I want the following:



First, create a project for our plugin:



Load the nuget package with NLog:



Do not forget to pick up the PostSharp itself:



We also hook up the CRM SDK assemblies.

Now the most interesting: where to store the settings of the logger?
Frankly, for me it is still an open question.
Only three options come to mind:


Each of these options has its drawbacks and positives.
In my opinion, the most successful option is the entity with the settings of the logger, but in this case you have to pull out this entity every time, since caching the settings is not a very good solution (the plug-in can run for a long time, and I can change the settings at this point).

But in this case, the task I have more familiarization, so we went further.

Let's write this simple plugin:

namespace LoggedPlugin { public class TestPlugin:IPlugin { [Logging] private string SimpleMethod(string input) { return "Hello CRM"; } [Logging] private void MethodThrowsException() { throw new NotImplementedException(); } public void Execute(IServiceProvider serviceProvider) { SimpleMethod("Hi CRM"); MethodThrowsException(); } } } 


Aspect we will have the following code:

 namespace LoggedPlugin.Logging { [Serializable] public class LoggingAttribute : OnMethodBoundaryAspect { public override void OnException(MethodExecutionArgs args) { Logger.Instance.Error(" ",args.Exception); } public override void OnEntry(MethodExecutionArgs args) { Logger.Instance.Trace(string.Format("{0}.{1}:  .", args.Method.DeclaringType.FullName, args.Method.Name)); var argumentInfos = args.Method.GetParameters(); for (var index = 0; index < args.Arguments.Count; index++) { var argument = args.Arguments[index]; Logger.Instance.Trace(string.Format("{0}:{1}", argumentInfos[index].Name, argument)); } } public override void OnSuccess(MethodExecutionArgs args) { Logger.Instance.Trace(string.Format("{0}.{1}:  .", args.Method.DeclaringType.FullName, args.Method.Name)); } } } 


Well, actually logger:
 namespace LoggedPlugin.Logging { public sealed class Logger { private static volatile Logger _instance; private static readonly object _syncRoot = new object(); private readonly NLog.Logger _logger; private Logger() { var config = new LoggingConfiguration(); var fileTarget = new FileTarget(); config.AddTarget("file",fileTarget); fileTarget.FileName = @"C:\logs\log.txt"; fileTarget.Layout = @"${date:format=[HH\:mm\:ss.fff]} ${level:uppercase=true} t[${threadid}] ${message} ${exception:format=ToString}"; var loggingRule = new LoggingRule("*", LogLevel.Trace, fileTarget); config.LoggingRules.Add(loggingRule); LogManager.Configuration = config; _logger = LogManager.GetLogger("FileLogger"); } public static Logger Instance { get { if (_instance == null) { lock (_syncRoot) { if (_instance == null) _instance = new Logger(); } } return _instance; } } public void Trace(string message) { _logger.Trace(message); } public void Error(string message, Exception e) { _logger.Error(message,e); } } } 


Logger is with a hardcode, but more is not needed - for academic purposes!

Next we sign the build, build it, and merge the three builds using the ILMerge utility:
LoggedPlugin.dll, NLog.dll and PostSharp.dll.

Then we publish the result in CRM:



I hung this plugin to create a lead.

After creating the lead, I saw this result:

================================================= =======
[22: 46: 51.816] TRACE t [31] LoggedPlugin.TestPlugin.SimpleMethod: Started method.
[22: 46: 51.863] TRACE t [31] input: Hi CRM
[22: 46: 51.863] TRACE t [31] LoggedPlugin.TestPlugin.SimpleMethod: Completed method.
[22: 46: 51.863] TRACE t [31] LoggedPlugin.TestPlugin.MethodThrowsException: The method started.
[22: 46: 51.863] ERROR t [31] The System.NotImplementedException has crashed: The method or operation is not implemented.
at LoggedPlugin.TestPlugin.MethodThrowsException ()

================================================= =======

Source: https://habr.com/ru/post/251491/


All Articles