Index: src/Common/DelftTools.Utils/Aop/TraceDllImportCallsAttribute.cs =================================================================== diff -u -r8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9 -r5fc71a385897af92ccb092f2f969b5709afab85a --- src/Common/DelftTools.Utils/Aop/TraceDllImportCallsAttribute.cs (.../TraceDllImportCallsAttribute.cs) (revision 8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9) +++ src/Common/DelftTools.Utils/Aop/TraceDllImportCallsAttribute.cs (.../TraceDllImportCallsAttribute.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a) @@ -26,11 +26,11 @@ public override bool CompileTimeValidate(MethodBase method) { // trace only methods with the DllImport attribute - return method.GetCustomAttributes(typeof (DllImportAttribute), false).Length > 0; + return method.GetCustomAttributes(typeof(DllImportAttribute), false).Length > 0; } public override void CompileTimeInitialize(MethodBase method, AspectInfo - aspectInfo) + aspectInfo) { base.CompileTimeInitialize(method, aspectInfo); // build cache: @@ -43,15 +43,19 @@ { // todo: check the performance overhead of this: if (!TraceDllImportCallsConfig.IsLoggingEnabled(type)) - return; + { + return; + } // todo: consider using OnExit, because sometimes even on-ref parameters are used as by-ref // and aren't filled in till after the call int count = args.Arguments.Count; var values = new object[count]; for (int i = 0; i < count; i++) + { values[i] = args.Arguments.GetArgument(i); + } TraceDllImportCallsConfig.Log(type, methodName, parameterNames, values); } @@ -66,25 +70,66 @@ private static readonly Dictionary LogWriters = new Dictionary(); + public static bool IsLoggingEnabled(Type type) + { + XmlTextWriter writer; + return LogWriters.TryGetValue(type, out writer); + } + + /// + /// Start logging (to xml) on a type with the TraceDllImportCalls attribute. + /// + /// + /// + public static void StartLogging(Type type, string path) + { + var writer = new XmlTextWriter(path, Encoding.UTF8) + { + Formatting = Formatting.Indented + }; + writer.WriteStartDocument(); + writer.WriteStartElement("Run"); + writer.WriteAttributeString("type", type.Name); + writer.WriteAttributeString("time", DateTime.Now.ToString( + CultureInfo.InvariantCulture.DateTimeFormat.FullDateTimePattern, + CultureInfo.InvariantCulture)); + LogWriters.Add(type, writer); + } + + public static void StopLogging(Type type) + { + XmlWriter writer = LogWriters[type]; + writer.WriteEndElement(); // close + writer.WriteEndDocument(); // close document + writer.Close(); // close file + LogWriters.Remove(type); + } + internal static void Log(Type type, string callName, string[] parameterNames, object[] parameterValues) { XmlTextWriter writer; if (!LogWriters.TryGetValue(type, out writer)) + { return; + } writer.WriteStartElement(callName); for (int i = 0; i < parameterNames.Length; i++) + { writer.WriteAttributeString(parameterNames[i], ToString(parameterValues[i])); + } writer.WriteEndElement(); } private static string ToString(object value) { var valueAsString = value as string; if (valueAsString != null) + { return valueAsString; + } var enuerable = value as IEnumerable; if (enuerable != null) @@ -96,43 +141,13 @@ sb.Append(" "); } if (sb.Length > 1) + { sb.Remove(sb.Length - 1, 1); // remove last space + } return sb.ToString(); } return value.ToString(); } - - public static bool IsLoggingEnabled(Type type) - { - XmlTextWriter writer; - return LogWriters.TryGetValue(type, out writer); - } - - /// - /// Start logging (to xml) on a type with the TraceDllImportCalls attribute. - /// - /// - /// - public static void StartLogging(Type type, string path) - { - var writer = new XmlTextWriter(path, Encoding.UTF8) {Formatting = Formatting.Indented}; - writer.WriteStartDocument(); - writer.WriteStartElement("Run"); - writer.WriteAttributeString("type", type.Name); - writer.WriteAttributeString("time", DateTime.Now.ToString( - CultureInfo.InvariantCulture.DateTimeFormat.FullDateTimePattern, - CultureInfo.InvariantCulture)); - LogWriters.Add(type, writer); - } - - public static void StopLogging(Type type) - { - XmlWriter writer = LogWriters[type]; - writer.WriteEndElement(); // close - writer.WriteEndDocument(); // close document - writer.Close(); // close file - LogWriters.Remove(type); - } } } \ No newline at end of file