0

So I'm trying to find out during runtime which parent method has called a method which is writing to a log file. I have many endpoints using [HttpGet]/[HttpPost] and I would like to know which one is being calling while being much further down the call stack. Now I've looked into using a stack trace/frames but it yielded very little usable results. I've also looked at using CallerMemberName but its seems to only go up one level.

I.e [HttpGet] GetAllInfo calls GetTextInfo calls GetTextInfoFromDB1 calls {the logger which writes}.

I want to know how to identify that it was GetAllInfo who was initially called, which caused the logger to write. The final catch is that I'm not about to edit a few hundred methods.

BluesSP
  • 13
  • 3
  • If you want to access information about the endpoint being called you should look at the [HttpContext](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-5.0#use-httpcontext-from-custom-components-1) (I'm assuming this is ASP.NET) – Xerillio Mar 25 '22 at 18:33
  • @Xerillio They want to obtain the string `GetAllInfo` from the method `GetTextInfoFromDB1` (as one example) -- what functionality of `HttpContext` will provide that? – Kirk Woll Mar 25 '22 at 18:35
  • @BluesSP, that far up the callstack, you'll probably have to make use of `System.Diagnostics.StackTrace` as shown in this answer: https://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method – Kirk Woll Mar 25 '22 at 18:36
  • @KirkWoll The question doesn't state that it's that exact string he's looking for. But if that indeed is the case it should be clarified. In that case you could take a look at [ActionContextAccessor](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.infrastructure.actioncontextaccessor?view=aspnetcore-5.0) instead. – Xerillio Mar 25 '22 at 18:39
  • Well, it states that it is `GetAllInfo` that is being sought, so either that string or I suppose a `MethodInfo` corresponding to the method. – Kirk Woll Mar 25 '22 at 18:42

1 Answers1

0

Check these methods out:

    /// <summary>
    ///     Gets the caller method.
    /// </summary>
    /// <param name="index"> The index. </param>
    /// <returns> </returns>
    public static MethodBase? GetCallerMethod(in int index = 2) =>
        new StackTrace(true).GetFrame(index)?.GetMethod();

    /// <summary>
    ///     Gets the caller method.
    /// </summary>
    /// <param name="index"> The index. </param>
    /// <param name="parsePrevIfNull"> if set to <c> true </c> [parse previous if null]. </param>
    /// <returns> </returns>
    public static MethodBase? GetCallerMethod(in int index, bool parsePrevIfNull)
    {
        var stackTrace = new StackTrace(true);
        var i = index;
        if (stackTrace.GetFrame(i) is not null || parsePrevIfNull is false)
        {
            return stackTrace.GetFrame(i)?.GetMethod();
        }

        while (stackTrace.GetFrame(--i) is null)
        {
        }

        return stackTrace.GetFrame(i)?.GetMethod();
    }

    /// <summary>
    ///     Gets the name of the caller method.
    /// </summary>
    /// <param name="index"> The index. </param>
    /// <returns> </returns>
    public static string? GetCallerMethodName(in int index = 2) =>
        GetCallerMethod(index)?.Name;

    /// <summary>
    ///     Gets the current method.
    /// </summary>
    /// <returns> </returns>
    public static MethodBase? GetCurrentMethod() =>
        GetCallerMethod();
Mohammad Mirmostafa
  • 1,720
  • 2
  • 16
  • 32