Log4net
  1. Log4net
  2. LOG4NET-106

TraceAppender : Add switch to disable using logger name as trace category

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Trivial Trivial
    • Resolution: Fixed
    • Affects Version/s: 1.2.9
    • Fix Version/s: 1.2.11
    • Component/s: Appenders
    • Labels:
      None

      Description

      TraceAppender now always put logger name as trace category when writing to System.Diagnostics.Trace.Write
      ie. :
      System.Diagnostics.Trace.Write(RenderLoggingEvent(loggingEvent), loggingEvent.LoggerName);

      This resulted in logger's name appearing twice in Trace logs if once use/share the same conversion for both file-based and trace appenders, eg.
      "%date [%thread] %-5level %logger - %message%"

      It would be nice to have a settable property on TraceAppender to disable using logger's name as trace category, ie. :

      public bool UseLoggerNameAsTraceCategory
      {
      get; set;
      }

      ...
      if (m_useLoggerNameAsTraceCategory)
      {
      System.Diagnostics.Trace.Write(RenderLoggingEvent(loggingEvent), loggingEvent.LoggerName);
      }
      else
      {
      System.Diagnostics.Trace.Write(RenderLoggingEvent(loggingEvent), null);
      }

        Issue Links

          Activity

          Hide
          Ron Grabowski added a comment -

          Do you think other users might want to pass in something besides null or the LoggerName?

          We could define the Category to be an ITraceAppenderCategory and ship with LoggingNameCategory, NullCategory, UserNameCategory, etc.:

          <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
          <category type="log4net.Appender.TraceAppender+LoggingNameCategory" />
          <layout type="log4net.Layout.SimplePatternLayout" />
          </appender>

          <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
          <category type="log4net.Appender.TraceAppender+UserNameCategory" />
          <layout type="log4net.Layout.SimplePatternLayout" />
          </appender>

          <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
          <category type="log4net.Appender.TraceAppender+NullCategory" />
          <layout type="log4net.Layout.SimplePatternLayout" />
          </appender>

          similiar to how the RollingFileAppender supports IDateTime for calculating dates in terms of local time or universal time.

          I would just write the 24-ish lines of code and go about my day:

          // untested
          public class NullCategoryTraceAppender : TraceAppender
          {
          private bool nullCategory = false;
          protected override void Append(LoggingEvent loggingEvent)
          {
          if (NullCategory)

          { Trace.Write(RenderLoggingEvent(loggingEvent), null); }

          else

          { Trace.Write(RenderLoggingEvent(loggingEvent), loggingEvent.LoggerName); }

          if (ImmediateFlush)

          { Trace.Flush(); }

          }
          public bool NullCategory
          {
          get

          { return nullCategory; }

          set

          { nullCategory = value; }

          }
          }

          Show
          Ron Grabowski added a comment - Do you think other users might want to pass in something besides null or the LoggerName? We could define the Category to be an ITraceAppenderCategory and ship with LoggingNameCategory, NullCategory, UserNameCategory, etc.: <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <category type="log4net.Appender.TraceAppender+LoggingNameCategory" /> <layout type="log4net.Layout.SimplePatternLayout" /> </appender> <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <category type="log4net.Appender.TraceAppender+UserNameCategory" /> <layout type="log4net.Layout.SimplePatternLayout" /> </appender> <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <category type="log4net.Appender.TraceAppender+NullCategory" /> <layout type="log4net.Layout.SimplePatternLayout" /> </appender> similiar to how the RollingFileAppender supports IDateTime for calculating dates in terms of local time or universal time. I would just write the 24-ish lines of code and go about my day: // untested public class NullCategoryTraceAppender : TraceAppender { private bool nullCategory = false; protected override void Append(LoggingEvent loggingEvent) { if (NullCategory) { Trace.Write(RenderLoggingEvent(loggingEvent), null); } else { Trace.Write(RenderLoggingEvent(loggingEvent), loggingEvent.LoggerName); } if (ImmediateFlush) { Trace.Flush(); } } public bool NullCategory { get { return nullCategory; } set { nullCategory = value; } } }
          Hide
          Chi Lang added a comment -

          I just want the simplest solutions that works so I would wait with
          generalization until we'll actually see another user request with
          TraceAppender.

          I like your config change

          <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
          <category type="log4net.Appender.TraceAppender+NullCategory" />
          <layout type="log4net.Layout.SimplePatternLayout" />
          </appender>

          but would rather still add an property to TraceAppender rather than extends
          it.

          Show
          Chi Lang added a comment - I just want the simplest solutions that works so I would wait with generalization until we'll actually see another user request with TraceAppender. I like your config change <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <category type="log4net.Appender.TraceAppender+NullCategory" /> <layout type="log4net.Layout.SimplePatternLayout" /> </appender> but would rather still add an property to TraceAppender rather than extends it.
          Hide
          Nicko Cadell added a comment -

          How about just using a PatternLayout to render the string for the category name:

          <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
          <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger [%ndc] %property - %message%newline" />
          <category value="%logger" />
          </appender>

          This would give you the current behaviour.
          To specify the user as the category use:

          <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
          <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger [%ndc] %property - %message%newline" />
          <category value="%username" />
          </appender>

          This allows static strings as well as dynamic values.

          Show
          Nicko Cadell added a comment - How about just using a PatternLayout to render the string for the category name: <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger [%ndc] %property - %message%newline" /> <category value="%logger" /> </appender> This would give you the current behaviour. To specify the user as the category use: <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger [%ndc] %property - %message%newline" /> <category value="%username" /> </appender> This allows static strings as well as dynamic values.
          Hide
          Ron Grabowski added a comment -

          r607786 doesn't address how to pass in a null category. This may work:

          <category value="" />

          but its probably better to explicitly express the desire to call a Trace.Write overload by using this syntax:

          <useCategory value="false" />

          Show
          Ron Grabowski added a comment - r607786 doesn't address how to pass in a null category. This may work: <category value="" /> but its probably better to explicitly express the desire to call a Trace.Write overload by using this syntax: <useCategory value="false" />
          Hide
          Ron Grabowski added a comment -

          Fixed in r607786.

          Show
          Ron Grabowski added a comment - Fixed in r607786.

            People

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

              Dates

              • Created:
                Updated:
                Resolved:

                Development