views:

2938

answers:

3

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 onyl 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>

A: 

Initially your setup looks perfectly fine. Have you tried logging without NHibernate going, just two of your own loggers and see if one is accepted and one is denied? That way you could at least rule out NHibernate.

Peter Lillevold
+3  A: 

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));
    }
Christian
Christian - could you point to any resources for configuring log4net using only code (that a log4net newbie could use)?
Sean Kearon
Sorry, don't have a real link here. Try to search for parts of the snippet, like Layout.SimpleLayout... I will add some more code in the post above..
Christian
Thanks Christian, that's really useful to get me started. Much appreciated.
Sean Kearon
A: 

what is pure code? some applications of pure code?