加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net Web项目中使用Log4Net进行错误日志记录

发布时间:2020-12-15 21:26:58 所属栏目:asp.Net 来源:网络整理
导读:div class="recommend-right" ? 使用 ? ?那么我们如何在Web项目中使用Log4Net呢? 一、基本配置 ? p align="left"1、下载Log4Net,地址如下:a href="http://logging.apache.org/log4net/download_log4net.cgi" rel="nofollow" target="_blank"http://loggin

<div class="recommend-right">

?

使用

? ?那么我们如何在Web项目中使用Log4Net呢?

一、基本配置

?

<p align="left">1、下载Log4Net,地址如下:<a href="http://logging.apache.org/log4net/download_log4net.cgi" rel="nofollow" target="_blank">http://logging.apache.org/log4net/download_log4net.cgi,如下图所示:


<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/63093a9cfd39c0226cff7d4372104c29.jpg" alt="" width="800" height="539">


<p align="left">? ? ??


<p align="left">2、下载到本地后,进行解压,,并找到对应版本的.NET Framework版本,如下图所示:


<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/fbd5130eafea30681dd21673c2464965.jpg" alt="">


<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/7d3cb39597081dfe15a8230edff7c5ab.jpg" alt="">


<p align="left">?


<p align="left">3、在项目中添加<span lang="EN-US">Log4Net.DLL的引用,如下图所示:


<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/09072248905a948c0b62d4c04dc5773b.jpg" alt="">


<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/39c1d85afd0b5561079bb65680c069c7.jpg" alt="" width="800" height="457">


<p align="left">?


<p align="left">4、在Web项目根目录下创建log4net.config配置文件,如下图所示:


<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/b530c844cb268506299837fc3e3dd3c1.jpg" alt="" width="800" height="438">


<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/34ada5064644ea10f5b637a14272d8b9.jpg" alt="" width="800" height="483">


<p align="left">配置文件log4new.config?XML代码如下:


<p align="left">?


<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools">

?
?
?
    ??
  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. ??

5、项目中添加一个LogHelper.cs操作类,这样可以之后的其他页面进行调用,如下图所示:

<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/2d78a08865de1978fa3164c41278a9a4.jpg" alt="" width="850" height="486">


<p align="left">?


<p align="left">LogHelper.cs类代码如下:


<p align="left">?


<div class="dp-highlighter bg_csharp">
<div class="bar">
<div class="tools">

?
?
?
  1. [assembly:?log4net.Config.XmlConfigurator(Watch?=?
  2. {??
  3. ????{??
  4. ??
  5. ????????
  6. ??
  7. ??????????
  8. ??
  9. ????????
  10. ????????????log4net.ILog?log?=?log4net.LogManager.GetLogger(t);??
  11. ????????}??
  12. ??????????
  13. ??????????
  14. ??
  15. ??????????
  16. ??
  17. ????????{??
  18. ????????????log.Error(msg);??
  19. ??
  20. }??

<p align="left">?

?6、在项目中添加一个全局应用程序类Global.asax,如下图所示:
<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/87a5726ca7f8500194238f7e94c0d4c6.jpg" alt="" width="850" height="501">


<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/5cd05f10a87551cb06c024a439b1cf84.jpg" alt="" width="850" height="478">


<p align="left">?


<p align="left">?方法中添加如下代码:


<p align="left">?


<div class="dp-highlighter bg_csharp">
<div class="bar">
<div class="tools">

?
?
?
  1. {??
  2. ?????XmlConfigurator.Configure();??

<p align="left">?


<p align="left">?

?7、打开Web.config文件,添加如下配置信息,如下图所示:
<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/2e30fed2f6e77dfc06977f4ddeb33d67.jpg" alt="" width="850" height="455">


<p align="left">?


<p align="left">? 配置文件代码如下:


<p align="left">?


<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools">

?
?
?
    ??
  1. ??
  2. ??有关如何配置?ASP.NET?应用程序的详细信息,请访问??
  3. ??--??
  4. ??
  5. ????
  6. ??
  7. ????
  8. ??
  9. ????
  10. ??
  11. ????????
  12. ??
  13. ??????
  14. ??
  15. ????
  16. ??

<p align="left">?8、在页面中调用,进行日志写入,如下图所示:


<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/4b385dd04e14581b4e780fe515b9af54.jpg" alt="" width="850" height="480">


<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/08c49a713804902fa26c36d5af8d8082.jpg" alt="">


<p align="left">页面调用代码:


<p align="left">?


<div class="dp-highlighter bg_csharp">
<div class="bar">
<div class="tools">

?
?
?
  1. {??
  2. ????{??
  3. ????}??

<p align="left">?

9、查看项目根目录下的Log文件夹,可以看见我们写入的日志log文件,如下图所示:

? ?至此在Web项目中使用Log4Net进行日志记录的功能就已经实现了。

<p align="left">---------------------------------------------------------------------------------------------------------------------


<p align="left">?


<p align="left">二、扩展


<p align="left">?


<p align="left">? ? 上面实现的是将日志写入到文件中,那么如果想写入到<a class="replace_word" title="MySQL知识库" href="http://lib.csdn.net/base/mysql" rel="nofollow" target="_blank">数据库中,该如何操作,这里以SQL Server为例,创建一个日志表,然后修改log4net.config文件,如下代码:


<p align="left">?


<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools">

?
?
?
    ??
  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. ??

<p align="left">?

注意<span lang="EN-US"><span lang="EN-US"><bufferSize value="<span lang="EN-US">10<span lang="EN-US">" /> ,需要注意的一个参数?bufferSize=10?表示是记录10 条到缓冲区,满10条后再写入SQL server;
<p align="left"><span lang="EN-US"><a class="replace_word" title="软件测试知识库" href="http://lib.csdn.net/base/softwaretest" rel="nofollow" target="_blank">测试时将其修改为1


<p align="left"><span lang="EN-US">生产环境建议100


<p align="left"><span lang="EN-US">?


<p align="left">上面配置节<a href="https://www.baidu.com/s?wd=%E4%B8%AD%E6%95%B0%E6%8D%AE&amp;tn=24004469_oem_dg&amp;rsv_dl=gh_pl_sl_csd" target="_blank">中数据库名,用户名和密码,根据实际情况进行修改。


<p align="left">?


<p align="left">数据库表ErrorLog创建语句为:


<p align="left">?


<div class="dp-highlighter bg_sql">
<div class="bar">
<div class="tools">

?
?
  1. ????nId?
  2. ????sThread?nvarchar(100)?
  3. ????sLogger?nvarchar(500)?
  4. ????sException?nvarchar(4000)?

<span lang="EN-US"><span lang="EN-US"><span lang="EN-US"><span lang="EN-US">如下图所示:

<p align="left">

<img src="https://www.52php.cn/res/2019/02-03/17/0c5d68200a7ee4800e65695139f0c000.jpg" alt="" width="850" height="132">


<p align="left">?


<p align="left">?


<p align="left">三、整合


<p align="left">想同时保存到日志文件和数据库,log4net.config的文件配置如下XML代码所示:


<p align="left">?


<div class="dp-highlighter bg_html">
<div class="bar">
<div class="tools">

?
?
    ??
  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. ????
  90. ????
  91. ??
  92. ??????
  93. ??
  94. ??????
  95. ??
  96. ??????
  97. ??
  98. ????
  99. ????
  100. ??
  101. ??????
  102. ??
  103. ????
  104. ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读