Log4net
  1. Log4net
  2. LOG4NET-157

FAQ for getting the fully-qualified name of a class

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.2.10
    • Fix Version/s: 1.2.11
    • Component/s: Documentation
    • Labels:
      None
    • Environment:
      N/A

      Description

      The FAQ here--

      http://logging.apache.org/log4net/release/faq.html

      states that the following are equivalent ways to obtain a logger:

      (1) private static readonly ILog log = LogManager.GetLogger(typeof(Foo));

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

      However, these constructs seem to behave differently when the classes involved are generic. When generic classes are involved, I believe the former generates a different logger for each type parameter combination, while the latter generates a different logger if and only if the number of type parameters is different.

      For example,

      TestClass<T, S> can yield "TestClass`2[[System.Int32..." in the former case and "TestClass`2" in the latter.

        Issue Links

          Activity

          Chris Jerdonek created issue -
          Ron Grabowski made changes -
          Field Original Value New Value
          Assignee Ron Grabowski [ ron ]
          Affects Version/s 1.2.10 [ 11128 ]
          Fix Version/s 1.2.11 [ 12310980 ]
          Hide
          Ron Grabowski added a comment -

          class TestClass<T, K>
          {
          private static ILog log1 = LogManager.GetLogger(typeof(TestClass<T,K>));
          private static ILog log2 = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

          public TestClass(T arg0, K arg1)
          {

          }
          }

          /* ------------------------------------------------------------------------------------------------ */

          log4net.Config.XmlConfigurator.Configure();

          TestClass<int, int> testClass1 = new TestClass<int, int>(1, 2);
          TestClass<string, int> testClass2 = new TestClass<string, int>("1", 2);
          TestClass<string, string> testClass3 = new TestClass<string, string>("1", "2");

          Console.WriteLine();
          Console.WriteLine();

          foreach (ILog log in LogManager.GetCurrentLoggers())

          { Console.WriteLine(log.Logger.Name); Console.WriteLine(); }

          /* ------------------------------------------------------------------------------------------------ */

          ConsoleApplication2.Program+TestClass`2[[System.Int32, mscorlib, Version=2.0.0.0
          , Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Ver
          sion=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

          ConsoleApplication2.Program+TestClass`2

          /* ------------------------------------------------------------------------------------------------ */

          I'm leaning towards changing the verbage on the webpage to recommend using typeof() when working with generic classes.

          Show
          Ron Grabowski added a comment - class TestClass<T, K> { private static ILog log1 = LogManager.GetLogger(typeof(TestClass<T,K>)); private static ILog log2 = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public TestClass(T arg0, K arg1) { } } /* ------------------------------------------------------------------------------------------------ */ log4net.Config.XmlConfigurator.Configure(); TestClass<int, int> testClass1 = new TestClass<int, int>(1, 2); TestClass<string, int> testClass2 = new TestClass<string, int>("1", 2); TestClass<string, string> testClass3 = new TestClass<string, string>("1", "2"); Console.WriteLine(); Console.WriteLine(); foreach (ILog log in LogManager.GetCurrentLoggers()) { Console.WriteLine(log.Logger.Name); Console.WriteLine(); } /* ------------------------------------------------------------------------------------------------ */ ConsoleApplication2.Program+TestClass`2[[System.Int32, mscorlib, Version=2.0.0.0 , Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Ver sion=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] ConsoleApplication2.Program+TestClass`2 /* ------------------------------------------------------------------------------------------------ */ I'm leaning towards changing the verbage on the webpage to recommend using typeof() when working with generic classes.
          Ron Grabowski made changes -
          Link This issue relates to LOG4NET-136 [ LOG4NET-136 ]
          Hide
          Ron Grabowski added a comment -

          LOG4NET-136 has an attachment showing how a CLR generic class representation (backtick, [[ ]]) can be output using the more conventional C# < > notation.

          Show
          Ron Grabowski added a comment - LOG4NET-136 has an attachment showing how a CLR generic class representation (backtick, [[ ]]) can be output using the more conventional C# < > notation.
          Hide
          Stefan Bodewig added a comment -

          FAQ modified with svn revision 1173199

          I didn't recommend one approach over the other as sometimes it may be desirable to have one logger for all generic instances while different loggers may be more appropriate in other cases.

          Show
          Stefan Bodewig added a comment - FAQ modified with svn revision 1173199 I didn't recommend one approach over the other as sometimes it may be desirable to have one logger for all generic instances while different loggers may be more appropriate in other cases.
          Stefan Bodewig made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Resolution Fixed [ 1 ]
          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open Resolved Resolved
          1214d 5h 54m 1 Stefan Bodewig 20/Sep/11 16:41

            People

            • Assignee:
              Ron Grabowski
              Reporter:
              Chris Jerdonek
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development