I think you are on the right track with the filter idea. You will need
to write a custom filter to look at the value of your userID parameter.
It could look something like this:

public class UserParamFilter : log4net.Filter.FilterSkeleton
{
  private long m_userID;

  public long UserID
  {
    get { return m_userID; }
    set { m_userID = value; }
  }

  public override log4net.Filter.FilterDecision
Decide(log4net.Core.LoggingEvent loggingEvent)
  {
    object val = loggingEvent.LookupProperty("UserID");
    if (val != null && val is long)
    {
      if (m_userID == (long)val)
      {
        return log4net.Filter.FilterDecision.Accept;
      }
    }
    return log4net.Filter.FilterDecision.Neutral;
  }
}

You may want to get your allowed users IDs from somewhere or you may
need to pass in a list etc. You could do this by writing an
AddUserID(long userId) method which built up a list.

You can then configure your appender with a threshold and filter chain
something like this:

<appender name="ConsoleAppender_Filter3"
type="log4net.Appender.ConsoleAppender" >
  <layout type="log4net.Layout.PatternLayout" value="%date %-5level
%logger - %message%n" />

  <!-- Reject anything below DEBUG -->
  <threshold value="DEBUG" />

  <!-- Accept anything of INFO or above -->
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="INFO" />
    <levelMax value="OFF" />
  </filter>

  <!-- Update the type value to the assembly qualified name of your
filter -->
  <filter type="TestConsoleApp.UserParamFilter, TestConsoleApp">
    <userId value="123" />
  </filter>

  <!-- Deny events that do not match any of the above filters -->
  <filter type="log4net.Filter.DenyAllFilter" />

</appender> 

Cheers,

Nicko

> -----Original Message-----
> From: Burger, Erik [mailto:[EMAIL PROTECTED] 
> Sent: 21 April 2005 12:44
> To: Log4NET User
> Subject: Filtering on custom parameter
> 
> Hi,
>  
> I have an ADONetAppender set up with a custom parameter 
> 'userID'. I would like to be able to log DEBUG (or greater) 
> messages for a certain user (or number of users) only, and 
> have the other users log only INFO (or greater) messages. 
> INFO is my master log level.
>  
> How would I go about doing this? Would one way of doing it be 
> setting the master log level to DEBUG and using a filter to 
> drop all DEBUG messages from anyone but the specified users?
>  
> Am I making sense at all??
>  
> Thanks in advance,
> Erik
>  
> -------------------------------
> Lead Developer
> Research and Development Group
> GPXS Wireless Ltd.
> Southampton, United Kingdom
> Phone: +44 (0) 23 8076 2519
> Fax:   +44 (0) 23 8076 2555
> Email: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> 
>  
> 
> 
> The information transmitted by this e-mail message is 
> intended solely for the use of the person to whom or entity 
> to which it is addressed. The message may contain information 
> that is privileged and confidential. Disclosure, 
> dissemination, distribution, review, retransmission to, other 
> use of or taking any action in reliance upon this information 
> by anyone other than the intended recipient is prohibited. If 
> you are not the intended recipient, please do not 
> disseminate, distribute or copy this communication, by e-mail 
> or otherwise. Instead, please notify us immediately by return 
> e-mail (including the original message with your reply) and 
> then delete and discard all copies of the message.
> 
> 
> Although we have taken precautions to minimize the risk of 
> transmitting viruses we nevertheless advise you to carry out 
> your own virus checks on any attachment to this message. We 
> accept no liability for any loss or damage caused by viruses.
> 
> 

Reply via email to