当前位置:网站首页>C# 使用Log4Net记录日志(基础篇)
C# 使用Log4Net记录日志(基础篇)
2022-07-26 07:22:00 【51CTO】
最近在“伯乐在线”上看到这么篇文章《 用Log4Net来记录系统的日志信息》,由于没有用到过Log4Net这个东东,就有了下面这篇文章:
第一步:下载Log4Net
下载地址: http://logging.apache.org/log4net/download_log4net.cgi 把下载的 log4net-1.2.13-bin-newkey解压后,如下图所示:

双击bin文件夹,如下图:

双击net文件夹,选择针对.NET FramerWork的不同版本

找到相应版本的log4net.dll
第二步:应用Log4Net
1、在项目中添加Log4Net.DLL的引用
开发环境:win7 旗舰版
开发工具:VS2010旗舰版
新建个WinForm应用程序,效果图如下仅为了简单演示
窗体命名为:FormMain
把Log4Net.Dll复制到项目的根目录PubDll文件夹下,然后添加Log4Net的引用,如下图:

引用添加之后,如下图:

小注:WinForm应用程序默认目标框架为.NET Framework4 Client Profile应该修改修改为.NET Framework4

2、配置Log4Net
(1)新建一个应用程序配置文件app.config

(2)app.config的配置文件内容如下:
(3)新建一个LogisTrac日志类,以实现公共调用,具体代码如下:
第三步:测试Log4Net
在按钮的事件中添加如下的代码:
运行项目点击Button按钮,在项目的根目录下就会看到log文件夹,例如本例中:

如果在app.config文件中不指定文件夹名称即去掉,下面这句:
效果如下:

小注:
文件夹的命名是根据你在app.config里面设置的file节点自动生成的,你可以根据自己的需要自己设置存放的路径和格式等。
打开log或者errorLog文件夹就会看到里面以日期命名的文件夹一直打开可以看到如下图:

看到这个文件就说明log4net.dll的应用已经成功了。
边栏推荐
- Yolov6 target detection practice: training your own data set (video tutorial)
- Become an Apache contributor, so easy!
- A guide for you to fully use TS
- hot100 哈希
- It's another summer of open source. 12000 project bonuses are waiting for you!
- Upgrade ecological proposition: what has Alibaba cloud brought to thousands of businesses?
- WCF 部署在IIS上
- Learn browser decoding from XSS payload
- Apache DolphinScheduler&TiDB联合Meetup | 聚焦开源生态发展下的应用开发能力
- Linux c SQLite database usage
猜你喜欢

Relevant configurations of pychart: change font style and size, change picture background, and change the font color of console output

Screen: frame paste, 0 fit, full fit

C51 and MDK coexist keil5 installation tutorial

Singles cup web WP

NFT数字藏品系统开发:数字藏品赋予品牌新活力

Summer Challenge harmonyos - hamster game based on arkui (JS)

【一库】妙啊!这个库组织npm脚本简直爆炸!
Usage of unity3d object pool

NFT digital collection system development: activating digital cultural heritage

从Boosting谈到LamdaMART
随机推荐
NFT数字藏品开发:数字藏品助力企业发展
July training (day 18) - tree
3.0.0 alpha 重磅发布!九大新功能、全新 UI 解锁调度系统新能力
NFT数字藏品系统开发:企业如何开发属于自己的数藏平台
Linux c SQLite database usage
MySQL installation tutorial - hands on installation
依赖和关联的对比和区别
NFT digital collection system development: what are the best digital marketing strategies for NFT digital collection
从XSS Playload 学习浏览器解码
3.0.0 alpha blockbuster release! Nine new functions and new UI unlock new capabilities of dispatching system
In July, glassnode data showed that the open position of eth perpetual futures contract on deribit had just reached a one month high of $237959827.
WCF 入门教程二
pycharm常用快捷键
NFT数字藏品开发:数字艺术藏品赋能公益平台
Compose canvas custom circular progress bar
[200 opencv routines] 231. Gray level co-occurrence matrix (GLCM) for feature description
Taishan office lecture: word error about inconsistent values of page margins
Apache Dolphinscheduler3.0.0-beta-1 版本发布,新增FlinkSQL、Zeppelin任务类型
【Keras入门日志(3)】Keras中的序贯(Sequential)模型与函数式(Functional)模型
[C language] do you really know printf? (printf is typically error prone, and collection is strongly recommended)