You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-dev@logging.apache.org by "Ron Grabowski (JIRA)" <ji...@apache.org> on 2009/05/24 19:27:45 UTC

[jira] Commented: (LOG4NET-136) logger conversionPattern restriction doesn't work correctly for Generic classes

    [ https://issues.apache.org/jira/browse/LOG4NET-136?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12712577#action_12712577 ] 

Ron Grabowski commented on LOG4NET-136:
---------------------------------------

I'm still surprised that I couldn't find example code that converts:

System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

into:

VB: System.Collections.Generic.List(Of System.Int32)
CS: System.Collections.Generic.List<System.Int32>

My implementation converts the string back into a Type and recurses through it:

        private string recursiveGetShortTypeString(Type type, string openToken, string closeToken)
        {
            // base case 
            string result = _displayTypeFullName ? type.FullName : type.Name;

            if (type.IsGenericType)
            {
                // recursive case
                result = result.Substring(0, result.IndexOf(GENERIC_TOKEN)) + openToken;

                Type[] genericArguments = type.GetGenericArguments();
                for (int i = 0; i < genericArguments.Length; i++)
                {
                    Type genericArgument = genericArguments[i];

                    result += recursiveGetShortTypeString(genericArgument, openToken, closeToken);

                    if (i != genericArguments.Length - 1)
                    {
                        result += GENERIC_SEPERATOR;
                    }
                }

                result += closeToken;
            }

            return result;
        }

It handles complex generic objects:

System.Collections.Generic.Dictionary`2[[System.Collections.Generic.Dictionary`2[[System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

VB: Dictionary(Of Dictionary(Of List(Of Int32), List(Of String)), List(Of String))
CS: Dictionary<Dictionary<List<Int32>, List<String>>, List<String>>

I also converted the library into a converter

<converter>
 <name value="csLogger" />
 <type value="ConsoleApplication3_GenericPrettyPrinter.CSharpLoggerPatternConverter, ConsoleApplication3-GenericPrettyPrinter" />
 </converter>

    public class CSharpLoggerPatternConverter : NamedPatternConverter
    {
        private readonly FullNameTypeConverter fullNameTypeConverter = new FullNameTypeConverter(true);

        protected override string GetFullyQualifiedName(LoggingEvent loggingEvent)
        {
            return fullNameTypeConverter.GetCSTypeString(loggingEvent.LoggerName);
        }
    }

that is based on the default LoggerPatternConverter (I made NamedPatternConverter non-internal): 

	internal sealed class LoggerPatternConverter : NamedPatternConverter 
	{
		override protected string GetFullyQualifiedName(LoggingEvent loggingEvent) 
		{
			return loggingEvent.LoggerName;
		}
	}

In theory you should be able to take advantage of the %logger{2} notation to limit class depth.

I suspect its faster to parse the generic type string instead of converting the string back into a Type. Will the Type always be loadable? 

Is that what you had in mind?


> logger conversionPattern restriction doesn't work correctly for Generic classes
> -------------------------------------------------------------------------------
>
>                 Key: LOG4NET-136
>                 URL: https://issues.apache.org/jira/browse/LOG4NET-136
>             Project: Log4net
>          Issue Type: Bug
>    Affects Versions: 1.2.10
>         Environment: Windows 2000 Professional, .NET Framework 2.0
>            Reporter: Tom Crossland
>            Assignee: Ron Grabowski
>            Priority: Minor
>             Fix For: 1.2.11
>
>         Attachments: ConsoleApplication3-GenericPrettyPrinter.zip
>
>
> <conversionPattern value="%date %-5level %logger{1} - %message%newline"/>
> Using the above conversion pattern for a logger of a Generic class (i.e. My.System.MyClass<My.System.MyObject>) will result in the following log output:
> 2008-01-17 21:54:48,968 INFO  0, Culture=neutral, PublicKeyToken=null]] -  My error message
> Obviously, in this case it's not appropriate to just take the portion of the class name after the last '.' character.
> Thanks

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.