Morton,

It does look like the ToString() method of Type is defined as returning
the generic parameter types without assembly qualifiers so that would
probably do what you need.

i.e. you can do:

log4net.LogManager.GetLogger(typeof(MyGenericClass<T,K>).ToString());

As this gives a logger name of:

TestApplication.MyGenericClass`2[TestApplication.TClass,TestApplication.
KClass]


This does open up a wider question. Because log4net loggers are held in
a hierarchy based on the dots in the name, is it right to consider the
logger named
"TestApplication.MyGenericClass`2[TestApplication.TClass,TestApplication
" a child of the logger named
"TestApplication.MyGenericClass`2[TestApplication"?

How would you enable logging for this type?

<logger name="TestApplication.MyGenericClass">
  <level value="debug" />
</logger>

No, this won't work as there isn't a logger with this name.


<logger name="TestApplication.MyGenericClass`2[TestApplication">
  <level value="debug" />
</logger>

This will work for all MyGenericClass<T,K> where T is either a root
level class called TestApplication or T is in the TestApplication global
namespace.


The only way to fully qualify the logger is to use the full type name:

<logger
name="TestApplication.MyGenericClass`2[TestApplication.TClass,TestApplic
ation.KClass]">
  <level value="debug" />
</logger>


I think it would be better if the generic class itself appeared as a
logger in the hierarchy, i.e. you could do something like:

<logger name="TestApplication.MyGenericClass`2">
  <level value="debug" />
</logger>

To enable logging for MyGenericClass<T,K> regardless of the 2 generic
type parameters. To do this we would need a dot after the
"TestApplication.MyGenericClass`2". i.e.:

TestApplication.MyGenericClass`2.[TestApplication.TClass,TestApplication
.KClass]

Obviously none of the builtin methods are going to produce this output
for us.

Cheers,
Nicko


> -----Original Message-----
> From: Morten Andersen [mailto:[EMAIL PROTECTED] 
> Sent: 02 May 2006 06:55
> To: Log4NET Dev
> Subject: Re: Problem when using generic classes
> 
> My guess was that Class<SubClass, SubClass2> is the most 
> common way to display a generic class. Anyway. The problem is 
> not the [] instead of <>, but the Version, Culture, and 
> PublicKeyToken.
> 
> typeof(MyGenericClass<MyClass, TestClass>).ToString()
> 
> result:
> 
> TestApplication.MyGenericClass`2[TestApplication.MyClass,TestA
> pplication.TestClass]
> 
> 
> - Morten
> 
> Nicko Cadell wrote:
> > Morten,
> >
> > The LogManager.GetLogger(Type) method uses the 
> Type.FullName property 
> > to get a string that represents the type name.
> > In your case the output is correct. 
> >
> > TestApplication.MyGenericClass`1[[TestApplication.MyClass,
> > TestApplication, Version=1.0.0.0, Culture=neutral, 
> > PublicKeyToken=null]]
> >   
> 
> 

Reply via email to