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.调度算法 前四个问题可以用如下一句话里来回答:每当集 ...
随机推荐
- [转]安装ambari
一.准备工作: 基本工具 yumrpmscpcurlwgetpdsh前几个一般系统都自带了,pdsh需要自己装 yum install pdsh 2.配置hosts vim /etc/hosts 10 ...
- 并发之AQS原理(三) 如何保证并发
并发之AQS原理(三) 如何保证并发 1. 如何保证并发 AbstractQueuedSynchronizer 维护了一个state(代表了共享资源)和一个FIFO线程等待队列(多线程竞争资源被阻塞时 ...
- html table标签
table标签 table的基本样式 table表格的美化 table表格行合并和列合并 table的基本样式: https://blog.csdn.net/lilongsy/article/deta ...
- MySQL----数据库操作2
数据库高级操作: SHOW DATABASES; 显示数据库 CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci CR ...
- java List.subList方法中的超级大陷阱
ArrayList 中 subList 的基本用法: subList(fromIndex:int,toIndex:int):List<E> 返回从fromIndex到toindex-1 的 ...
- LINQ学习之旅(三)
Linq to Sql语句之Join和Order By Join操作 适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join ...
- Collections.sort 给集合排序
List<MenuVo> child_menus = new ArrayList<MenuVo>(); for (MenuVo menuVo : child_menus) { ...
- django 10.5 sqlite3迁移到mysql
参考: http://www.voidcn.com/article/p-hesvaooz-ru.html 原文: python ./manage.py syncdb --database slave ...
- Codeforces 891C Envy
Envy 感觉这种最小生成树上的啥题都差不多的解法.. #include<bits/stdc++.h> #define LL long long #define fi first #def ...
- 【noip模拟赛4】找啊找啊找BF 拓扑排序
描述 sqybi上次找GF的工作十分不成功,于是依旧单身的他在光棍节前的某天突发奇想,要给自己找一个BF(这里指的是男性的好朋友……),这样既可以和人分享内心的压抑(路人甲:压抑还分享么……),也可以 ...