Error Logging using D365 CE Plugin

Requirement
Error logging using D365 CE Plugin.

Errorlogging5

Details
Let’s see what options we can use for the error logging.

ITracing Service
First option that we have for the error logging in D365 CE plugins is using ITracing service. To usethis option first we need to make sure we enable below setting in D365 CE environment.
Errorlogging1

Once this option is enabled D365 CE store plugin tracing which we can refer from
Errorlogging2

While writing plugin code we can use tracing service like below for logging
Errorlogging3

To review error details we can also use Plugin Trace Viewer from XrmToolBox.

Using Custom Entity
Other option that we can use to store error log in the custom entity. We can create a custom entity and add required field and store information from the plugin. When we want to capture data in the custom entities, it depends on how we want to register our plugin, synchronous or asynchronous and this is very important. let’s see how

Synchronous Plugin
When we write a synchronous plugin it runs under transaction, to understand this simply let’s take an example, let’s say we want to create record of two entity A and B based on some logic in our plugin, so we wrote plugin and while executing it created entity record A and while creating entity record B , we got some error so it will rollback all the changes done by this plugin execution which means it will remove the entity record A which was created under this plugin execution.

Which simply means if we got some error we can’t store error details in the custom entity because that will be rollbacked correct ? actually it depends if you want to throw error to the user or not. let’s see this with example.

I have this below sample code where I am updating some field on the account create and I have used a wrong field name which means it will throw error and under the catch block, I am writing error details to my custom entity and after that throwing error to user.

try
            {
                if (pluginContext.InputParameters.Contains("Target") && pluginContext.InputParameters["Target"] is Entity)
                {
                    Entity accountEntity = (Entity)pluginContext.InputParameters["Target"];
                    //wrong field name
                    Entity _accountupdate = new Entity("account");
                    _accountupdate.Id = accountEntity.Id;
                    _accountupdate["him_wrongattribute"] = "This is test2";
                    orgService.Update(_accountupdate);
                }

                }

            catch (Exception ex)
            {
                tracingService.Trace("Writing error message from Exception");
                Entity _error = new Entity("him_errorlog");
                _error["him_name"] = "Demo 2";
                _error["him_description"] = ex.Message;
                orgService.Create(_error);
                tracingService.Trace("Writing error writing completed from Exception");
                throw new InvalidPluginExecutionException("Error Occurred while Creating Account");
            }

And I have registered this plugin as synchronous and when I try to create account entity record I can see the trace generated like below:
Errorlogging4

But there is no record in my custom entity as they are rollbacked by the plugin when we throw error, now in the same code if I comment the last line like below where I was throwing error and update my plugin assembly.

  //throw new InvalidPluginExecutionException("Error Occurred while Creating Account");

Even though this is synchronous plugin it won’t rollback changes as I am capturing error but not throwing it, so my custom entity will store standard message like “‘account’ entity doesn’t contain attribute with Name = ‘him_wrongattribute’ and NameMapping = ‘Logical'”

Asynchronous Plugin
Asynchronous plugin don’t run under transaction so it won’t rollback your changes so you can store your data in your custom entity.

More reference: https://docs.microsoft.com/en-us/powerapps/developer/data-platform/logging-tracing

Summary
We discussed how we can do error logging in the plugin and how it matters if you are writing synchronous plugin or asynchronous plugin.

Hope it will help someone !!
Keep learning and Keep Sharing !!

Leave a Reply

Your email address will not be published. Required fields are marked *