2016-07-14 17 views
0

Ich habe eine TraceAspect-Methode. Diese Klasse wird für die Protokollierung mit AOP verwendet.Log mit AOP funktioniert nicht

public class TracingAspect : IMessageSink 
{ 
    internal TracingAspect(IMessageSink next) 
    { 
     m_next = next; 
    } 

    #region Private Vars 
    private IMessageSink m_next; 
    private String m_typeAndName; 
    #endregion // Private Vars 

    #region IMessageSink implementation 
    public IMessageSink NextSink 
    { 
     get { return m_next; } 
    } 

    public IMessage SyncProcessMessage(IMessage msg) 
    { 
     Preprocess(msg); 
     IMessage returnMethod = m_next.SyncProcessMessage(msg); 
     PostProcess(msg, returnMethod); 
     return returnMethod; 
    } 

    public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink) 
    { 
     throw new InvalidOperationException(); 
    } 
    #endregion //IMessageSink implementation 

    #region Helper methods 
    private void Preprocess(IMessage msg) 
    { 
     // We only want to process method calls 
     if (!(msg is IMethodMessage)) return; 

     IMethodMessage call = msg as IMethodMessage; 
     Type type = Type.GetType(call.TypeName); 
     m_typeAndName = type.Name + "." + call.MethodName; 
     NLogging.Trace("PreProcessing: " + m_typeAndName + "("); 

     // Loop through the [in] parameters 
     for (int i = 0; i < call.ArgCount; ++i) 
     { 
      if (i > 0) 
       NLogging.Trace(", "); 
      NLogging.Trace(call.GetArgName(i) + " = " + call.GetArg(i)); 
     } 
     NLogging.Trace(")"); 
    } 

    private void PostProcess(IMessage msg, IMessage msgReturn) 
    { 
     // We only want to process method return calls 
     if (!(msg is IMethodMessage) || 
      !(msgReturn is IMethodReturnMessage)) return; 

     IMethodReturnMessage retMsg = (IMethodReturnMessage)msgReturn; 
     NLogging.Trace("PostProcessing: "); 
     Exception e = retMsg.Exception; 
     if (e != null) 
     { 
      NLogging.Trace("Exception was thrown: " + e); 
      return; 
     } 

     // Loop through all the [out] parameters 
     NLogging.Trace(m_typeAndName + "("); 
     if (retMsg.OutArgCount > 0) 
     { 
      NLogging.Trace("out parameters["); 
      for (int i = 0; i < retMsg.OutArgCount; ++i) 
      { 
       if (i > 0) 
        NLogging.Trace(", "); 
       NLogging.Trace(retMsg.GetOutArgName(i) + " = " + 
           retMsg.GetOutArg(i)); 
      } 
      NLogging.Trace("]"); 
     } 
     if (retMsg.ReturnValue.GetType() != typeof(void)) 
      NLogging.Trace(" returned [" + retMsg.ReturnValue + "]"); 

     NLogging.Trace(")\n"); 
    } 
    #endregion Helpers 
} 

public class TracingProperty : IContextProperty, IContributeObjectSink 
{ 
    #region IContributeObjectSink implementation 
    public IMessageSink GetObjectSink(MarshalByRefObject o, IMessageSink next) 
    { 
     return new TracingAspect(next); 
    } 
    #endregion // IContributeObjectSink implementation 

    #region IContextProperty implementation 
    public string Name 
    { 
     get 
     { 
      return "CallTracingProperty"; 
     } 
    } 
    public void Freeze(Context newContext) 
    { 
    } 
    public bool IsNewContextOK(Context newCtx) 
    { 
     return true; 
    } 
    #endregion //IContextProperty implementation 
} 

[Conditional("DEBUG")] 
[AttributeUsage(AttributeTargets.Class,AllowMultiple = false)] 
public class TracingAttribute : ContextAttribute 
{ 
    public TracingAttribute() : base("CallTracing") { } 
    public override void GetPropertiesForNewContext(IConstructionCallMessage ccm) 
    { 
     ccm.ContextProperties.Add(new TracingProperty()); 
    } 
} 

Auch ich habe eine statische Klasse.

[Tracing] 
public static class SampleStaticClass 
{ 
    public static string SampleStaticMethod() 
    { 
     return "From Static method"; 
    } 
} 

TracingAspect wird für alle nicht statisch arbeiten methods.But es dort für statische methods.Is arbeiten willnot sowieso statische Methoden zur Anmeldung AOP?

Antwort

0

Sie können ein AOP-Framework verwenden, um Ihre statische Methode zu protokollieren.

Die meisten der Kompilierung AOP-Framework kann es wie PostSharp tun (Gewerbe)

In meinem knownledge nur eine Runtime AOP-Framework können es tun: NConcern .NET (Open Source)