Use Multiple log4net Outputs from One Application
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:
<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.
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的更多相关文章
- [转]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 ...
- log4net Tutorial
Introduction One of the greatest logging tools out there for .NET is log4net. This software is the g ...
- Spark Application的调度算法
要想明白spark application调度机制,需要回答一下几个问题: 1.谁来调度? 2.为谁调度? 3.调度什么? 3.何时调度? 4.调度算法 前四个问题可以用如下一句话里来回答:每当集群资 ...
- Oracle Applications Multiple Organizations Access Control for Custom Code
档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...
- 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 ...
- Websphere Application Server 环境配置与应用部署最佳实践
在发布一个运行于 WebSphere Application Server 的 J2EE 应用之前,对服务器进行配置和部署应用是必不可少的一个过程,这个过程是非常复杂的.WAS 为用户提供了可视化的管 ...
- log4net初探
/// <summary> /// Static constructor that initializes logging by reading /// settings from the ...
- 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 ...
- spark application调度机制(spreadOutApps,oneExecutorPerWorker 算法)
1.要想明白spark application调度机制,需要回答一下几个问题: 1.谁来调度? 2.为谁调度? 3.调度什么? 3.何时调度? 4.调度算法 前四个问题可以用如下一句话里来回答:每当集 ...
随机推荐
- mysql 5.6 在线 DDL
原文链接地址:http://seanlook.com/2016/05/24/mysql-online-ddl-concept/ 做MySQL的都知道,数据库操作里面,DDL操作(比如CREATE,DR ...
- C++ code:数值计算之辛普生(Simpson)法求解积分问题
- hdu2289二分答案 圆台体积
精度小一点就能过 #include<bits/stdc++.h> #define maxn 1000000009 #define esp 1e-9 #define PI 3.1415926 ...
- windows 系统常用操作
1.所有端口使用情况 netstat -ano 2.查询xxxx端口pid netstat -aon|findstr "xxxx" 3.根据端口Pid查详情 tasklist|fi ...
- 演示Thread.sleep(100)和Thread.currentThread().isInterrupted()+@Deprecated:将方法标注为废弃的方法
package charpter08; public class TestInterrupt01 { public static void main(String[] args) { Processo ...
- hdu 4463 有一条边必须加上 (2012杭州区域赛K题)
耐克店 和 苹果店必须相连 Sample Input42 30 01 00 -1 1 -10 Sample Output3.41 # include <iostream> # includ ...
- 基于thinkphp的RBAC权限控制
RBAC Role-Based Access Control 权限控制在后台管理中是十分常见的,它的模型大体上是下面这张图的形式 我用的字段和上面不一样,图只是个示例 一个简易的权限控制模型只需要3 ...
- [转]svn检出的时候报 Unable to connect to a repository at URL错误
昨天晚上遇到的问题: 在同一个SVN地址下,很多子目录,各种目录各种权限,现在因为业务,需要下载各种目录下的文件. 第一次遇到这个问题..现象是: 1)在web浏览器下可用: 2)在本地创建的目录下c ...
- asp.net core 内置DI容器的一点小理解
DI容器本质上是一个工厂,负责提供向它请求的类型的实例. .net core内置了一个轻量级的DI容器,方便开发人员面向接口编程和依赖倒置(IOC). 具体体现为Micorosoft.Extensio ...
- BZOJ1293 [SCOI2009]生日礼物 离散化
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1293 题意概括 彩珠有N个,分为K种.每一个彩珠有一个对应的坐标.坐标上可以没有彩珠,多个彩珠也可 ...