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.
<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.调度算法 前四个问题可以用如下一句话里来回答:每当集 ...
随机推荐
- java 延时的几种方法方法
Java 延时常见的几种方法 1. 用Thread就不会iu无法终止 new Thread(new Runnable() { public void run() { while (true) { ...
- javafx点击鼠标出现弹窗,demo
在学习javafx的过程中,不知道怎么出现一个弹窗,如,点击一个按钮出现一个修改信息的列表选项 终于在javafx文档示例中发现了类似的东西,记录一下,备忘package demo9_button; ...
- SSD(single shot multibox detector)算法及Caffe代码详解[转]
转自:AI之路 这篇博客主要介绍SSD算法,该算法是最近一年比较优秀的object detection算法,主要特点在于采用了特征融合. 论文:SSD single shot multibox det ...
- Python 手动新建 Scrapy项目
# 创建项目 scrapy startproject 工程名 # 创建爬虫 scrapy genspider example example.com
- Centos7与Windows10添加Windows10启动项并设置为默认启动
在Centos7下root登陆 编辑 /boot/grub2/grub.cfg vim /boot/grub2/grub.cfg 在第一行添加 menuentry "Windows10&qu ...
- anaconda3下配置python-3.5+tensorflow-gpu-1.9.0人脸识别项目环境
https://www.cnblogs.com/31415926535x/p/10620732.html 之前为了配置tensorflow-gpu的环境又是装cuda,又是装cudnn,还有tenso ...
- dns安全可视化 设计
这么设计 dns 常见的 安全分类.显示出来. dns 的安全数据,显示出来. 各种相关数据. 展示方式, 图标,饼图,柱状图等等,多屏拼接. 前端的可视化展示 方式 由 文慧 提供 或者实现. 相关 ...
- 安卓工作室 文件浏览器 android studio File browser
安卓工作室 文件浏览器 android studio File browser 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: ...
- HDU.3516.Tree Construction(DP 四边形不等式)
题目链接 贴个教程: 四边形不等式学习笔记 \(Description\) 给出平面上的\(n\)个点,满足\(X_i\)严格单增,\(Y_i\)严格单减.以\(x\)轴和\(y\)轴正方向作边,使这 ...
- Java并发程序设计(十三)锁的性能优化
锁的性能优化 一.优化注意事件 一)减少锁的持有时间 只在必要时进行同步,能明显减少锁的持有时间. 二)锁的细化 缺陷:当系统需要全局锁时,其消耗的资源会比较多. 三)锁的分离 比如读写分离锁 四)锁 ...