Thursday, February 09, 2012
 
The best way to analyze your logs! Minimize
 Log4Net Mail archive   

The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.

Subject: Log4net: context only for exception
Prev Next
You are not authorized to post a reply.

Author Messages
koger

06/18/2009 2:27 PM  


Hi

I also posted this question at
http://stackoverflow.com/questions/1011730/log4net-context-only-for-exception
http://stackoverflow.com/questions/1011730/log4net-context-only-for-exception
. But I think this might be a better place. Here is the question:

I'm looking for a way to collect a set of data, that will only be used for
debugging, the data should only be logged if I log an exception. AFAIK it
can happend when ILog.Error, Fatal or Debug with an exception argument when
logging other data with an exception, the data shouldn't be logged.

I plan to use the GlobalContext or ThreadContext for building the dataset.

My idea was to hook into Log4Net and attach to an event I would imagine, to
alter the message pattern to include contexts. But I can't find any event
that would help me, perhaps there also might be a easier way?

What do you think about the overall design of this, am I on the right track
or am I missing something?

If this way is good, how can I implement it?

Ron Grabowski

06/18/2009 11:18 PM  


You can make two different appenders: one that handles DEBUG statements and one that handles ERROR and higher messages or you could change the layout based on the level of the message:

<layout type="ConsoleApplication1.LevelPatternLayout, ConsoleApplication1">
      <levelConversionPattern>
        <level value="DEBUG" />
        <conversionPattern value="%level - %message%newline" />
      </levelConversionPattern>
      <levelConversionPattern>
        <level value="ERROR" />
        <conversionPattern value="%level - %logger - %property{ErrorUserId} - %message%newline" />
      </levelConversionPattern>
 </layout>

public class LevelPatternLayout : PatternLayout
{
    private readonly Hashtable m_levelToPatternLayout = new Hashtable();

    public override void Format(TextWriter writer, LoggingEvent loggingEvent)
    {
        var patternLayout = m_levelToPatternLayout[loggingEvent.Level] as PatternLayout;

        if (patternLayout == null)
        {
            base.Format(writer, loggingEvent);
        }
        else
        {
            patternLayout.Format(writer, loggingEvent);
        }
    }

    public void AddLevelConversionPattern(LevelConversionPattern levelLayout)
    {
        m_levelToPatternLayout[levelLayout.Level] = new PatternLayout(levelLayout.ConversionPattern);
    }
}

public class LevelConversionPattern
{
    public Level Level { get; set; }
    public string ConversionPattern { get; set; }
}

You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > Log4net: context only for exception



ActiveForums 3.7

 

 

 

 

 

 

 

 

Log4Net Dashboard

Log analysis and monitoring made easy!

Log4Net Dashboard is a log viewer that can read log statements from a variety of logging output targets.

You can download a free developer version.

  

Check it out!

On the demonstration site you can try it  with live data.demo.l4ndash.com - Try Log4Net Dashboard with live data

The mail archive is a copy of all the mail sent to the mail address: log4net-user@logging.apache.org, organized as a forum.

If you would like to participate in the mail list, send a mail to log4net-user-subscribe@logging.apache.org.

More information about the mailing list is available on: http://logging.apache.org/log4net/support.html

 

A complete topic list is available and can be viewed here (warning, it takes some time to load)

 

Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement