Using nunit in website and class project.

1.Using nuit in website.

<1> Using add reference function to add nunit dll.

<2> Add the below section to web.config

<configuration>
 <configSections>
  <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
   <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
    <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
    <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
     <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
     <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
     <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
    </sectionGroup>
   </sectionGroup>
  </sectionGroup>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
 </configSections>
 <log4net>
  <!--日志记录器logger,可以有多个-->
  <logger name="AppLogger">
   <level value="INFO"/>
   <appender-ref ref="LogFileAppender"/>
   <appender-ref ref="ConsoleAppender"/>
  </logger>
  <logger name="Form1">
   <level value="DEBUG"/>
   <appender-ref ref="LogFileAppender"/>
  </logger>
  <!--所有logger的基,root的设置在所有logger中都起作用。
        当在root和logger中重复设定相同的appender时,你会发现同一日志信息将被记录两次。-->
  <!--<root>
            <level value="WARN" />
            <appender-ref ref="LogFileAppender" />
            <appender-ref ref="ConsoleAppender" />
        </root>-->
  <!--一个appender可以由多个logger共用,当然一个logger可以指定多个appender。-->
  <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
   <param name="File" value="App.log"/>
   <param name="AppendToFile" value="true"/>
      <param name="datePattern" value=".yyyyMMdd"/>
      <param name="maximumFileSize" value="1MB"/>
   <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] %X{auth} - %m%n"/>
   </layout>
   <filter type="log4net.Filter.LevelRangeFilter">
    <param name="LevelMin" value="DEBUG"/>
    <param name="LevelMax" value="FATAL"/>
   </filter>
  </appender>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
   <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] %X{auth} - %m%n"/>
   </layout>
  </appender>
 </log4net>

<3>In Global.asax file add the code:

 

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        log4net.Config.DOMConfigurator.Configure();
    }
   
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

    }
      
</script>

<4> Add a file App.log in project root folder.

<5>Then you can use log4j.net to log message
  log4net.ILog log = log4net.LogManager.GetLogger("AppLogger");
        log.Debug("aaa");
        log.Info("bbb");

 

2.Using log4j.net in class project

<1>In class project add log4j.net dll to project

<2>Add  the configuration info in app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler,log4net" />
  </configSections>
  <log4net>
    <!--日志记录器logger,可以有多个-->
    <logger name="AppLogger">
      <level value="DEBUG"/>
      <appender-ref ref="LogFileAppender"/>
      <appender-ref ref="ConsoleAppender"/>
    </logger>
    <logger name="Form1">
      <level value="DEBUG"/>
      <appender-ref ref="LogFileAppender"/>
    </logger>
    <!--所有logger的基,root的设置在所有logger中都起作用。
        当在root和logger中重复设定相同的appender时,你会发现同一日志信息将被记录两次。-->
    <!--<root>
            <level value="WARN" />
            <appender-ref ref="LogFileAppender" />
            <appender-ref ref="ConsoleAppender" />
        </root>-->
    <!--一个appender可以由多个logger共用,当然一个logger可以指定多个appender。-->
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="App.log"/>
      <param name="AppendToFile" value="true"/>
      <param name="datePattern" value=".yyyyMMdd"/>
      <param name="maximumFileSize" value="1MB"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] %X{auth} - %m%n"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG"/>
        <param name="LevelMax" value="FATAL"/>
      </filter>
    </appender>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] %X{auth} - %m%n"/>
      </layout>
    </appender>
  </log4net>
</configuration>

<3>Add method to read the app.config file

   public void SetLoggerConfig()
        {
            String filePath = @"C:\My Documents\Visual Studio 2005\Projects\ADOProject\BLL\App.config";
            FileInfo file = new FileInfo(filePath);
            log4net.Config.DOMConfigurator.Configure(file);
        }

<4> Get logger object

private ILog logger = log4net.LogManager.GetLogger("AppLogger"); // AppLogger is defined in app.config file

<5> You can log the info now . And the App.log file will auto create to the debug folder of the class project.

posted @ 2009-12-28 16:30  C.fly  阅读(342)  评论(0编辑  收藏  举报