The current implementation of LogManager.GetLogger returns 
log4net.Core.LogImpl. In its constructor, LogImpl queries its repository and 
stores the current level mappings. The LogImpl isn't aware of LevelMap changes 
after its been created. Maybe changing a repository's LevelMap should trigger 
the ConfigurationChanged event. It may be possible to make your own LogImpl 
that always looks up Level values from the repository.

On a related note, I can't figure out why this is failing:

StringAppender stringAppender = new StringAppender();
stringAppender.Layout = new PatternLayout("%message");
string repositoryId = Guid.NewGuid().ToString();
ILoggerRepository repository = LogManager.CreateRepository(repositoryId);
repository.LevelMap.Add(Level.Warn.Name, Level.Off.Value);
BasicConfigurator.Configure(repository, stringAppender);
ILog log = LogManager.GetLogger(repositoryId, "TestFormatString");
log.Warn("Warn acting as Off. This message should not appear.");
Assert.IsEmpty(stringAppender.GetString());

One option is to add a Filter to all of your appenders that checks for a 
property in the repository then aborts the logging call if necessary:

log.Logger.Repository.Properties["DEBUG-DISABLED"] = true;
foo();
log.Logger.Repository.Properties.Remove("DEBUG-DISABLED");

There may be other options if I can figure out why my test code is failing.

----- Original Message ----

From: Ramaa Davanagere <[EMAIL PROTECTED]>

To: Log4NET User <[email protected]>

Sent: Wednesday, September 27, 2006 7:38:16 AM

Subject: RE: Turn off logging for a specific event only



           <!--  _filtered {font-family:"Comic Sans MS"; panose-1:3 15 7 2 3 3 
2 2 2 4;} /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal     
 {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New 
Roman";} a:link, span.MsoHyperlink         {color:blue; 
text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed   
{color:purple; text-decoration:underline;} p.MsoPlainText, li.MsoPlainText, 
div.MsoPlainText    {margin:0in; margin-bottom:.0001pt; font-size:10.0pt; 
font-family:"Comic Sans MS"; color:navy;}  _filtered { margin:1.0in 81.65pt 
1.0in 81.65pt;} div.Section1  {} -->        

  Below is the structure of my code. In my constructor, I load my configuration 
file and configure my log4net logger.

   

  myFunction() method gets called from the Main() method. As you can see, when 
entering the myFunction() method, I want to set the log level to OFF and then 
reset it to DEBUG while exiting the function. The myFunction() methods calls 
several other components and methods (for eg: a,b, c, etc) to do stuff and 
hence passing the Boolean variable to this method will not work. Also, having 
an separate appender will not work in my scenario, since the methods a, b and c 
will be used in various other places as well.

   

  public myConstructor()

  {

              if (logconfigpath == null)

              {

                          logconfigpath = 
System.AppDomain.CurrentDomain.BaseDirectory;

                          logconfigpath += 
System.AppDomain.CurrentDomain.RelativeSearchPath;

                          log4net.Config.XmlConfigurator.Configure(new 
System.IO.FileInfo(Path.GetFullPath(logconfigpath) + 
Path.DirectorySeparatorChar.ToString() + "myconfigfile.xml"));

              }

  }

   

  public void Main(XmlElement oParameter1, string sParameter2, string 
sParameter3, string sParameter4)

  {

              logger.Info(System.Reflection.MethodBase.GetCurrentMethod() + ": 
Entering.");

              

              //do my stuff here.

              

              String sResult = myFunction (oParameter1, sParameter2);

              

              logger.Info(System.Reflection.MethodBase.GetCurrentMethod() + ": 
Leaving.");

  }

   

  private string myFunction(XmlElement oParameter1, string sParameter2)

  {

          //turn off logging here...I would like to set the loglevel to OFF

          

          //do my stuff here.

          

          //turn on the logging .....I would like to reset the loglevel to DEBUG

   

  }

   

  After looking around on the internet, I found this code

   

  log4net.Repository.Hierarchy.Logger logger2 = 
(log4net.Repository.Hierarchy.Logger)log4net.LogManager.GetLogger("myappendername").Logger);

  logger2.Level = logger2.Repository.LevelMap["OFF"];

   

  But this is not working as expected.  Log messages are still being writing to 
my log file, even though the level is set to OFF. Obviously, I'm missing 
something here. 

   

  Please help

   

  Thank you

   

  -Ramaa

   

  -----Original Message-----

 From: Ron Grabowski [mailto:[EMAIL PROTECTED] 

 Sent: Tuesday, September 26, 2006 7:04 PM

 To: Log4NET User

 Subject: Re: Turn off logging for a specific event only

   

  Will this work?

   

  private int calculateBigNumber(bool loggingEnabled)

   

  {

   

   if (loggingEnabled && log.IsDebugEnabled)

   

   {

   

    log.Debug("Entering method");

   

   }

   

   int result = 0;

   

   for (int i=0;i<100;i++)

   

   {

   

    result += i;

   

   }

   

   if (loggingEnabled && log.IsDebugEnabled)

   

   {

   

    log.Debug("Leaving method");

   

   }

   

   return result;

   

  }

   

   

   

  ----- Original Message ----

   

  From: Ramaa Davanagere <[EMAIL PROTECTED]>

   

  To: "[email protected]" <[email protected]>

   

  Sent: Tuesday, September 26, 2006 2:28:19 PM

   

  Subject: Turn off logging for a specific event only

   

   

   

             <!--  _filtered {font-family:"Comic Sans MS"; panose-1:3 15 7 2 3 
3 2 2 2 4;} /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal   
  {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New 
Roman";} a:link, span.MsoHyperlink          {color:blue; 
text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed             
{color:purple; text-decoration:underline;} span.EmailStyle17     
{font-family:"Comic Sans MS"; color:windowtext; font-weight:normal; 
font-style:normal; text-decoration:none none;}  _filtered { margin:1.0in 1.25in 
1.0in 1.25in;} div.Section1      {} -->        

   

    Is it possible to turn off the logging for a specific event? I would like 
to turn of the logging when the code hits a particular method and reset the 
logging to its original value which is "DEBUG" while exiting that method. Is it 
possible to accomplish this? If so, please provide me some sample code on how 
to achieve this. My code is in c#.net and uses log4net version 1.2.10

   

     

   

    Please help 

   

     

   

    Thanks.

   

     

   

    -Ramaa

   

    

   

    

   

   

   

   

   

   

  

  







Reply via email to