Introduction

This is an article simply to demonstrate how to use several output log files depending on the library or the application.

Using the Code

Developers who develop class libraries may need to log details separate from the main application to support users or to quickly retrieve the data. But when they try to implement this, they will face a problem. This is because normally log4net has only one config section under the ASP.NET web.config file or Windows application app.config file. To overcome this problem, you can use different logger entries which define the output.

Default case:

Hide   Copy Code
<log4net>
<appender type="log4net.Appender.RollingFileAppender" name="RollingFile">
<file value="c:\\logs\\Log.txt" />
<layout type="log4net.Layout.PatternLayout" />
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender> <root>
<level value="ALL" />
<appender-ref ref="RollingFile" />
</root>
</log4net>

When you specify the <root /> tag, it says all the log data will log to that output file. To stop this, we need to remove the <root /> element and have separate <logger /> tags for every library or the application. You also need to specify your root namespace for the library.

Hide   Shrink    Copy Code
<log4net>
<appender type="log4net.Appender.RollingFileAppender" name="classApp1">
<file value="c:\\Library1.txt" />
<layout type="log4net.Layout.PatternLayout">
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender> <appender type="log4net.Appender.RollingFileAppender" name="classApp2">
<file value="c:\\Library2.txt" />
<layout type="log4net.Layout.PatternLayout">
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender> <appender type="log4net.Appender.RollingFileAppender" name="application">
<file value="c:\\Application.txt" />
<layout type="log4net.Layout.PatternLayout">
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender> <logger name="ClassLibrary1">
<level value="ERROR" />
<maximumfilesize value="256KB" />
<param value="ERROR" name="Threshold" /> <appender-ref ref="classApp1" />
</logger> <logger name="ClassLibrary2">
<level value="WARN" />
<maximumfilesize value="256KB" />
<param value="WARN" name="Threshold" /> <appender-ref ref="classApp2" />
</logger> <logger name="WindowsApplication1">
<level value="WARN" />
<maximumfilesize value="256KB" />
<param value="WARN" name="Threshold" /> <appender-ref ref="application" />
</logger> </log4net>

You can see three <logger /> items and separate <appender />tags referenced from those loggers. "name" of the <logger /> will specify the root namespace of the library or the application. Specify which appender you need to refer from the logger. When executing the application, if something is written to the log from the ClassLibrary2.Class2, it will refer the <logger name="ClassLibrary2" /> entry. This means the output will be on c:\\Library2.txt file. This way you can specify any amount of loggers dynamically.

Note: The code in Visual Studio 2005 format.

From:  https://www.codeproject.com/Articles/18720/Use-Multiple-log-net-Outputs-from-One-Application

Use Multiple log4net Outputs from One Application的更多相关文章

  1. [转]Multiple outputs from T4 made easy

    本文转自:http://damieng.com/blog/2009/01/22/multiple-outputs-from-t4-made-easy One of the things I wante ...

  2. log4net Tutorial

    Introduction One of the greatest logging tools out there for .NET is log4net. This software is the g ...

  3. Spark Application的调度算法

    要想明白spark application调度机制,需要回答一下几个问题: 1.谁来调度? 2.为谁调度? 3.调度什么? 3.何时调度? 4.调度算法 前四个问题可以用如下一句话里来回答:每当集群资 ...

  4. Oracle Applications Multiple Organizations Access Control for Custom Code

    档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...

  5. Microsoft .NET Pet Shop 4: Migrating an ASP.NET 1.1 Application to 2.0

    249 out of 297 rated this helpful - Rate this topic Gregory Leake Microsoft Corporation Alan Le, Ale ...

  6. Websphere Application Server 环境配置与应用部署最佳实践

    在发布一个运行于 WebSphere Application Server 的 J2EE 应用之前,对服务器进行配置和部署应用是必不可少的一个过程,这个过程是非常复杂的.WAS 为用户提供了可视化的管 ...

  7. log4net初探

    /// <summary> /// Static constructor that initializes logging by reading /// settings from the ...

  8. Understanding IIS Bindings, Websites, Virtual Directories, and lastly Application Pools

    In a recent meeting, some folks on my team needed some guidance on load testing the Web application ...

  9. spark application调度机制(spreadOutApps,oneExecutorPerWorker 算法)

    1.要想明白spark application调度机制,需要回答一下几个问题: 1.谁来调度? 2.为谁调度? 3.调度什么? 3.何时调度? 4.调度算法 前四个问题可以用如下一句话里来回答:每当集 ...

随机推荐

  1. ubuntu 窗口操作快捷键

    alt + space 打开窗口菜单SUPER + A: 搜索或浏览程序 SUPER/alt + tab: 切换窗口 SUPER + L: 进入睡眠状态 SUPER + M:查看日历

  2. 性能测试四十:Mysql存储过程造数据

    性能测试是基于大量数据的,而进行性能测试之前肯定没那么多数据,所以就要自己准备数据 数据构造方法: 1.业务接口 -- 适合数据表关系复杂 -- 优点:数据完整性比较好2.存储过程 -- 适合表数量少 ...

  3. Bootstrap表格中,thead固定,tbody有垂直滚动条

    1.html源码:这里的table是使用的vue写法,实际生成的表格和一个一个写的tr th td无异 <div class="panel-body no-padding"& ...

  4. JavaScript中的短路

    短路:逻辑运算从左到右.逻辑或运算,当左边的条件成立时,后面的条件将不再参与运算. 因此在逻辑或运算中,尽量将条件结果为true的放第一位.而在逻辑与运算中,尽量将条件结果为false的放到第一位. ...

  5. Monitoring and Managing Tomcat

    http://tomcat.apache.org/tomcat-7.0-doc/monitoring.html

  6. Python divmod方法

    有95条数据 每十条存一页 all_item = 95 pager = 10 result = all_item.__divmod__(pager) print(result) (9{商},5{余数} ...

  7. BZOJ1218 [HNOI2003]激光炸弹 二维前缀和

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1218 题意概括 给出一个大的矩阵,求边长为r的正方形区域的最大sum. 题解 二维前缀和然后暴力就 ...

  8. 003 将spark源码导入到IDEA中

    1.解压源代码 2.进入IDEA的首界面 3.使用open将解压的工程加载 4.将文件的形式改成maven项目 5.使用

  9. [OpenCV-Python] OpenCV 中的图像处理 部分 IV (六)

    部分 IVOpenCV 中的图像处理 OpenCV-Python 中文教程(搬运)目录 23 图像变换 23.1 傅里叶变换目标本小节我们将要学习: • 使用 OpenCV 对图像进行傅里叶变换 • ...

  10. 树莓派VNC搭建相关教程+Ubuntu16.04连接vncserver灰屏问题!

    树莓派VNC搭建相关教程: 为了节省money,于是我决定用VNC界面来代替显示器,为后面做C++ Qt以及Python Qt开发打下基础,我别无选择!下面开始进入正题: 1-- 下载VNC-View ...