Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to ignore errors when serializing?
    primarykey
    data
    text
    <p>We have some code that is exhibiting some strange behaviour. It interacts with a third party component which may be to blame. To debug this I've added a method that logs all calls to this third party component as XML. The method looks like this:</p> <pre><code> protected override void OnUnload(EventArgs e) { if (_twLog != null) { _twLog.Flush(); _twLog.Close(); _twLog.Dispose(); } _twLogDisposed = true; base.OnUnload(e); } private TextWriter _twLog; private bool _twLogDisposed; private void Log(string methodName, object returnValue, object[] paramaters) { if (!_twLogDisposed &amp;&amp; Directory.Exists(HttpContext.Current.Server.MapPath("~/crmlogs/"))) { if (_twLog == null) { string path = HttpContext.Current.Server.MapPath("~/crmlogs/" + HttpContext.Current.Request.UserHostAddress.Replace(":",string.Empty) +".xml"); _twLog = new StreamWriter(File.Open(path, FileMode.Append, FileAccess.Write)); } </code></pre> <p>object returnValueToSerialize = returnValue; if (returnValue is DataTable) { StringWriter sw = new StringWriter(); ((DataTable)returnValue).WriteXml(sw); returnValueToSerialize = sw.ToString(); sw.Dispose(); }</p> <pre><code> var crmEvent = new CrmEvent(methodName, returnValueToSerialize, paramaters); var xs = new XmlSerializer(typeof (CrmEvent)); xs.Serialize(_twLog, crmEvent); } } public class CrmEvent { public CrmEvent() { } public CrmEvent(string methodName, object returnValue, object[] paramaters) { MethodName = methodName; ReturnValue = returnValue ?? string.Empty; Paramaters = paramaters; Date = DateTime.Now; } public string MethodName { get; set; } public object ReturnValue { get; set; } public object[] Paramaters { get; set; } public DateTime Date { get; set; } } </code></pre> <p>I've done a find and replace with a regex and ensured that this method is called after every call to the component. The only thing that I'm concerned about is if this component returns objects that can't be serialized. I would like to record as much data as possible if this happens instead of just failing. What is the best way to do this?</p> <p>Thanks,</p> <p>Joe</p> <p>Ok here is my ugly as hell solution that seems to work. Hopefully there is a better way to do this though.</p> <pre><code> private void Log(string methodName, object returnValue, object[] paramaters) { if (!_twLogDisposed &amp;&amp; Directory.Exists(HttpContext.Current.Server.MapPath("~/crmlogs/"))) { if (_twLog == null) { string path = HttpContext.Current.Server.MapPath("~/crmlogs/" + HttpContext.Current.Request.UserHostAddress.Replace(":", string .Empty) + ".xml"); _twLog = new StreamWriter(File.Open(path, FileMode.Append, FileAccess.Write)); } object returnValueToSerialize; try { var sw = new StringWriter(); if (returnValue is DataTable) { var dt = (DataTable) returnValue; if (string.IsNullOrEmpty(dt.TableName)) { dt.TableName = "tempname"; // WriteXml requires this to be set } dt.WriteXml(sw); } else { var retValXs = new XmlSerializer(returnValue.GetType()); retValXs.Serialize(sw, returnValue); } returnValueToSerialize = sw.ToString(); sw.Dispose(); } catch (Exception ex) { returnValueToSerialize = "Could not seriaize " + returnValue.GetType().Name + ": " + ex.Message; } var crmEvent = new CrmEvent(methodName, returnValueToSerialize, paramaters); var xs = new XmlSerializer(typeof (CrmEvent)); try { xs.Serialize(_twLog, crmEvent); } catch { // Because we handle un serializable returnvalues above this must be an unserializable paramater for (int i = 0; i &lt; crmEvent.Paramaters.Length; i++) { var curParam = crmEvent.Paramaters[i]; if (!curParam.GetType().IsPrimitive) { var xsParam = new XmlSerializer(curParam.GetType()); var sw = new StringWriter(); try { xsParam.Serialize(sw, curParam); crmEvent.Paramaters[i] = sw.ToString(); } catch (Exception ex) { crmEvent.Paramaters[i] = "Could not seriaize " + curParam.GetType().Name + ": " + ex.Message; } } } try { xs.Serialize(_twLog, crmEvent); } catch { xs.Serialize(_twLog, new CrmEvent(methodName, "Error serializing", new object[] {})); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload