private void EnableWhenDiagnosticsEnablingAuditCategoryDoNotExist(SqlAuditAdapter adapter, DiagnosticSettingsResource currentSettings = null)
        {
            if (adapter.CreateDiagnosticSettings(
                    GetEventHubNameOnCreateOrUpdate(currentSettings),
                    GetEventHubAuthorizationRuleIdOnCreateOrUpdate(currentSettings),
                    GetWorkspaceIdOnCreateOrUpdate(currentSettings),
                    this) == false)
            {
                throw DefinitionsCommon.CreateDiagnosticSettingsException;
            }

            try
            {
                IsGlobalAuditEnabled        = true;
                IsAzureMonitorTargetEnabled = true;
                if (SetAuditingPolicy(adapter) == false)
                {
                    throw DefinitionsCommon.SetAuditingSettingsException;
                }
            }
            catch (Exception)
            {
                try
                {
                    adapter.RemoveFirstDiagnosticSettings(this);
                }
                catch (Exception) { }

                throw;
            }
        }
        private void ChangeWhenMultipleCategoriesAreEnabled(SqlAuditAdapter adapter, DiagnosticSettingsResource settings)
        {
            if (adapter.DisableAuditCategory(this, settings) == false)
            {
                throw DefinitionsCommon.UpdateDiagnosticSettingsException;
            }

            try
            {
                if (AuditState == AuditStateType.Enabled)
                {
                    EnableWhenDiagnosticsEnablingAuditCategoryDoNotExist(adapter, settings);
                }
                else
                {
                    if (ShoudDiagnosticSettingsBeRemovedOnDisabledPolicy(settings))
                    {
                        DisableWhenDiagnosticsEnablingAuditCategoryDoNotExist(adapter);
                    }
                    else
                    {
                        if (adapter.CreateDiagnosticSettings(
                                GetEventHubNameOnDisablePolicy(settings),
                                GetEventHubAuthorizationRuleIdOnDisablePolicy(settings),
                                GetWorkspaceIdOnOnDisablePolicy(settings), this) == false)
                        {
                            throw DefinitionsCommon.CreateDiagnosticSettingsException;
                        }
                    }
                }
            }
            catch (Exception)
            {
                try
                {
                    adapter.EnableAuditCategory(this, settings);
                }
                catch (Exception) { }

                throw;
            }
        }
        private void DisableWhenOnlyAuditCategoryIsEnabled(SqlAuditAdapter adapter, DiagnosticSettingsResource settings, string oldEventHubName, string oldEventHubAuthorizationRuleId, string oldWorkspaceId)
        {
            if (ShoudDiagnosticSettingsBeRemovedOnDisabledPolicy(settings))
            {
                if (adapter.RemoveFirstDiagnosticSettings(this) == false)
                {
                    throw new Exception("Failed to remove Diagnostic Settings");
                }

                try
                {
                    DisableWhenDiagnosticsEnablingAuditCategoryDoNotExist(adapter);
                }
                catch (Exception)
                {
                    try
                    {
                        adapter.CreateDiagnosticSettings(oldEventHubName,
                                                         oldEventHubAuthorizationRuleId, oldWorkspaceId, this);
                    }
                    catch (Exception) { }

                    throw;
                }
            }
            else
            {
                settings.EventHubName = GetEventHubNameOnDisablePolicy(settings);
                settings.EventHubAuthorizationRuleId = GetEventHubAuthorizationRuleIdOnDisablePolicy(settings);
                settings.WorkspaceId = GetWorkspaceIdOnOnDisablePolicy(settings);
                if (adapter.UpdateDiagnosticSettings(settings, this) == false)
                {
                    throw DefinitionsCommon.UpdateDiagnosticSettingsException;
                }
            }
        }