Log4net strategy for named logs?

I usually declare the following in each class:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

and use the static member in each class to register at different levels (information, debugging, etc.)

I saw this somewhere and used it somewhat thoughtlessly, arguing that the setup is flexible enough to help me filter the namespace and write individual types if I would like to find ways to fix performance issues and what not.

But I rarely had to use this "level" of accurate logging. So, I would like to see what others are using. Do you use the above, since I feel that many are using just that, or are you creating the names of loggers like "debug", "trace", "error", "moduleA", etc., and share the logger between the different types assembly?

+5
source share
4 answers

I mainly use

public class MyClass
{
    private static readonly ILog log = Log.Get<MyClass>();
}

where Log.Get is the class that basically does this inside

return LogManager.GetLogger(typeof(T));

The launch cost is even less than the reflection method, as well as a cleaner imo.

:, , , , . ( , OP) , , .

, : ILog . , , Log, , .

ILog, , , DI :

public class MyClass
{
    readonly ILog _log;

    public class MyClass(ILog log)
    {
        _log = log;
    }
}

. , . , . log4net Autofac.

+11

- ILog , "System.Reflection.MethodBase.GetCurrentMethod(). DeclaringType" .

, , 1 10000 , 10 . .

​​ , , , log4net.

. , .. :

= System.Diagnostics.StackTrace(). GetFrame (1).GetMethod();

, .

+2

, , PostSharp (http://www.postsharp.org/). (-). , , . , .

0
source

Recently, I created something like this:

public static class Logger
{
    private static bool isLoaded = false;       

    public static ILog Log
    {
        get
        {
            System.Reflection.MethodBase method;
            method = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod();
            StringBuilder loggerName = new StringBuilder();
            loggerName.AppendFormat("{0}.{1}(", method.ReflectedType.FullName, method.Name);

            ParameterInfo[] parameters = method.GetParameters();
            string[] parametersStr = new string[parameters.Length];

            if (parameters.Length > 0)
            {
                for (int i = 0; i < parameters.Length; i++)
                {
                    parametersStr[i] = parameters[i].ToString();
                }
                loggerName.Append(String.Join(", ", parametersStr));
            }

            loggerName.Append(")");

            return GetLogger(loggerName.ToString());
        }
    }


    private static ILog GetLogger(string loggerName)
    {
        if (!isLoaded)
        {
            log4net.Config.XmlConfigurator.Configure();
        }
        return LogManager.GetLogger(loggerName);
    }
}

it allows me to use log4net without creating an instance of it in each class, I have to use it, and I still get the name and method of the class in which I registered the action.

Sample Usage:

Logger.Log.DebugFormat("Response [{0}]", xmlResponse);
-1
source

All Articles