MS Dynamics CRM and PostSharp
Greetings, Dear Host Readers
In this article I want to tell about the results of my research regarding the applicability of PostSharp to MS Dynamics CRM in terms of logging.
So there is:
What I want: I want to log in (corny, but namely logging).
I decided that I would try to apply logging to the plugin for MS Dynamics CRM.
From logging I want the following:
First, create a project for our plugin:

Upload a nuget package with NLog:

Do not forget to pick up PostSharp itself:

We also hook up CRM SDK assemblies.
Now the most interesting: where to store the settings of the logger?
Frankly, for me this is still an open question.
Only three options come to mind:
Each of these options has its drawbacks and positive aspects.
In my opinion, the most successful option is an entity with the settings of the logger, but in this case you will have to pull out this entity each time, since caching the settings is not an entirely successful solution (the plugin can take a long time to execute, but I can change the settings at that moment).
But in this case, my task is more familiarization, so we went further.
Let's write such a simple plug-in:
The aspect with us will have the following code:
Well, actually the logger:
This logger is with a hardcode, but no more is needed - for academic purposes!
Then we sign the assembly, build it and hold three assemblies 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: The method started.
[22: 46: 51.863] TRACE t [31] input: Hi CRM
[22: 46: 51.863] TRACE t [31] LoggedPlugin.TestPlugin.SimpleMethod: Method completed.
[22: 46: 51.863] TRACE t [31] LoggedPlugin.TestPlugin.MethodThrowsException: The method started.
[22: 46: 51.863] ERROR t [31] System.NotImplementedException failed: The method or operation is not implemented.
at LoggedPlugin.TestPlugin.MethodThrowsException ()
========================================== ================
In this article I want to tell about the results of my research regarding the applicability of PostSharp to MS Dynamics CRM in terms of logging.
So there is:
- MS Dynamics CRM 2013
- Nlog
- Postsharp
What I want: I want to log in (corny, but namely logging).
I decided that I would try to apply logging to the plugin for MS Dynamics CRM.
From logging I want the following:
- Log the method that was called
- Log input arguments
- Log when the method has been completed
- Log exceptions
First, create a project for our plugin:
Upload a nuget package with NLog:
Do not forget to pick up PostSharp itself:
We also hook up CRM SDK assemblies.
Now the most interesting: where to store the settings of the logger?
Frankly, for me this is still an open question.
Only three options come to mind:
- Hardcode is a bad option
- In the case of plugins, in the plugin configuration
- Create an entity to store settings
Each of these options has its drawbacks and positive aspects.
In my opinion, the most successful option is an entity with the settings of the logger, but in this case you will have to pull out this entity each time, since caching the settings is not an entirely successful solution (the plugin can take a long time to execute, but I can change the settings at that moment).
But in this case, my task is more familiarization, so we went further.
Let's write such a simple plug-in:
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();
}
}
}
The aspect with us 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 the 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);
}
}
}
This logger is with a hardcode, but no more is needed - for academic purposes!
Then we sign the assembly, build it and hold three assemblies 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: The method started.
[22: 46: 51.863] TRACE t [31] input: Hi CRM
[22: 46: 51.863] TRACE t [31] LoggedPlugin.TestPlugin.SimpleMethod: Method completed.
[22: 46: 51.863] TRACE t [31] LoggedPlugin.TestPlugin.MethodThrowsException: The method started.
[22: 46: 51.863] ERROR t [31] System.NotImplementedException failed: The method or operation is not implemented.
at LoggedPlugin.TestPlugin.MethodThrowsException ()
========================================== ================