`

Log4net Easy as 1-2-3

阅读更多
几乎所有的项目都需要以 log 的形式保留一些系统纪录,实现这类功能的API又以Log4J最为出名。对于.NET平台上,值得庆幸的是,Apache组织已经把Log4J迁移到.NET平台上,从而诞生了:og4j的姐妹-Log4net.下面的文字仅仅作为快速入门的参考,更详细的应用可以访问apache的站点。

1. 首先你应该下载log4net.dll并引入到你的项目References中.

2. 需要修改你的global.asa.cs. 配置application对象启动的时候加载log4net配置. 这一步是不可以缺少的.

 protected void Application_Start(Object sender, EventArgs e)
{
   log4net.Config.XmlConfigurator.Configure();
}


对于Application程式,则在main入口点加载配置。

3. Log4net 的配置应设置于web.config或App.config中,以Web.Config为例在根节点 configuration 中加入如下section:
<configSections>
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>


4. 该config section 声明了名为 log4net的一个Section,其必须位于web.config根节点 configuration 下。
以下是一个sample:

 <log4net debug="false">
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
        <param name="File" value="XxxxApplication.log.txt" />
        <param name="datePattern" value="MM-dd HH:mm" />
        <param name="AppendToFile" value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
        </layout>
    </appender>
    <appender name="HttpTraceAppender" type="log4net.Appender.ASPNetTraceAppender" >
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
        </layout>
    </appender>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
        </layout>
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="_LogData\Log.txt" />
        <param name="AppendToFile" value="true" />
        <param name="MaxSizeRollBackups" value="10" />
        <param name="MaximumFileSize" value="5MB" />
        <param name="RollingStyle" value="Size" />
        <param name="StaticLogFileName" value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
        </layout>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="LogFileAppender" />
    </root>
</log4net>


5. 上面的配置定义了多个appender. 简单来说, 每一个 appender 都是一个输出管道。

6. root节点指定了选用的 appender. 本例选用了LogFileAppender. (文本文件输出).

在Appender定义中定义了输出的格式. 和目标文本文件所在位置. (起始位置是应用程序根目录. (web.config所在目录).

7. 到目前位置就配置好了log4net. 可以在我们的应用中直接使用了.

8. 以下说明应用方法:
要输出日志, 必须首先得到带有一个别名的logger. 使用以下命令:
 log4net.ILog Logger logger = log4net.LogManager.GetLogger(typeof(XXXX));


之后调用

logger.Info(string message);
logger.Error(string message);
logger.Debug(string message);
...




即可输出日志.
分享到:
评论

相关推荐

    ramin 2.2 for xp

    '1' (number one) key repeatedly during booting and the Radmin driver will not load. ---------------------------------------------------------------------- Limitations ------------------------------...

    rfc全部文档离线下载rfc1-rfc8505

    immediate and easy use by a wide class of users. With this goal, it seems natural to provide the ability to use any remote HOST as if it had been dialed up from a TTY (teletype) terminal. ...

    SEG-Y Binary Conventor

    2) output the progress into a logfile 3) fast converting to text files (7 seconds for 18M, 3hours10minutes for 24.5G) 4) output the converted text file into split files. 100 records per file. Easy to ...

    EurekaLog_7.5.0.0_Enterprise

    EurekaLog 7.3 Hotfix 1 (7.3.1.0), 2-October-2015 1)....Fixed: Long startup time on terminal services servers EurekaLog 7.3 (7.3.0.0), 24-September-2015 1)....Added: RAD Studio 10 Seattle support 2).....

    Big Data Made Easy - A Working Guide To The Complete Hadoop Toolset

    ### Big Data Made Easy – A Comprehensive Guide to the Hadoop Ecosystem #### Introduction to Big Data and Hadoop In today's digital era, the volume and variety of data generated by businesses and ...

    Java - J2EE Job Interview Companion.pdf

    3. **Utilize Configuration Files:** Configure Log4j using external configuration files rather than hardcoding settings within the application. This approach provides greater flexibility and makes it ...

    Diskeeper 2008 v12.0.759.0

    2. Some of the Diskeeper installation dialogs do not display correctly on computers using the High Contrast Black display setting under the Windows Accessibility Options. Note, however, the ...

    django-master-class

    #### 1. Unit Testing (Simon Willison) **Introduction:** Unit testing is a crucial aspect of software development that ensures the reliability of individual components or functions within an ...

    ErrorLog allows web developers quick and easy access to clearly formatted entries from the apache error_log file

    Page.Request.SaveAs("D:\\SCMwwwroot\\HCBN\\errorLog\\Requests\\" + str_time + ".txt", true); // 关闭文件流 sw1.Close(); sw2.Close(); // 调用基类的OnError方法 base.OnError(e); } ``` 在这段代码...

    微软内部资料-SQL性能优化2

    Typically, the process address space is evenly divided into two 2-GB regions. Each process has access to 2 GB of the 4 GB address space. The upper 2 GB of address space is reserved for the system. ...

    ICS delphixe10源码版

    repository that is zipped each night for easy download. The download page above also includes the OpenSSL binaries needed to support SSL. ICS V8 supports Delphi 64-bit and Mac OS-X projects. Note that...

    i-vector的工具箱

    This makes it easy to use the tools on machines with limited memory (but enough disk space) as well as computer clusters (from a terminal). In addition, the interactive tools can optionally write the...

    jclazz-1.2.2

    1. About jclazz is a crossplatform powerful Java bytecode viewer and decompiler. It supports latest Java versions (from 1.4 till 1.6). It can be used both as command line tool and user application ...

    intellitamper

    v2.0 Beta 2 (November-1999) : - Main window can now be resized to full screen. - Many bugs killed. Where are the next ?... v2.0 Beta 1 (September-1999) : - Full program conversion into C ...

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    in Chapter 1 but aren’t used in Chapters 2, 3, and 4. It also introduces another new technique: keeping track of the previous row you processed and the row you’re currently processing, in order to ...

    simple-tags

    The Simple Tags can be installed in 3 easy steps: 1. Unzip "Simple tags" archive and put all files into a folder like "/wp-content/plugins/simple-tags/" 2. Activate the plugin through the 'Plugins' ...

    ADB and Fastboot Tools v1.4.3

    What is this?...If you have previously installed it as system-wide and now you want it current user only (or vice-versa), it won't remove it, you must do it yourself. (at least for now)

    微软内部资料-SQL性能优化5

    Just as the organization of a telephone directory makes it easy for a person to search, SQL Server quickly searches a table with a clustered index. Because a clustered index determines the sequence ...

    BobBuilder_app

    Pages in a b+tree are usually implemented as a list or array of child pointers and so while finding and inserting a value is a O(log k) operation the process actually has to move children around in ...

    Zend Framework 2 Application Development

    Chapter 3: Scratching the Surface of Zend Framework 2 Chapter 4: The First Request/Response – Building the User Wall Chapter 5: Handling Text Content – Posting Text Chapter 6: Working with Images –...

Global site tag (gtag.js) - Google Analytics