views:

2549

answers:

2

This is similar to 650694 but no answer was accepted there, I can't get any of those suggestions to work at all, and I suspect I may be in a slightly different situation.

I'm calling log4net.Config.XmlConfigurator.Configure(). But after that point in the program, I want to change the logging threshold to a value only known at runtime.

From the other question, I tried:

((log4net.Repository.Hierarchy.Logger)mylogger.Logger).Level = log4net.Core.Level.Error;

and:

var appender = new log4net.Appender.ColoredConsoleAppender();
appender.Layout = new log4net.Layout.PatternLayout(@"%date %-5level %message%newline");
appender.Threshold = log4net.Core.Level.Error;
appender.ActivateOptions();
log4net.Config.BasicConfigurator.Configure(appender);

but neither one seems to have any effect: I'm still seeing DEBUG and INFO logging statements on the console.

My hunch is that I'm adding a new appender, which has no effect on the appender declared in the XML config (which tells it to print DEBUG level messages), but I don't have any evidence for this yet.

I've been digging through the log4net API for a while now, and I'm just not seeing it. Is there something simple I'm missing?

+7  A: 

Finally found a working solution, here.

The big pieces were:

  • need to set the threshold on all loggers, including the "cannot be retrieved by name" root logger
  • need to get the Level from the Hierarchy's LevelMap

Big thanks to Eddie for asking good pointed questions, which led me to google the right words. I never would have figured this out alone.

(Aside: Repository, Hierarchy, Logger, RootLogger, LevelMap -- I had no idea it was even possible to make a logging library this complex. It's got about 20 layers of indirection, which I'm sure makes it flexible enough for anything, but makes it nearly impossible to do simple things like "don't log any messages above threshold X". Gah!)

Ken
Yow, that's more complex than I would have imagined! Good link. My answer from question 650694 has worked for me in multiple applications, at least with log4Net 1.2.10. I don't know why it didn't work for you, but what you found is *thorough*!
Eddie
A: 

And some code ; )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using NUnit.Framework;

namespace ExampleConsoleApplication
{
    [TestFixture]
    class TestClass
    {

    //private static readonly ILog logger =
    //     LogManager.GetLogger ( typeof ( TestClass ) );

    private static readonly log4net.ILog logger = log4net.LogManager.GetLogger ( System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType );

     static void Main ( string[] args )
     {

      Console.WriteLine ( " START " );
      #region LoggerUsage
      DOMConfigurator.Configure (); //tis configures the logger 
      logger.Debug ( "Here is a debug log." );
      logger.Info ( "... and an Info log." );
      logger.Warn ( "... and a warning." );
      logger.Error ( "... and an error." );
      logger.Fatal ( "... and a fatal error." );

      #endregion LoggerUsage
      TestClass objTestClass = new TestClass();
      objTestClass.TestMethodNameOK ();
      objTestClass.TestMethodNameNOK ();

      Console.WriteLine ( " END HIT A KEY TO EXIT " );
      Console.ReadLine ();
      } //eof method 

     [SetUp]
     protected void SetUp ()
     {
      //Add Here the Initialization of the objects 
     }
     [Test ( Description = "Add here the description of this test method " )]
     protected void TestMethodNameOK ()
     { 
      //build ok use case scenario here - e.g. no exception should be raced '
      //Vegetable newCarrot = pool.GetItemByPropertyValue<Vegetable> ( "WriongByPurpose", "Orange" );
      //Assert.IsInstanceOfType ( typeof ( Vegetable ), newCarrot );
      //Assert.AreSame ( newCarrot, carrot );
      //logger.Info ( " I got the newCarrot which is " + newCarrot.Color );

     } //eof method 

     [Test ( Description = "Add here the description of this test method " )]
     protected void TestMethodNameNOK ()   //e.g. the one that should raze Exception
     {
      //build ok use case scenario here - e.g. no exception should be raced '
      //Vegetable newCarrot = pool.GetItemByPropertyValue<Vegetable> ( "WriongByPurpose", "Orange" );
      //Assert.IsInstanceOfType ( typeof ( Vegetable ), newCarrot );
      //Assert.AreSame ( newCarrot, carrot );
      //logger.Info ( " I got the newCarrot which is " + newCarrot.Color );

     } //eof method 

    } //eof class 

} //eof namespace 





#region TheAppConfig
/*
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
     <section name="log4net"
        type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>

    <log4net>
     <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="Program.log" />
      <param name="AppendToFile" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <!--<param name="Header" value="======================================" />
        <param name="Footer" value="======================================" />-->
        <param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
      </layout>
     </appender>

     <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red" />
      </mapping>
      <mapping>
        <level value="DEBUG" />
        <foreColor value="HighIntensity" />
      </mapping>
      <mapping>
        <level value="INFO" />
        <foreColor value="Green" />
      </mapping>
      <mapping>
        <level value="WARN" />
        <foreColor value="Yellow" />
      </mapping>
      <mapping>
        <level value="FATAL" />
        <foreColor value="White" />
        <backColor value="Red" />
      </mapping>

      <layout type="log4net.Layout.PatternLayout">
       <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
     </appender>


     <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.2.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="data source=ysg;initial catalog=DBGA_DEV;integrated security=true;persist security info=True;" />
      <commandText value="INSERT INTO [DBGA_DEV].[ga].[tb_Data_Log] ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />

      <parameter>
       <parameterName value="@log_date" />
       <dbType value="DateTime" />
       <layout type="log4net.Layout.PatternLayout" value="%date{yyyy'.'MM'.'dd HH':'mm':'ss'.'fff}" />
      </parameter>
      <parameter>
       <parameterName value="@thread" />
       <dbType value="String" />
       <size value="255" />
       <layout type="log4net.Layout.PatternLayout" value="%thread" />
      </parameter>
      <parameter>
        <parameterName value="@domainName" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout" value="%user" />
      </parameter>
      <parameter>
       <parameterName value="@log_level" />
       <dbType value="String" />
       <size value="50" />
       <layout type="log4net.Layout.PatternLayout" value="%level" />
      </parameter>
      <parameter>
       <parameterName value="@logger" />
       <dbType value="String" />
       <size value="255" />
       <layout type="log4net.Layout.PatternLayout" value="%logger" />
      </parameter>
      <parameter>
       <parameterName value="@message" />
       <dbType value="String" />
       <size value="4000" />
       <layout type="log4net.Layout.PatternLayout" value="%message" />
      </parameter>
     </appender>
     <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="AdoNetAppender" />
      <appender-ref ref="ColoredConsoleAppender" />
     </root>
    </log4net>
</configuration>
*/
#endregion TheAppconfig

 //this is the xml added replace here your log4net and Nunit paths
//<Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
     //  <SpecificVersion>False</SpecificVersion>
     //  <HintPath>..\..\..\Log4Net\log4net-1.2.10\bin\net\2.0\release\log4net.dll</HintPath>
     //</Reference>
     //<Reference Include="nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
YordanGeorgiev