コード例 #1
0
ファイル: PrivateMethods.cs プロジェクト: arashemami/Sandbox
        internal static void WritePlatformLog(IPlatformLogType activity, string description, string details = null)
        {
            CloudTableClient cloudTableClient = EnvironmentSettings.StorageConnection.PlatformStorage.CreateCloudTableClient();

            //Create an instance of each entity type and pass in associated CloudTableClient & TableName
            PlatformLogTableEntity_ByActivity logTableEntity_Activity = new PlatformLogTableEntity_ByActivity(cloudTableClient, activity.LogName.ToLower() + "" + "byactivity");
            PlatformLogTableEntity_ByTime     logTableEntity_Time     = new PlatformLogTableEntity_ByTime(cloudTableClient, activity.LogName.ToLower() + "" + "bytime");

            //Gather up all the entities into a list for our parallel task to execute in a ForEach
            List <Object> entityTypes = new List <object>();

            entityTypes.Add(logTableEntity_Activity);
            entityTypes.Add(logTableEntity_Time);

            try
            {
                Parallel.ForEach(entityTypes, obj =>
                {
                    #region Trace Statements

                    //Display the id of the thread for each parallel instance to verifiy prallelism
                    //Trace.TraceInformation("Current thread ID: " + Thread.CurrentThread.ManagedThreadId);

                    #endregion

                    //Transform the LogItem into each corresponding table entity type for insert execution into logs
                    (obj as IPlatformLogTableEntity).ActivityType = activity.Activity;
                    (obj as IPlatformLogTableEntity).Description  = description;
                    (obj as IPlatformLogTableEntity).Details      = details;

                    //Create table for entity if not exists
                    (obj as IPlatformLogTableEntity).cloudTable.CreateIfNotExists();

                    //create an insert operation for each entity, assign to designated CloudTable, and add to our list of tasks:
                    TableOperation operation = TableOperation.Insert((obj as TableEntity));
                    (obj as IPlatformLogTableEntity).cloudTable.Execute(operation);
                });
            }
            catch (Exception e)
            {
            }
        }
コード例 #2
0
        internal static void WritePlatformLog(CategoryType categoryType, ActivityType activityType, string description, string details = null, string accountId = null, string accountName = null, string userId = null, string userName = null, string userEmail = null, string ipAddress = null, string origin = null, string serializedObject = null)
        {
            CloudTableClient cloudTableClient = Settings.Azure.Storage.StorageConnections.PlatformStorage.CreateCloudTableClient();

            //Create and set retry policy for logging
            //IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(1), 4);
            IRetryPolicy linearRetryPolicy = new LinearRetry(TimeSpan.FromSeconds(1), 4);

            cloudTableClient.DefaultRequestOptions.RetryPolicy = linearRetryPolicy;

            //Gather up all the entities into a list for our parallel task to execute in a ForEach
            List <Object> entityTypes = new List <object>();


            //Create an instance of each entity type and pass in associated CloudTableClient & TableName
            PlatformLogTableEntity_ByTime logTableEntity_Time = new PlatformLogTableEntity_ByTime(cloudTableClient, "platformactivitylogbytime");

            entityTypes.Add(logTableEntity_Time);

            PlatformLogTableEntity_ByCategory logTableEntity_Category = new PlatformLogTableEntity_ByCategory(cloudTableClient, "platformactivitylogbycategory");

            entityTypes.Add(logTableEntity_Category);

            PlatformLogTableEntity_ByActivity logTableEntity_Activity = new PlatformLogTableEntity_ByActivity(cloudTableClient, "platformactivitylogbyactivity");

            entityTypes.Add(logTableEntity_Activity);

            if (accountId != null)
            {
                PlatformLogTableEntity_ByAccountID logTableEntity_AccountID = new PlatformLogTableEntity_ByAccountID(cloudTableClient, "platformactivitylogbyaccountid");
                entityTypes.Add(logTableEntity_AccountID);
            }

            if (userId != null)
            {
                PlatformLogTableEntity_ByUserID logTableEntity_UserID = new PlatformLogTableEntity_ByUserID(cloudTableClient, "platformactivitylogbyuserid");
                entityTypes.Add(logTableEntity_UserID);
            }



            try
            {
                Parallel.ForEach(entityTypes, obj =>
                {
                    #region Trace Statements

                    //Display the id of the thread for each parallel instance to verifiy prallelism
                    //Trace.TraceInformation("Current thread ID: " + Thread.CurrentThread.ManagedThreadId);

                    #endregion

                    //Transform the LogItem into each corresponding table entity type for insert execution into logs
                    (obj as IPlatformLogTableEntity).Category    = categoryType.ToString();
                    (obj as IPlatformLogTableEntity).Activity    = activityType.ToString();
                    (obj as IPlatformLogTableEntity).Description = description.ToString();
                    (obj as IPlatformLogTableEntity).Details     = details;

                    (obj as IPlatformLogTableEntity).UserID    = userId;
                    (obj as IPlatformLogTableEntity).UserName  = userName;
                    (obj as IPlatformLogTableEntity).UserEmail = userEmail;

                    (obj as IPlatformLogTableEntity).IPAddress = ipAddress;
                    (obj as IPlatformLogTableEntity).Origin    = origin;

                    (obj as IPlatformLogTableEntity).AccountID   = accountId;
                    (obj as IPlatformLogTableEntity).AccountName = accountName;

                    (obj as IPlatformLogTableEntity).Object = serializedObject;


                    //create an insert operation for each entity, assign to designated CloudTable, and add to our list of tasks:
                    TableOperation operation = TableOperation.Insert((obj as TableEntity));
                    (obj as IPlatformLogTableEntity).cloudTable.Execute(operation);
                });
            }
            catch (Exception e)
            {
                //Email platform admins about this exception
                #region Email Platform Admins About Exception

                try
                {
                    /*
                     *
                     * Using SendGrid Library is not possible in this one case due to circular reference issues
                     *
                     */

                    #region Create Parameter String

                    var parametersString = new StringBuilder();

                    parametersString.Append("(");

                    try
                    {
                        parametersString.Append(categoryType.ToString());
                        parametersString.Append(", ");

                        parametersString.Append(activityType.ToString());
                        parametersString.Append(", ");

                        parametersString.Append(description);
                        parametersString.Append(", ");

                        if (!String.IsNullOrEmpty(details))
                        {
                            parametersString.Append(details);
                            parametersString.Append(", ");
                        }
                        else
                        {
                            parametersString.Append("null");
                            parametersString.Append(", ");
                        }

                        if (!String.IsNullOrEmpty(accountId))
                        {
                            parametersString.Append(accountId);
                            parametersString.Append(", ");
                        }
                        else
                        {
                            parametersString.Append("null");
                            parametersString.Append(", ");
                        }

                        if (!String.IsNullOrEmpty(accountName))
                        {
                            parametersString.Append(accountName);
                            parametersString.Append(", ");
                        }
                        else
                        {
                            parametersString.Append("null");
                            parametersString.Append(", ");
                        }


                        if (!String.IsNullOrEmpty(userId))
                        {
                            parametersString.Append(userId);
                            parametersString.Append(", ");
                        }
                        else
                        {
                            parametersString.Append("null");
                            parametersString.Append(", ");
                        }

                        if (!String.IsNullOrEmpty(userName))
                        {
                            parametersString.Append(userName);
                            parametersString.Append(", ");
                        }
                        else
                        {
                            parametersString.Append("null");
                            parametersString.Append(", ");
                        }


                        if (!String.IsNullOrEmpty(userEmail))
                        {
                            parametersString.Append(userEmail);
                            parametersString.Append(", ");
                        }
                        else
                        {
                            parametersString.Append("null");
                            parametersString.Append(", ");
                        }

                        if (!String.IsNullOrEmpty(ipAddress))
                        {
                            parametersString.Append(ipAddress);
                            parametersString.Append(", ");
                        }
                        else
                        {
                            parametersString.Append("null");
                            parametersString.Append(", ");
                        }

                        if (!String.IsNullOrEmpty(origin))
                        {
                            parametersString.Append(origin);
                            parametersString.Append(", ");
                        }
                        else
                        {
                            parametersString.Append("null");
                            parametersString.Append(", ");
                        }
                        if (!String.IsNullOrEmpty(serializedObject))
                        {
                            parametersString.Append(serializedObject);
                            //parametersString.Append(", ");
                        }
                        else
                        {
                            parametersString.Append("null");
                            //parametersString.Append(", ");
                        }

                        parametersString.Append(")");
                    }
                    catch
                    {
                        parametersString.Append(" ...Error parsing next parameter -----> )");
                    }


                    #endregion

                    dynamic sg = new SendGridAPIClient(Settings.Services.SendGrid.Account.APIKey);

                    Email  from    = new Email(Settings.Endpoints.Emails.FromExceptions, "Platform Exception");
                    string subject = "Exception Alert! (From: Platform Logger)";

                    var personalization = new Personalization();
                    foreach (var email in Settings.Endpoints.Emails.PlatformEmailAddresses)
                    {
                        Email to = new Email(email);
                        personalization.AddTo(to);
                    }

                    //Mail mail = new Mail(from, subject, to, content);
                    Mail mail = new Mail();
                    mail.From            = from;
                    mail.Subject         = subject;
                    mail.Personalization = new List <Personalization>();
                    mail.Personalization.Add(personalization);



                    //var myMessage = new SendGridMessage();

                    //myMessage.From = new MailAddress(Settings.Endpoints.Emails.FromExceptions, "Platform Exception");
                    //myMessage.AddTo(Settings.Endpoints.Emails.PlatformEmailAddresses);
                    //myMessage.Subject = "Exception Alert! (From: Platform Logger)";

                    //myMessage.Html =

                    var body =
                        "Exception location: <b>" + System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "</b>" +
                        "<br/><br/>" +
                        "Exception occurred while attempting to write to the platform log..." +
                        "<br/><br/><b>" +
                        e.Message +
                        "</b><br/><br/>" +
                        "Log Parameters:<br/><br/><b>" +
                        parametersString.ToString() +
                        "</b><br/><br/>" +
                        JsonConvert.SerializeObject(e) +
                        "<br/><br/>";

                    Content content = new Content("text/html", body);

                    //var username = Settings.Services.SendGrid.Account.UserName;
                    //var pswd = Settings.Services.SendGrid.Account.APIKey;
                    //var credentials = new NetworkCredential(username, pswd);

                    //var transportWeb = new Web(credentials);
                    //transportWeb.Deliver(myMessage);

                    mail.Contents = new List <Content>();
                    mail.Contents.Add(content);

                    var requestBody = mail.Get();

                    //dynamic response = await sg.client.mail.send.post(requestBody: requestBody);
                    dynamic response = sg.client.mail.send.post(requestBody: requestBody);
                    //dynamic d = response;
                }
                catch
                {
                }



                #endregion
            }
        }