Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net pure code configuration with filter in c#

I am trying to configure Log4Net purely by code, but when I did with a minimal configuration, I was flooded by logging messages from NHibernate and the fluent interface.

So, what I am trying to do is simple. Tell Log4Net to show me only log messages of my single class. I toyed around a little bit, but can't figure it out...

Can anyone help, I think the following code illustrates my idea:

var filter = new log4net.Filter.LoggerMatchFilter();
filter.LoggerToMatch = typeof(DatabaseDirectory).ToString();
filter.AcceptOnMatch = false;

var x = new log4net.Appender.ConsoleAppender();
x.Layout = new log4net.Layout.SimpleLayout();
x.AddFilter(filter);

log4net.Config.BasicConfigurator.Configure(x);

Ok, thanks for your help, but there must be some issue here. But I get closer. I tried the XML configuration, which has much more documentation. And I managed to achieve the desired result using the following XML configuration. There must be some misconfiguration in the "pure code" version above.

The following XML configuration provides the "correct" output, which is not the same than the config in code above. Anybody sees the difference?

<log4net>
<root>
  <level value="DEBUG" />
  <appender-ref ref="ConsoleAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="Examples.FirstProject.Entities.DatabaseDirectory"/>
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="[%C.%M] %-5p %m%n" />
  </layout>
</appender>

like image 482
Christian Ruppert Avatar asked May 19 '09 22:05

Christian Ruppert


1 Answers

I figured it out.. Sometimes, just writing it down opens your eyes...

        var filter = new log4net.Filter.LoggerMatchFilter();
        filter.LoggerToMatch = typeof(DatabaseDirectory).ToString();
        filter.AcceptOnMatch = true;

        var filterDeny = new log4net.Filter.DenyAllFilter();


        var x = new log4net.Appender.ConsoleAppender();
        x.Layout = new log4net.Layout.SimpleLayout();
        x.AddFilter(filter);
        x.AddFilter(filterDeny);

        log4net.Config.BasicConfigurator.Configure(x);

See what was missing :-) The denyALL filter!!

Some more code examples:

    public static void AllToConsoleSetup()
    {
        var x = new log4net.Appender.ConsoleAppender { Layout = new log4net.Layout.SimpleLayout() };
        log4net.Config.BasicConfigurator.Configure(x);
        SetupDone = true;
    }


    public static void ShowOnlyLogOf(Type t)
    {
        var filter = new log4net.Filter.LoggerMatchFilter {LoggerToMatch = t.ToString(), AcceptOnMatch = true};
        var filterDeny = new log4net.Filter.DenyAllFilter();
        var x = new log4net.Appender.ConsoleAppender {Layout = new log4net.Layout.SimpleLayout()};
        x.AddFilter(filter);
        x.AddFilter(filterDeny);

        log4net.Config.BasicConfigurator.Configure(x);
        SetupDone = true;
    }

Really UGLY but working (it screws up the highlighting, do not miss the last lines):

        public static void DefaultSetup()
    {
       // AllToConsoleSetup();
        XmlConfigurator.Configure(XmlSetup());
      // DbConfig();

    }


    private static Stream XmlSetup()
    {
        const string x = @" <log4net>
<root>

  <level value=""ALL"" />
  <appender-ref ref=""AdoNetAppender"">

  </appender-ref>
</root>


<appender name=""AdoNetAppender"" type=""log4net.Appender.AdoNetAppender"">
  <bufferSize value=""1"" />
  <connectionType value=""System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" />
  <connectionString value=""data source=Christian-PC\SQLEXPRESS;initial catalog=log4net_2;integrated security=false;persist security info=True;User ID=log4net;Password=XXXX"" />
  <commandText value=""INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)"" />
  <parameter>
    <parameterName value=""@log_date"" />
    <dbType value=""DateTime"" />
    <layout type=""log4net.Layout.RawTimeStampLayout"" />
  </parameter>
  <parameter>
    <parameterName value=""@thread"" />
    <dbType value=""String"" />
    <size value=""655"" />
    <layout type=""log4net.Layout.PatternLayout"">
      <conversionPattern value=""%thread"" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value=""@log_level"" />
    <dbType value=""String"" />
    <size value=""50"" />
    <layout type=""log4net.Layout.PatternLayout"">
      <conversionPattern value=""%level"" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value=""@logger"" />
    <dbType value=""String"" />
    <size value=""655"" />
    <layout type=""log4net.Layout.PatternLayout"">
      <conversionPattern value=""%logger"" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value=""@message"" />
    <dbType value=""String"" />
    <size value=""4000"" />
    <layout type=""log4net.Layout.PatternLayout"">
      <conversionPattern value=""%message"" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value=""@exception"" />
    <dbType value=""String"" />
    <size value=""2000"" />
    <layout type=""log4net.Layout.ExceptionLayout"" />
  </parameter>
  <filter type=""log4net.Filter.LoggerMatchFilter"">
    <param name=""LoggerToMatch"" value=""Ruppert"" />
  </filter>
  <filter type=""log4net.Filter.DenyAllFilter"">
  </filter>
</appender>
</log4net>";
        return new MemoryStream(ASCIIEncoding.Default.GetBytes(x));
    }
like image 159
Christian Ruppert Avatar answered Oct 01 '22 12:10

Christian Ruppert