The logging structure does not contain information about the boundaries of your method, and therefore, it cannot format XML accordingly. You can try to expand the structure and pass additional information to logging methods.
An easier way to get this conclusion is to do all the formatting of the messages in your class aspect and configure the logging structure to display only the text of the message without any other information.
For example, in the log4net configuration:
<layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message%newline" /> </layout>
In your OnEntry aspect , you should open the XML tag (or JSON object) and close the tag in OnExit . You can start with a simple example below and optimize the code for your specific case (for example, initialize a log instance and format strings in the RuntimeInitialize, CompileTimeInitialize methods).
[Serializable] public class XmlLogAttribute : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { ILog log = LogManager.GetLogger(args.Method.DeclaringType); if (log.IsDebugEnabled) { log.DebugFormat("<Method name=\"{0}\">", args.Method.Name); log.Debug("<Params>"); foreach (object methodArgument in args.Arguments) { if (methodArgument == null) { log.Debug("<null/>"); } else { log.DebugFormat("<{0}>{1}</{0}>", methodArgument.GetType(), methodArgument); } } log.Debug("</Params>"); } } public override void OnExit(MethodExecutionArgs args) { ILog log = LogManager.GetLogger(args.Method.DeclaringType); log.Debug("</Method>"); } }
Alexd source share