简体   繁体   中英

CRM 2013 how to create entity when error

I create custom error logger in CRM 2013 have functionality to save error information into CRM entity. I debug my code and find that my code works well. But the problem is when CRM rollback the transaction, the log entity also disappear. I want to know is it possible to create entity on catch block and still throw that error?

public void Execute(IServiceProvider serviceProvider)
        {
            try
            {
                ...
            }
            catch (Exception ex)
            {
                     IPluginExecutionContext context =
                  (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.
                    GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(Guid.Empty);

                var log = new Log
                {
                    Message = ex.Message
                };

                service.Create(log);
                throw;
            }
        }

I found the other way to solve this issue. We can create new service to create new transaction outside the transaction being failed. Here some snippet if you want to do the same:

     try
            {
                ...
            }
            catch (Exception ex)
            {

             var HttpCurrentContext = HttpContext.Current;
           var  UrlBase = HttpCurrentContext.Request.Url.Host;
           string httpUrl = @"http://";
            if (HttpCurrentContext.Request.IsLocal)
            {
                UrlBase += ":" + HttpCurrentContext.Request.Url.Port;
            }
                  if (!UrlBase.Contains(httpUrl))
            {
                UrlBase = httpUrl + UrlBase;
            }
            var UriBase = UriBuilder(UrlBase.ToLowerInvariant().Trim() + "/xrmservices/2011/organization.svc").Uri;
IServiceConfiguration<IOrganizationService> orgConfigInfo =
               ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(UriBase);
            var creds = new ClientCredentials();
            using (_serviceProxy = new OrganizationServiceProxy(orgConfigInfo, creds))
            {

                // This statement is required to enable early-bound type support.
                _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                _service = (IOrganizationService)_serviceProxy;

                 var log = new Log
                {
                    Message = ex.Message
                };

                _service.Create(NewLog);
         }
                throw;
            }

Essentially, no. You cannot prevent that an exception rolls back the transaction. See a similar question on StackOverflow .

A common approach is to create a separate logging service that can store logs outside of the database transaction.

Btw Dynamics CRM 2015 spring release introduces the capability to store logs regardless if your plugin is participating in a database transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM