- 原文地址:http://blog.csdn.net/linraise/article/details/50547149
- 配置文件解析地址:http://blog.csdn.net/pfe_nova/article/details/12225349
注意: ConfigFile 可以指定相对路径 和 绝对路径。 eg: /log/xxxx.log 或者 d://log//xxxx.log
-
1.引入库log4net.dll
-
2.展开项目文件下的Properties文件夹,打开AssemblyInfo.cs并在AssemblyInfo.cs中添加一行:在AssemblyInfo.cs中添加一行:(其中log4net.config对应配置文件名)
1 |
|
- 3.添加log4net.config配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
- 4.添加一个公共的日志管理类AppLog.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
- 5.在任何你想写日志的地方使用,例如:
1 2 |
|
- 6.经验之谈:不要把项目名称起为log4net,或任何数字在中间的名称,否则引入log4net.dll的时候就会直接报错
- 7.当运行正常没有创建日志文件或者log = LogManager.GetLogger(typeof(AppLog))中log对象字段值为false时,右击log4net.config选择属性-->高级把复制到项目的值改为始终复制
- 8 类库项目中好像不能实现此打印日志功能,