代码改变世界

Nhibernate Beginner ch6 I am a logger

2011-07-09 11:57  一一九九  阅读(191)  评论(0编辑  收藏  举报

    log4net has three major objects that we need to be concerned with – the log manager, loggers, and appenders.  Imagine that log4net is a big bucket into which we  throw all of the items we want to log on little scraps of paper.  instead of writing all of these pieces of paper by hand, we use an object called a logger. loggers are used to classify and organize information as it is added to the bucket.

    在代码中使用Log4Net 的时候需要使用如下的语句:

    Log4net.Config.BasicConfigurator.Configure();

tips: 在using log4net的时候发现已经add reference了最初提示了一下存在Log4net的namespace, 再重新编译的时候就丢失了,感觉比较奇怪,以为log4net.dll有问题,删除重新添加,问题依旧,google了一下,发现是Project的Target Framework搞得。

     可见:http://www.selfelected.com/the-type-or-namespace-name-log4net-could-not-be-found-are-you-missing-a-using-directive-or-an-assembly-reference/

http://mocella.blogspot.com/2010/01/using-log4net-with-net-40-wpf.html

http://msdn.microsoft.com/en-us/library/cc656912.aspx

     详细:

I then went to compile and got the following error: The type or namespace name 'log4net' could not be found (are you missing a using directive or an assembly reference?)

I double-checked, and yes, I had indeed added a reference to log4net, so what was wrong?  Turns out, for .net 4.0 WPF projects, the default Target Framework is the “.Net Framework 4 Client Profile”, which is a subset of the full framework with the goal of shrinking footprint of the .net framework needed to download for a typical windows application.  However, log4net apparently needs access to system.web.dll, which isn’t included in the client profile. 

To fix your app so it’ll compile/run, open your project’s properties page, and set the Target Framework to “.Net Framework 4” and save your changes. 

from: http://mocella.blogspot.com/2010/01/using-log4net-with-net-40-wpf.html

ConversinPattern

<layout type="log4net.Layout.PatternLayout">
  <conversinPattern value="%d{HH:mm:ss.fff} [%t] %p %c - %m%n"/>
</layout>

<conversionPattern> controls the format of the message.  In this case , it displays the ate(%d{HH:MM:ss.fff}), the ID of the thread the log came from in square brackets([%t]), the priority of the log(%p), the clas the message came from (%c), a dash (-), the content of the log message(%m), and a newline(%n).

猜:这里的大多数信息(比如time, thread, priority)应该都是Log4net记录的,对于任何一个Client来说只需要记录%m即可。

todo: 验证一下是不是这么一回事?Log4net是怎么做到这些的?
done: 的确是。

<root>

     this is the “default” block for log4net, which will set up the basic configuration.
  <root>
        <priority value="INFO"/>
       <!--<appender-ref ref="Console"/>—>
        <appender-ref ref="RoolingLogFileAppender"/>
</root>

while we can set the logging level at the root block, we also can set a <threshold> on each of the appenders.

if i only want Debug through WARN messages(Debug, Info , warn) to go to the console, i could do that with log4net too! all we have to do is add a filter to one of the appenders as follows:

<filter type=”log4net.Filter.LevelRangeFilter”>
         <param name=”LevelMin”   value=”DEBUG”/>
         <param name=”levelMax”  value=”WARN”/>
</filter>

what if we only want information from a particular logger, such as nhibernate messages , to go into the rolling fiel log and everything else to the console? we need to create to a new block in our configuration called a logger. we would divide our root block into two parts, a root and a logger block as follows.

<logger name=”NHibernate” additivity=”false”>
      <level value=”DEBUG”/>
      <appender-ref  ref=”RollingLogFile”/>

</logger>

additivity=”false” tells log4net to “use up ”this message and not to pass it  on to other appenders, so it will only show up in the appenders that are added in this block.