Log4net
  1. Log4net
  2. LOG4NET-30

Add support for type aliases in config file

    Details

    • Type: New Feature New Feature
    • Status: Open
    • Priority: Trivial Trivial
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 1.2 Maintenance Release
    • Component/s: None
    • Labels:
      None

      Description

      IBatisNet uses "type" attributes in their xml config files:

      type="Company.Project.Foo.Data.Entity.Product, Company.Project.Data"
      type="Company.Project.Foo.Data.Entity.Category, Company.Project.Data"
      type="Company.Project.Foo.Data.Entity.User, Company.Project.Data"

      to save the user from having to re-type long strings every time something references a Product, Category, etc. They define an <alias> node that contains <typeAlias> entries:

      <alias>
      <typeAlias alias="Product" type="Company.Project.Foo.Data.Entity.Product, Company.Project.Data" />
      <typeAlias alias="Category" type="Company.Project.Foo.Data.Entity.Product, Company.Project.Data" />
      <typeAlias alias="User" type="Company.Project.Foo.Data.Entity.Product, Company.Project.Data" />
      </alias>

      Wherever I have to specify a type, I may use one of the aliases I defined at the top of the file.

      It would be nice if log4net supported a similiar concept. It would also be nice if log4net shipped with some default aliases. I think normal casing would be ok:

      FileAppender -> log4net.Appender.FileAppender

      An example incorporating the above suggestions would be (this will probably wrap when it posts to the mailing list):

      <alias>
      <typeAlias alias="SessionContextPatternConverter" type="log4netAspExtensions.SessionContextPatternConverter, log4netAspExtensions" />
      <typeAlias alias="CacheContextPatternConverter" type="log4netAspExtensions.CacheContextPatternConverter, log4netAspExtensions" />
      <typeAlias alias="RequestContextPatternConverter" type="log4netAspExtensions.RequestContextPatternConverter, log4netAspExtensions" />
      <typeAlias alias="ApplicationContextPatternConverter" type="log4netAspExtensions.ApplicationContextPatternConverter, log4netAspExtensions" />
      </alias>

      <appender name="LoginFileAppender" type="FileAppender">
      <file value="c:/inetpub/wwwroot/Logs/Logins.txt" />
      <layout type="PatternLayout">
      <converter>
      <name value="asp-session" />
      <type value="SessionContextPatternConverter" />
      </converter>
      <conversionPattern value="%5p %d (%c:%L) - [%asp-session

      {UserId}] %m%n" />
      </layout>
      </appender>

      I haven't fully looked into the <convertor> node to see if it can support this style of attributes but an even shorted syntax might be:

      <appender name="LoginFileAppender" type="FileAppender">
      <file value="c:/inetpub/wwwroot/Logs/Logins.txt" />
      <layout type="PatternLayout">
      <converter name="asp-session" type="SessionContextPatternConverter" />
      <conversionPattern value="%5p %d (%c:%L) - [%asp-session{UserId}

      ] %m%n" />
      </layout>
      </appender>

      It should be possible to overwrite the builtin aliases. Had I overridden FileAppender:

      <typeAlias alias="FileAppender" type="log4netAspExtensions.AspNetFileAppender, log4netAspExtensions" />

      I would be able to declare my appender with support for the ~ character:

      <appender name="LoginFileAppender" type="FileAppender">
      <file value="~/Logs/Login.txt" />
      <layout type="PatternLayout">
      <converter name="asp-session" type="SessionContextPatternConverter" />
      <conversionPattern value="%5p %d (%c:%L) - [%asp-session

      {UserId}

      ] %m%n" />
      </layout>
      </appender>

      I think that the above changes would descrease the size of the config file and increase the files readability.

        Activity

        Hide
        Ron Grabowski added a comment -

        It would also be nice if when declaring an <appender> node, I didn't always have to specify the complete namespace for the built-in appenders. Instead of having to do this:

        <appender name="LoginFileAppender" type="log4net.Appender.FileAppender">

        I could say:

        <appender name="LoginFileAppender" type="FileAppender">

        I think more people are using the built-in appenders rather than making their own.

        A small snippet of code starting on line 289 of XmlHierarchyConfigurator.cs could accomplish this:

        // prepend the default Appender namespace when using the short-hand notation
        if (typeName.IndexOf(".") < 0)

        { typeName = "log4net.Appender." + typeName; }

        IAppender appender = (IAppender)Activator.CreateInstance(SystemInfo.GetTypeFromString(typeName, true, true));

        That works well for Appenders, but I doubt the other nodes can be configured this way because of the way CreateObjectFromXml and SetParameter work.

        I noticed that NLog supports a <variable /> tag in their config file. This might be better than just having an <alias> node:

        <variable name="FileAppender" value="log4net.Appender.FileAppender" />
        <variable name="PatternLayout" value="log4net.Layout.PatternLayout" />
        <variable name="CustomXmlConversionPattern">
        <![CDATA[
        <Log>
        <Message>%m</Message>
        <Date>%d</Date>
        <Level>%p</Level>
        <Class>%c</Class>
        </Log>
        ]]>
        </variable>

        <appender name="LoginFileAppender" type="$

        {FileAppender}">
        <file value="Logins.txt" />
        <layout type="${PatternLayout}">
        <conversionPattern value="${CustomXmlConversionPattern}" />
        </layout>
        </appender>

        <appender name="GeneralFileAppender" type="${FileAppender}

        ">
        <file value="General.txt" />
        <layout type="$

        {PatternLayout}

        ">
        <conversionPattern value="$

        {CustomXmlConversionPattern}

        " />
        </layout>
        </appender>

        I wouldn't rank this high on the list of priorities.

        Show
        Ron Grabowski added a comment - It would also be nice if when declaring an <appender> node, I didn't always have to specify the complete namespace for the built-in appenders. Instead of having to do this: <appender name="LoginFileAppender" type="log4net.Appender.FileAppender"> I could say: <appender name="LoginFileAppender" type="FileAppender"> I think more people are using the built-in appenders rather than making their own. A small snippet of code starting on line 289 of XmlHierarchyConfigurator.cs could accomplish this: // prepend the default Appender namespace when using the short-hand notation if (typeName.IndexOf(".") < 0) { typeName = "log4net.Appender." + typeName; } IAppender appender = (IAppender)Activator.CreateInstance(SystemInfo.GetTypeFromString(typeName, true, true)); That works well for Appenders, but I doubt the other nodes can be configured this way because of the way CreateObjectFromXml and SetParameter work. I noticed that NLog supports a <variable /> tag in their config file. This might be better than just having an <alias> node: <variable name="FileAppender" value="log4net.Appender.FileAppender" /> <variable name="PatternLayout" value="log4net.Layout.PatternLayout" /> <variable name="CustomXmlConversionPattern"> <![CDATA[ <Log> <Message>%m</Message> <Date>%d</Date> <Level>%p</Level> <Class>%c</Class> </Log> ]]> </variable> <appender name="LoginFileAppender" type="$ {FileAppender}"> <file value="Logins.txt" /> <layout type="${PatternLayout}"> <conversionPattern value="${CustomXmlConversionPattern}" /> </layout> </appender> <appender name="GeneralFileAppender" type="${FileAppender} "> <file value="General.txt" /> <layout type="$ {PatternLayout} "> <conversionPattern value="$ {CustomXmlConversionPattern} " /> </layout> </appender> I wouldn't rank this high on the list of priorities.
        Hide
        Ron Grabowski added a comment -

        This may be a solution:

        http://www.mail-archive.com/log4net-dev%40logging.apache.org/msg01070.html

        "
        The $

        {foo}

        notation searches the environment for a key and uses its
        value. I wonder if it would be useful to introduce the idea of a local
        environment that would allow locally scoped temporary environment
        variables:

        <log4net>
        <localEnvironment name="LOG4NET_PATH" value="c:\logs\staging" />
        <localEnvironment name="DEFAULT_LAYOUT" value="%message%newline" />
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="$

        {LOG4NET_PATH}

        \log.txt" />
        <layout
        type="log4net.Layout.PatternLayout"
        value="$

        {DEFAULT_LAYOUT}

        " />
        </appender>

        If the item was not found in the local environment, System.Environment
        would be searched.
        "

        Show
        Ron Grabowski added a comment - This may be a solution: http://www.mail-archive.com/log4net-dev%40logging.apache.org/msg01070.html " The $ {foo} notation searches the environment for a key and uses its value. I wonder if it would be useful to introduce the idea of a local environment that would allow locally scoped temporary environment variables: <log4net> <localEnvironment name="LOG4NET_PATH" value="c:\logs\staging" /> <localEnvironment name="DEFAULT_LAYOUT" value="%message%newline" /> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="$ {LOG4NET_PATH} \log.txt" /> <layout type="log4net.Layout.PatternLayout" value="$ {DEFAULT_LAYOUT} " /> </appender> If the item was not found in the local environment, System.Environment would be searched. "

          People

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

            Dates

            • Created:
              Updated:

              Development