简体   繁体   中英

Does getting current class and method name reduce performance?

I have a logging class which creates an instance of log4net and I can call it from anywhere in my code. I also have a method to find out the class and method name of caller. here is my method:

private static string CurrentMethod()
{
    StackTrace stackTrace = new StackTrace();
    MethodBase method = stackTrace.GetFrame(2).GetMethod();
    string cn = method.ReflectedType.Name;
    string mn = method.Name;

    string output = "[" + cn + "." + mn + "]";
    return output;
}

i found these articles: link1 , Link2 , Link3 , Link4 and so many others, but none of them discuss about the efficiency and performance. now I have two questions: 1- can I use this method in a big project(about 1000 requests in one second)? I mean how much does this effect the project's efficiency and performance? 2- Os there a better way to write the above method?

Also here is part of my logging class. I guess it can help:

public static class Tools_Log
{
    private static ILog logger;

    public static ILog GetLogger()
    {
        return logger ?? (logger = CreateLogger());
    }
    private static ILog CreateLogger()
    {
        //Some log4net initialization
        return LogManager.GetLogger("WebService");
    }

    private static string CurrentMethod()
    {
        StackTrace stackTrace = new StackTrace();
        MethodBase method = stackTrace.GetFrame(2).GetMethod();
        string cn = method.ReflectedType.Name;
        string mn = method.Name;

        string output = "[" + cn + "." + mn + "]";
        return output;
    }

    public static string MessageForLogFile(string message, string exeption, double time)
    {
        string currentMethod;
        string executiontime;
        string consumer;
        string body;
        string output;

        currentMethod = CurrentMethod();
        executiontime = ExecutionTime(time);
        body = BodyForLog(message, exeption);
        consumer = Consumer();

        output = OutPut(currentMethod, executiontime, consumer, body);
        return output;
    }
}

i call the log class like this:

Tools_Log.GetLogger().Info(Tools_Log.MessageForLogFile("some text", "some text", execution time));

Thanks.

Yes, reflection is always a slower process. If you want the name of the method you can get it quite easily with the CallerMemberNameAttribute . Add a string parameter with an empty default value, and apply that attribute to it, and the compiler will send the name for you.

Also, if you're looking for fast logging, take a look at ETW instead of Log4Net.

Here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb968803(v=vs.85).aspx

And Here: http://blogs.msdn.com/b/vancem/archive/2012/07/09/logging-your-own-etw-events-in-c-system-diagnostics-tracing-eventsource.aspx

EDIT:
As for your question in the comments, unfortunately, you can't get the name of the method 2 levels up, at least, not directly. CallerMemberName is basically just syntax sugar, it tells the compiler to take the name of the calling member, and place it in the parameter, so it works on a single level. However, since it relies on default parameters to do so, if you send the parameter manually, the CallerMemberName functionality doesn't apply, so you can do something like this:

public void UserMethod()
{
    IntermediaryMethod();
}

public void IntermediaryMethod([CallerMemberName] caller = "")
{
    LogMethod(caller)
}

public void LogMethod([CallerMemberName] caller = "")
{
    // Log...
}

If you pass a value by yourself, it won't be overridden, so doing something like this will allow you to get the name of the caller from 2 levels up, while retaining the functionality for a single level.

Performance must be measured and this is done using profilers.

BTW, maybe instead of using StackTrace class, you can use caller information attributes introduced in .NET 4.5.

While [CallerMemberName] attribute doesn't provide class name, [CallerFilePath] provides the full path to the source code file where the caller member is defined ( it might be even better for debugging/logging purposes since you know where to investigate a possible bug or error ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM