public static string GetXmlString(Exception exception, UserIdentity user, DateTime d)
 {
     if (exception == null) throw new ArgumentNullException("exception");
     StringWriter sw = new StringWriter();
     using (XmlWriter xw = XmlWriter.Create(sw, new XmlWriterSettings() { Indent = true }))
     {
         xw.WriteStartElement("Log");
         xw.WriteAttributeString("date", d.ToString("s"));
         WriteException(xw, "exception", exception);
         WriteContext(xw, "context", user);
         xw.WriteEndElement();
     }
     return sw.ToString();
 }
        static void WriteContext(XmlWriter writer, string name, UserIdentity user)
        {
            if (user == null) return;
            writer.WriteStartElement(name);

            writer.WriteElementString("UserName", user.UserName);
            writer.WriteElementString("Culture", user.CurrentCultureId.ToString());

            writer.WriteStartElement("Claims");
            foreach (var claim in user.Claims)
                writer.WriteElementString("Claim", claim);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }