Home

Castle Stronghold

Castle Project Forum Index Castle Project
Support forum
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why NHibernate writes through Log4Net Facility

 
Post new topic   Reply to topic    Castle Project Forum Index -> Facilities
View previous topic :: View next topic  
Author Message
Valera



Joined: 07 Dec 2006
Posts: 1

PostPosted: Thu Dec 07, 2006 4:45 pm    Post subject: Why NHibernate writes through Log4Net Facility Reply with quote

Hello !
We have a project that uses Castle ActiveRecord and Windsor. We attached Log4Net facility and find out that NHibernate begins to write down its logging DEBUG messages using configuration typed in our Log4Net facility configuration. Why it happens? How NHibernate knows about the facility? We did not expect this effect. How to prevent it?
Back to top
View user's profile Send private message
hammett
Castle Team


Joined: 15 Apr 2006
Posts: 1739
Location: SP, Brasil

PostPosted: Thu Dec 07, 2006 5:17 pm    Post subject: Reply with quote

'Cause Log4net is statically configured. The configuration is the same for the entire running app.
_________________
Cheers,
hammett
http://hammett.castleproject.org/
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
sradack



Joined: 08 Aug 2006
Posts: 25
Location: Houston, TX

PostPosted: Fri Dec 08, 2006 11:58 pm    Post subject: Reply with quote

You can change how log4net handle's messages from NHibernate. Try adding a logger to your configuration and changing the log level to something more sane for normal use. For example:

Code:

<logger name="NHibernate">
  <level value="WARN" />
  <appender-ref ref="YourAppender" />
</logger>


The WARN log level will give you much less information. It's usually what I leave my production applications on. Feel free to experiment. Take a look at the Log4Net manual for more detail.
Back to top
View user's profile Send private message Visit poster's website
goodwill



Joined: 27 Oct 2006
Posts: 67

PostPosted: Wed Dec 13, 2006 7:09 pm    Post subject: Reply with quote

Is that possible for me to do a default WARN and then only allow my own package to log debug message? I am using monorail.
_________________
William
Blog about gadgets and programming?
http://bugthis.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
sradack



Joined: 08 Aug 2006
Posts: 25
Location: Houston, TX

PostPosted: Wed Dec 13, 2006 9:31 pm    Post subject: Reply with quote

The default logging configuration is controlled by the root logger. So you might want to try:

Code:


<root>
  <level value="WARN"/>
  <appender-ref ref="MyAppender"/>
</root>


Most projects setup logging hierarchies that match their namespace heirarchies. So a class MyCompany.MyProject.MyClass would setup a logger called the same thing. Then, you could configure the logger in your configuration like so:

Code:

<logger name="MyCompany.MyProject">
  <level value="DEBUG"/>
</logger>


Using both snippets above, you'd have a default level of WARN on all loggers, but a DEBUG level on a specific namespace or class, in this case, MyCompany.MyProject.
Back to top
View user's profile Send private message Visit poster's website
goodwill



Joined: 27 Oct 2006
Posts: 67

PostPosted: Thu Dec 14, 2006 4:49 am    Post subject: Reply with quote

You better try yourself Smile I tried but it seems like it wont work, somehow the controller logger does not report themselve as your package name, instead it seems like it always call itself directly as the controller name, so its not possible to wildcard for that.
_________________
William
Blog about gadgets and programming?
http://bugthis.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
goodwill



Joined: 27 Oct 2006
Posts: 67

PostPosted: Thu Dec 14, 2006 7:14 am    Post subject: Reply with quote

I try to create a log4net config like this to filter out all NHibernate entries but without any luck. For some reason I think this config is error- nothing is logged if I put this on. I am sure I wrote the regex correctly, so anyone can give me some idea?
Code:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
   <file value="Logs\log-file.txt" />
   <appendToFile value="true" />
   <filter type="log4net.Filter.StringMatchFilter">
      <regexToMatch value="^((?!NHibernate).)*$">
   </filter>

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


Even something as simple as this has no luck:
Code:

<?xml version="1.0" encoding="utf-8" ?>
<log4net update="Overwrite">
<appender name="FileAppender" type="log4net.Appender.FileAppender">
   <file value="Logs\log-file.txt" />
   <appendToFile value="true" />
   <filter type="log4net.Filter.StringMatchFilter">
      <regexToMatch value="^.*$">
   </filter>
   <filter type="log4net.Filter.DenyAllFilter" />
   <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
   </layout>
</appender>
   <root>
      <level value="All" />
      <appender-ref ref="FileAppender" />
   </root>
</log4net>

_________________
William
Blog about gadgets and programming?
http://bugthis.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
goodwill



Joined: 27 Oct 2006
Posts: 67

PostPosted: Thu Dec 14, 2006 9:46 am    Post subject: Reply with quote

First of all sorry for my stupid missing close tag error Embarassed

But it doesn't go away after I change the configuration. I have turn on log4net debug log and I am sure the configuration file is workable one this time. I think I might have some confusion on how its being used. Here is my configuration file:

Code:

<?xml version="1.0" encoding="utf-8" ?>
<log4net update="Overwrite" debug="true">
<appender name="FileAppender" type="log4net.Appender.FileAppender">
   <file value="Logs\log-file.txt" />
   <appendToFile value="true" />
   <filter type="log4net.Filter.StringMatchFilter">
      <regexToMatch value="^.*?(Castle.MonoRail|NHibernate).*$" />
      <acceptOnMatch value="false" />
   </filter>
   <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
   </layout>
</appender>
   <root>
      <level value="All" />
      <appender-ref ref="FileAppender" />
   </root>
</log4net>


It suppose to use StringMatchFilter and acceptOnMatch=false, which would filter out the matched expressions. I dumped those log lines and put it into my own regex test it successfully matched. How come all the logs posted into my logfile without getting filtered at all?
_________________
William
Blog about gadgets and programming?
http://bugthis.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
sradack



Joined: 08 Aug 2006
Posts: 25
Location: Houston, TX

PostPosted: Thu Dec 14, 2006 9:18 pm    Post subject: Reply with quote

IMHO, the logging facility should take care to name the logger the same name as the class it's resolving the dependency for. I'm not sure if it does that right now. You could temporarily setup the logging dependency statically:

Code:

...
private ILogger Log = LogManager.CreateLogger(typeof(MyType));
...


This should allow you to configure Log4Net as we were talking about before. Of course, this doesn't utilize the facility to setup the logging depedency, so it's probably not what you were hoping for.

I'll take a look at the logging facility when I get back from out of town on Monday. Hopefully, someone else will chime in and explain how the logging facility names the loggers that it sets up for dependent classes.
Back to top
View user's profile Send private message Visit poster's website
Ron



Joined: 09 Jun 2006
Posts: 25

PostPosted: Thu Dec 14, 2006 9:25 pm    Post subject: Reply with quote

Do you want to forward log messages in NHibernate's log4net repository to Castle's logging facility?

Or do you want to just filter out NHibernate messages so only WARN or higher messages appear?
Back to top
View user's profile Send private message
hammett
Castle Team


Joined: 15 Apr 2006
Posts: 1739
Location: SP, Brasil

PostPosted: Fri Dec 15, 2006 12:01 am    Post subject: Reply with quote

Yup, the logging facility does that, but it does not affect components that are not on the container.
_________________
Cheers,
hammett
http://hammett.castleproject.org/
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
goodwill



Joined: 27 Oct 2006
Posts: 67

PostPosted: Fri Dec 15, 2006 4:02 am    Post subject: Reply with quote

I realize the <logger name=...> tag would not accept a wildcard setup, however, I have achieved this result by adding a filter at the appender:

Code:


<?xml version="1.0" encoding="utf-8" ?>
<log4net update="Overwrite" debug="true">
<appender name="FileAppender" type="log4net.Appender.FileAppender">
   <file value="Logs\applog.txt" />
   <appendToFile value="true" />
   <filter type="log4net.Filter.LoggerMatchFilter">
      <loggerToMatch value="Castle.MonoRail" />
      <acceptOnMatch value="false" />
   </filter>
   <filter type="log4net.Filter.LoggerMatchFilter">
      <loggerToMatch value="NHibernate" />
      <acceptOnMatch value="false" />
   </filter>
   <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
   </layout>
</appender>
   <root>
      <level value="All" />
      <appender-ref ref="FileAppender" />
   </root>
</log4net>



This does the trick correctly.
_________________
William
Blog about gadgets and programming?
http://bugthis.blogspot.com
Back to top
View user's profile Send private message Visit poster's website
pedroteixeira



Joined: 11 May 2007
Posts: 6
Location: Brazil

PostPosted: Fri May 11, 2007 7:27 pm    Post subject: Reply with quote

The best way to accomplish what you want is to use the "additivity" attribute, supported by log4net.

Here an example:
Code:

<root>
    <level value="DEBUG" />
    <appender-ref ref="Console" />
    <appender-ref ref="rollingfile" />
  </root>

  <logger name="NHibernate" additivity="false">
    <level value="INFO" />
    <appender-ref ref="nhibernate-rollingfile" />
  </logger>


This way works for me - NHibernate does not write DEBUG log into the applications logs.
Back to top
View user's profile Send private message
kvieres



Joined: 01 Dec 2008
Posts: 3

PostPosted: Wed Jan 21, 2009 6:21 am    Post subject: Reply with quote

My "log4net.config" file:

Code:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net>
    <appender name="rollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\Inetpub\\wwwroot\\log.txt" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="yyyy.MM.dd" />
      <param name="StaticLogFileName" value="true" />

      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="rollingFile" />
    </root>
   
    <!-- Do not want NHibernate log -->
    <logger name="NHibernate" [b]additivity="false"[/b]>
      <level value="INFO" />
    </logger>

    <!-- ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF -->
    <logger name="Castle.MonoRail">     
      <level value="ERROR" />
    </logger>
   
  </log4net>
</configuration>



I think that the NHibernate log is default on instalation, but using additivity="false" you can turn off.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Castle Project Forum Index -> Facilities All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group