internal static void LogEndProcessingMailboxEvent(Guid activityId, AssistantBase assistant, List <KeyValuePair <string, object> > customData, Guid mailboxGuid, string mailboxDisplayNameTracingOnlyUsage, TimeBasedDatabaseJob job = null)
 {
     AssistantsLog.InternalLogAssistantEvent(activityId, assistant, AssistantsEventType.EndProcessingMailbox, customData, mailboxGuid);
     if (job != null)
     {
         AssistantsLog.LogMailboxSlaEvent(assistant, mailboxGuid, mailboxDisplayNameTracingOnlyUsage, job, MailboxSlaEventType.EndProcessingMailbox, MailboxSlaFilterReasonType.None, null);
     }
 }
        internal static void LogStartProcessingMailboxEvent(Guid activityId, AssistantBase assistant, MapiEvent mapiEvent, Guid mailboxGuid)
        {
            ArgumentValidator.ThrowIfNull("mapiEvent", mapiEvent);
            List <KeyValuePair <string, object> > customData = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("MapiEvent", AssistantsLog.FormatMapiEvent(mapiEvent))
            };

            AssistantsLog.InternalLogAssistantEvent(activityId, assistant, AssistantsEventType.StartProcessingMailbox, customData, mailboxGuid);
        }
        protected override void InternalLogActivityEvent(IActivityScope activityScope, ActivityEventType eventType)
        {
            object        userState     = activityScope.UserState;
            AssistantBase assistantBase = null;
            string        assistantShortName;

            if (activityScope.ActivityType == ActivityType.Global)
            {
                assistantShortName = "GlobalActivity";
            }
            else
            {
                SystemTaskBase systemTaskBase = userState as SystemTaskBase;
                if (systemTaskBase != null)
                {
                    assistantShortName = systemTaskBase.Workload.Id;
                }
                else
                {
                    AssistantBase assistantBase2 = userState as AssistantBase;
                    if (assistantBase2 == null)
                    {
                        return;
                    }
                    assistantBase      = assistantBase2;
                    assistantShortName = assistantBase.NonLocalizedName;
                }
            }
            AssistantsEventType eventType2;

            switch (eventType)
            {
            case ActivityEventType.SuspendActivity:
                eventType2 = AssistantsEventType.SuspendActivity;
                goto IL_92;

            case ActivityEventType.EndActivity:
                eventType2 = AssistantsEventType.EndActivity;
                goto IL_92;
            }
            base.SafeTraceDebug(0L, "Skip logging ActivityEvent '{0}'.", new object[]
            {
                eventType
            });
            return;

IL_92:
            List <KeyValuePair <string, object> > customData = WorkloadManagementLogger.FormatWlmActivity(activityScope, true);

            AssistantsLog.InternalLogRow(activityScope.ActivityId, assistantShortName, assistantBase, eventType2, customData, Guid.Empty);
        }
        private static void LogDatabaseSlaEvent(AssistantBase assistant, DatabaseSlaEventType eventType, Exception exception = null)
        {
            string text         = "Unknown";
            string databaseName = "Unknown";

            if (assistant != null)
            {
                databaseName = ((assistant.DatabaseInfo == null) ? "Unknown" : assistant.DatabaseInfo.DatabaseName);
                text         = assistant.NonLocalizedName;
            }
            MailboxAssistantsSlaReportLogFactory.MailboxAssistantsDatabaseSlaLog mailboxAssistantsDatabaseSlaLog = MailboxAssistantsSlaReportLogFactory.GetLogInstance(text, SlaLogType.DatabaseSlaLog) as MailboxAssistantsSlaReportLogFactory.MailboxAssistantsDatabaseSlaLog;
            if (mailboxAssistantsDatabaseSlaLog != null)
            {
                mailboxAssistantsDatabaseSlaLog.LogDatabaseEvent(text, databaseName, eventType, exception);
            }
        }
        private static void LogMailboxSlaEvent(AssistantBase assistant, Guid mailboxGuid, string mailboxDisplayNameTracingOnlyUsage, TimeBasedDatabaseJob job, MailboxSlaEventType eventType, MailboxSlaFilterReasonType reason = MailboxSlaFilterReasonType.None, Exception exception = null)
        {
            string text         = "Unknown";
            string databaseName = "Unknown";
            string jobId        = string.Empty;
            MailboxSlaRequestType requestType = MailboxSlaRequestType.Unknown;

            if (assistant != null)
            {
                databaseName = ((assistant.DatabaseInfo == null) ? "Unknown" : assistant.DatabaseInfo.DatabaseName);
                text         = assistant.NonLocalizedName;
            }
            if (job != null)
            {
                jobId       = job.StartTime.ToString("O");
                requestType = ((job is TimeBasedDatabaseWindowJob) ? MailboxSlaRequestType.Scheduled : MailboxSlaRequestType.OnDemand);
            }
            MailboxAssistantsSlaReportLogFactory.MailboxAssistantsSlaReportLog logInstance = MailboxAssistantsSlaReportLogFactory.GetLogInstance(text, SlaLogType.MailboxSlaLog);
            if (logInstance != null)
            {
                logInstance.LogMailboxEvent(text, databaseName, jobId, requestType, mailboxGuid, mailboxDisplayNameTracingOnlyUsage, eventType, reason, exception);
            }
        }
        private static void InternalLogRow(Guid activityId, string assistantShortName, AssistantBase assistant, AssistantsEventType eventType, List <KeyValuePair <string, object> > customData, Guid mailboxGuid)
        {
            if (!AssistantsLog.Instance.Enabled)
            {
                AssistantsLog.Instance.SafeTraceDebug(0L, "Mailbox assistant log is disabled, skip writing to the log file.", new object[0]);
                return;
            }
            if (AssistantsLog.LogDisabledAssistants.Contains(assistantShortName))
            {
                AssistantsLog.Instance.SafeTraceDebug(0L, "Mailbox assistant '{0}' is disabled for logging, skip writing to the log file.", new object[]
                {
                    assistantShortName
                });
                return;
            }
            string text = string.Empty;

            if (assistant != null && assistant.DatabaseInfo != null)
            {
                text = assistant.DatabaseInfo.DatabaseName;
            }
            LogRowFormatter logRowFormatter = new LogRowFormatter(AssistantsLog.Instance.LogSchema);

            if (AssistantsLog.Instance.IsDebugTraceEnabled)
            {
                string text2 = string.Empty;
                if (customData != null)
                {
                    bool flag;
                    text2 = LogRowFormatter.FormatCollection(customData, out flag);
                }
                AssistantsLog.Instance.SafeTraceDebug(0L, "Start writing row to mailbox assistant log: ServerName='{0}', Location='{1}', AssistantName='{2}', ActivityId='{3}', TargetObject='{4}', Event='{5}', CustomData='{6}'", new object[]
                {
                    AssistantsLog.Instance.ServerName,
                    text,
                    assistantShortName,
                    activityId,
                    mailboxGuid,
                    AssistantsLog.stringDictionary[eventType],
                    text2
                });
            }
            logRowFormatter[1] = AssistantsLog.Instance.ServerName;
            logRowFormatter[3] = assistantShortName;
            logRowFormatter[6] = AssistantsLog.stringDictionary[eventType];
            logRowFormatter[2] = text;
            logRowFormatter[7] = customData;
            logRowFormatter[5] = ((mailboxGuid == Guid.Empty) ? string.Empty : mailboxGuid.ToString("D"));
            logRowFormatter[4] = ((activityId == Guid.Empty) ? string.Empty : activityId.ToString("D"));
            AssistantsLog.Append(logRowFormatter);
            AssistantsLog.Instance.SafeTraceDebug(0L, "The above row is written to mailbox assistant log successfully.", new object[0]);
        }
        private static void InternalLogAssistantEvent(Guid activityId, AssistantBase assistant, AssistantsEventType eventType, List <KeyValuePair <string, object> > customData, Guid mailboxGuid)
        {
            string assistantShortName = (assistant == null) ? "Unknown" : assistant.NonLocalizedName;

            AssistantsLog.InternalLogRow(activityId, assistantShortName, assistant, eventType, customData, mailboxGuid);
        }
 internal static void LogDriverNotStartedEvent(string assistantName, AssistantBase assistant)
 {
     AssistantsLog.InternalLogRow(Guid.Empty, assistantName, null, AssistantsEventType.DriverNotStarted, null, Guid.Empty);
     AssistantsLog.LogDatabaseSlaEvent(assistant, DatabaseSlaEventType.DatabaseIsStopped, null);
 }
        internal static void LogEndGetMailboxesEvent(Guid activityId, string assistantName, int numberOfMailboxesToProcess, AssistantBase assistant)
        {
            List <KeyValuePair <string, object> > customData = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("MailboxesToProcess", numberOfMailboxesToProcess)
            };

            AssistantsLog.InternalLogRow(activityId, assistantName, null, AssistantsEventType.EndGetMailboxes, customData, Guid.Empty);
            AssistantsLog.LogDatabaseSlaEvent(assistant, DatabaseSlaEventType.EndMailboxTableQuery, null);
        }
 internal static void LogDatabaseStopEvent(AssistantBase assistant)
 {
     AssistantsLog.LogDatabaseSlaEvent(assistant, DatabaseSlaEventType.StopDatabase, null);
 }
 internal static void LogStartProcessingMailboxEvent(Guid activityId, AssistantBase assistant, Guid mailboxGuid, string mailboxDisplayNameTracingOnlyUsage, TimeBasedDatabaseJob job)
 {
     ArgumentValidator.ThrowIfNull("job", job);
     AssistantsLog.InternalLogAssistantEvent(activityId, assistant, AssistantsEventType.StartProcessingMailbox, null, mailboxGuid);
     AssistantsLog.LogMailboxSlaEvent(assistant, mailboxGuid, mailboxDisplayNameTracingOnlyUsage, job, MailboxSlaEventType.StartProcessingMailbox, MailboxSlaFilterReasonType.None, null);
 }
 internal static void LogMailboxSucceedToOpenStoreSessionEvent(Guid activityId, string assistantName, AssistantBase assistant, Guid mailboxGuid, string mailboxDisplayNameTracingOnlyUsage, TimeBasedDatabaseJob job)
 {
     AssistantsLog.InternalLogRow(activityId, assistantName, assistant, AssistantsEventType.SucceedOpenMailboxStoreSession, null, mailboxGuid);
     AssistantsLog.LogMailboxSlaEvent(assistant, mailboxGuid, mailboxDisplayNameTracingOnlyUsage, job, MailboxSlaEventType.SucceedOpenMailboxStoreSession, MailboxSlaFilterReasonType.None, null);
 }
        internal static void LogMailboxFilteredEvent(Guid activityId, string assistantName, AssistantBase assistant, string reason, Guid mailboxGuid, string mailboxDisplayNameTracingOnlyUsage, MailboxSlaFilterReasonType filterReason)
        {
            List <KeyValuePair <string, object> > customData = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("Reason", reason)
            };

            AssistantsLog.InternalLogRow(activityId, assistantName, assistant, AssistantsEventType.FilterMailbox, customData, mailboxGuid);
            AssistantsLog.LogMailboxSlaEvent(assistant, mailboxGuid, mailboxDisplayNameTracingOnlyUsage, null, MailboxSlaEventType.FilterMailbox, filterReason, null);
        }
 internal static void LogMailboxNotInterestingEvent(Guid activityId, string assistantName, AssistantBase assistant, Guid mailboxGuid, string mailboxDisplayNameTracingOnlyUsage)
 {
     AssistantsLog.InternalLogRow(activityId, assistantName, assistant, AssistantsEventType.MailboxNotInteresting, null, mailboxGuid);
     AssistantsLog.LogMailboxSlaEvent(assistant, mailboxGuid, mailboxDisplayNameTracingOnlyUsage, null, MailboxSlaEventType.MailboxNotInteresting, MailboxSlaFilterReasonType.None, null);
 }
 internal static void LogMailboxInterestingEvent(Guid activityId, string assistantName, AssistantBase assistant, List <KeyValuePair <string, object> > customData, Guid mailboxGuid, string mailboxDisplayNameTracingOnlyUsage)
 {
     AssistantsLog.InternalLogRow(activityId, assistantName, assistant, AssistantsEventType.MailboxInteresting, customData, mailboxGuid);
     AssistantsLog.LogMailboxSlaEvent(assistant, mailboxGuid, mailboxDisplayNameTracingOnlyUsage, null, MailboxSlaEventType.MailboxInteresting, MailboxSlaFilterReasonType.None, null);
 }
        internal static void LogMailboxFailedToOpenStoreSessionEvent(Guid activityId, string assistantName, AssistantBase assistant, Exception storeSessionException, Guid mailboxGuid, string mailboxDisplayNameTracingOnlyUsage, TimeBasedDatabaseJob job)
        {
            List <KeyValuePair <string, object> > list = new List <KeyValuePair <string, object> >();

            if (storeSessionException != null)
            {
                list.Add(new KeyValuePair <string, object>("ExceptionType", storeSessionException.GetType().ToString()));
                list.Add(new KeyValuePair <string, object>("ExceptionMessage", storeSessionException.Message));
                if (storeSessionException.InnerException != null)
                {
                    list.Add(new KeyValuePair <string, object>("InnerExceptionType", storeSessionException.InnerException.GetType().ToString()));
                    list.Add(new KeyValuePair <string, object>("InnerExceptionMessage", storeSessionException.InnerException.Message));
                }
            }
            AssistantsLog.InternalLogRow(activityId, assistantName, assistant, AssistantsEventType.FailedOpenMailboxStoreSession, list, mailboxGuid);
            AssistantsLog.LogMailboxSlaEvent(assistant, mailboxGuid, mailboxDisplayNameTracingOnlyUsage, job, MailboxSlaEventType.FailedOpenMailboxStoreSession, MailboxSlaFilterReasonType.None, storeSessionException);
        }
        private StoreSession OpenMailboxSession(StoreMailboxData mailbox)
        {
            Guid          activityId       = (ActivityContext.ActivityId != null) ? ActivityContext.ActivityId.Value : Guid.Empty;
            AssistantBase assistant        = this.Assistant as AssistantBase;
            string        nonLocalizedName = this.Assistant.NonLocalizedName;
            Guid          mailboxGuid      = mailbox.MailboxGuid;
            StoreSession  result;

            try
            {
                ExchangePrincipal exchangePrincipal;
                if (mailbox.TenantPartitionHint != null)
                {
                    ADSessionSettings adSettings = ADSessionSettings.FromTenantPartitionHint(mailbox.TenantPartitionHint);
                    exchangePrincipal = ExchangePrincipal.FromLocalServerMailboxGuid(adSettings, this.DatabaseInfo.Guid, mailbox.Guid);
                }
                else
                {
                    exchangePrincipal = ExchangePrincipal.FromMailboxData(mailbox.Guid, this.DatabaseInfo.Guid, mailbox.OrganizationId ?? OrganizationId.ForestWideOrgId, Array <CultureInfo> .Empty);
                }
                if (mailbox.IsPublicFolderMailbox)
                {
                    StoreSession storeSession = PublicFolderSession.OpenAsAdmin(null, exchangePrincipal, null, CultureInfo.InstalledUICulture, string.Format("{0};Action={1}", "Client=TBA", this.Assistant.GetType().Name), null);
                    AssistantsLog.LogMailboxSucceedToOpenStoreSessionEvent(activityId, nonLocalizedName, assistant, mailboxGuid, mailbox.DisplayName, this);
                    result = storeSession;
                }
                else
                {
                    bool           flag     = false;
                    MailboxSession mailbox2 = this.DatabaseInfo.GetMailbox(exchangePrincipal, ClientType.TimeBased, this.Assistant.GetType().Name);
                    try
                    {
                        mailbox2.ReconstructExchangePrincipal();
                        mailbox2.ExTimeZone = ExTimeZone.CurrentTimeZone;
                        flag = true;
                        AssistantsLog.LogMailboxSucceedToOpenStoreSessionEvent(activityId, nonLocalizedName, assistant, mailboxGuid, mailbox.DisplayName, this);
                        result = mailbox2;
                    }
                    finally
                    {
                        if (!flag)
                        {
                            mailbox2.Dispose();
                        }
                    }
                }
            }
            catch (ObjectNotFoundException ex)
            {
                string text    = "MailboxNotFound";
                string message = string.Format("{0}: {1}", this, text);
                string value   = string.Format("{0}:{1}", text, mailbox.MailboxGuid);
                ExTraceGlobals.TimeBasedDatabaseJobTracer.TraceError((long)this.GetHashCode(), message);
                AssistantsLog.LogMailboxFailedToOpenStoreSessionEvent(activityId, nonLocalizedName, assistant, ex, mailboxGuid, mailbox.DisplayName, this);
                throw new SkipException(new LocalizedString(value), ex);
            }
            catch (StorageTransientException ex2)
            {
                string message2 = string.Format("{0}: Could not open mailbox store session due to storage transient error: {1}", this, ex2.Message);
                ExTraceGlobals.TimeBasedDatabaseJobTracer.TraceError((long)this.GetHashCode(), message2);
                AssistantsLog.LogMailboxFailedToOpenStoreSessionEvent(activityId, nonLocalizedName, assistant, ex2, mailboxGuid, mailbox.DisplayName, this);
                Interlocked.Increment(ref this.mailboxesFailedToOpenStoreSessionCount);
                throw;
            }
            catch (Exception ex3)
            {
                string message3 = string.Format("{0}: Could not open mailbox store session due to error: {1}", this, ex3.Message);
                ExTraceGlobals.TimeBasedDatabaseJobTracer.TraceError((long)this.GetHashCode(), message3);
                AssistantsLog.LogMailboxFailedToOpenStoreSessionEvent(activityId, nonLocalizedName, assistant, ex3, mailboxGuid, mailbox.DisplayName, this);
                Interlocked.Increment(ref this.mailboxesFailedToOpenStoreSessionCount);
                throw;
            }
            return(result);
        }