Logging entry and exit of methods along with parameters automagically?

前端 未结 7 525
遇见更好的自我
遇见更好的自我 2021-01-02 04:31

Is there a way for me to add logging so that entering and exiting methods gets logged along with parameters automatically somehow for tracing purposes? How would I do so?

相关标签:
7条回答
  • 2021-01-02 04:51

    You could use a post-compiler like Postsharp. The sample from the website talks about setting up a tracer for entering/exiting a method, which is very similar to what you want.

    0 讨论(0)
  • 2021-01-02 04:51

    You can use open source framework CInject on CodePlex that has a LogInjector to mark entry and exit of a method call.

    Or you can follow the steps mentioned on this article on Intercepting Method Calls using IL and create your own interceptor using Reflection.Emit classes in C#.

    0 讨论(0)
  • 2021-01-02 04:52

    Have a look at:

    How do I intercept a method call in C#? PostSharp - il weaving - thoughts

    Also search SO for 'AOP' or 'Aspect Oriented Programming' and PostSharp...you get some interesting results.

    0 讨论(0)
  • 2021-01-02 04:55

    I'm not sure what your actual needs are, but here's a low-rent option. It's not exactly "automatic", but you could use StackTrace to peel off the information you're looking for in a manner that wouldn't demand passing arguments - similar to ckramer's suggestion regarding interception:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using System.Diagnostics;
    
    namespace TracingSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                DoSomething();
            }
    
            static void DoSomething()
            {
                LogEnter();
    
                Console.WriteLine("Executing DoSomething");
    
                LogExit();
            }
    
            static void LogEnter()
            {
                StackTrace trace = new StackTrace();
                if (trace.FrameCount > 1)
                {
                    string ns = trace.GetFrame(1).GetMethod().DeclaringType.Namespace;
                    string typeName = trace.GetFrame(1).GetMethod().DeclaringType.Name;
                    Console.WriteLine("Entering {0}.{1}.{2}", ns, typeName, trace.GetFrame(1).GetMethod().Name);
                }
            }
    
            static void LogExit()
            {
                StackTrace trace = new StackTrace();
                if (trace.FrameCount > 1)
                {
                    string ns = trace.GetFrame(1).GetMethod().DeclaringType.Namespace;
                    string typeName = trace.GetFrame(1).GetMethod().DeclaringType.Name;
                    Console.WriteLine("Exiting {0}.{1}.{2}", ns, typeName, trace.GetFrame(1).GetMethod().Name);
                }
            }
        }
    }
    

    You could combine something like the above example with inheritance, using a non-virtual public member in the base type to signify the action method, then calling a virtual member to actually do the work:

    public abstract class BaseType
    {
        public void SomeFunction()
        {
    
            LogEnter();
            DoSomeFunction();
            LogExit();
        }
    
        public abstract void DoSomeFunction();
    }
    
    public class SubType : BaseType
    {
        public override void DoSomeFunction()
        {
            // Implementation of SomeFunction logic here...
        }
    }
    

    Again - there's not much "automatic" about this, but it would work on a limited basis if you didn't require instrumentation on every single method invocation.

    Hope this helps.

    0 讨论(0)
  • 2021-01-02 05:00

    The best way to achieve this sort of thing is by using interception, There are a couple of ways to do this, though they all tend to be somewhat invasive. One would be to derive all your objects from ContextBoundObject. Here is an example of using this sort of approach. The other approach would be to use one of the existing AOP libraries to achieve this. Something like DynamicProxy from the Castle Project is at the core of many of these. Here are a few links: Spring.Net PostSharp Cecil

    There are probably several others, and I know Castle Windsor, and Ninject both provide AOP capabilities on top of the IoC functionality.

    Once AOP is in place you would simply write an interceptor class that would write the information about the method calls out to log4net.

    I actually wouldn't be surprised if one of the AOP frameworks would give you that sort of functionality out of the box.

    0 讨论(0)
  • 2021-01-02 05:10

    This does not apply to all C# applications but I am doing the following in an Asp.Net Core application to add logging to controller method calls by implementing the following interfaces:

    IActionFilter, IAsyncActionFilter.

    public void OnActionExecuting(ActionExecutingContext context)    
    
    public void OnActionExecuted(ActionExecutedContext context)    
    
    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)    
    
    0 讨论(0)
提交回复
热议问题