This Forum is ARCHIVE only
For LightSwitch questions,
please use the official forums at:

http://social.msdn.microsoft.com/forums/en-US/lightswitch/threads


Generic Auditing
Last Post 27 Aug 2012 05:11 PM by whitec. 2 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
CraigUser is Offline
New Member
New Member
Send Private Message
Posts:23
Avatar

--
05 Jul 2012 11:01 PM

    Firstly this is based on   http://social.msdn.microsoft.com/Forums/en-US/lightswitch/thread/69c3e821-8f61-4ea2-993d-57794b65202c  but has been adapted and re-written in C#.

     

    I wanted Auditing at screen level for my main Entities, so I have adapted  Frank Claassen's VB code, so here it is if anyone wants it.

    This obviously needs an AUDIT table that the information is going to be saved to, but once there it is easy to save your data to (I created a relationship with mine to the relevant table)

     

            #region Audit

     

            #region Generic Audit

     

            private void Audit()

            {

                dynamic newvals = "\n";

                dynamic oldvals = "\n";

                string ChangeType = null;

                EntityChangeSet Changes = this.DataWorkspace.ApplicationData.Details.GetChanges();

                foreach (IEntityObject Obj in Changes)

                {

                    foreach (var prop in Obj.Details.Properties.All().OfType().Where

                            (x => x.Name != "Unwanted_Field_Name" && x.Name != "And_Another"))

                    {

                        //prop = prop_loopVariable;

                        if (Obj.Details.EntityState == EntityState.Modified)

                        {

                            if (!object.Equals(prop.Value, prop.OriginalValue))

                            {

                                oldvals += string.Format("\n{0}: {1}", prop.DisplayName, prop.OriginalValue);

                                newvals += string.Format("\n{0}: {1}", prop.DisplayName, prop.Value);

                                ChangeType = Obj.Details.Entity.Details.Name.ToString() + " " + Obj.Details.EntityState.ToString();

                                //AddAuditLine(Type.Replace("SD_", ""), oldvals.Replace("\n\n", ""), newvals.Replace("\n\n", ""));

                            }

                        }

     

                        else if (Obj.Details.EntityState == EntityState.Added)

                        {

                            newvals += string.Format("\n{0}: {1}", prop.DisplayName, prop.Value);

                            ChangeType = Obj.Details.Entity.Details.Name.ToString() + " " + Obj.Details.EntityState.ToString();

                            //AddAuditLine(Type.Replace("SD_", ""), null, newvals.Replace("\n\n", ""));

                        }

     

                        else //EntityState.Deleted

                        {

                            oldvals += string.Format("\n{0}: {1}", prop.DisplayName, prop.Value);

                            ChangeType = Obj.Details.Entity.Details.Name.ToString() + " " + Obj.Details.EntityState.ToString();

                            //AddAuditLine(Type.Replace("SD_", ""), oldvals.Replace("\n\n", ""), null);

                        }

                    }

     

                    if (ChangeType != null)

                    {

                        AddAuditLine(ChangeType, oldvals.Replace("\n\n", ""), newvals.Replace("\n\n", ""));

                        ChangeType = null;

                    }

                }

            }

     

            #endregion Generic Audit

     

            private void AddAuditLine(string AuditDescription, string OriginalValue, string NewValue)

            {

                AUDITs.SelectedItem = AUDITs.AddNew();

                AUDITs.SelectedItem.LAST_UPDATED = DateTime.Now;

                AUDITs.SelectedItem.LAST_UPDATED_BY = Application.User.FullName;

                AUDITs.SelectedItem.AUDIT = AuditDescription;

                AUDITs.SelectedItem.ORIGINAL_VALUE = OriginalValue;

                AUDITs.SelectedItem.NEW_VALUE = NewValue;

            }

     

            #endregion Audit

     

    CraigUser is Offline
    New Member
    New Member
    Send Private Message
    Posts:23
    Avatar

    --
    05 Jul 2012 11:02 PM
    Forgot to say, this is run from the _Saving method on the screen.
    {
    Audit();
    }
    CraigUser is Offline
    New Member
    New Member
    Send Private Message
    Posts:23
    Avatar

    --
    27 Aug 2012 05:11 PM
    If you want more complete auditing you can use http://blog.pragmaswitch.com/?p=291



    P.S. This is not my work.
    You are not authorized to post a reply.


    Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation