经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
NLog简单使用
来源:cnblogs  作者:_York  时间:2018/11/14 10:08:05  对本文有异议

一、安装

二、安装后会在根目录出现NLog.config配置文件,简单修改配置文件为写入文件记录日志:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  5. autoReload="true"
  6. throwExceptions="false"
  7. internalLogLevel="Error" internalLogFile="c:\temp\nlog-internal.log">
  8.  
  9. <!-- optional, add some variables
  10. https://github.com/nlog/NLog/wiki/Configuration-file#variables
  11. -->
  12. <variable name="myvar" value="myvalue"/>
  13.  
  14. <!--
  15. See https://github.com/nlog/nlog/wiki/Configuration-file
  16. for information on customizing logging rules and outputs.
  17. -->
  18. <targets>
  19.  
  20. <!--
  21. add your targets here
  22. See https://github.com/nlog/NLog/wiki/Targets for possible targets.
  23. See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
  24. -->
  25.  
  26. <!--Write events to a file with the date in the filename.-->
  27. <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
  28. layout="${longdate} ${uppercase:${level}} ${message}" />
  29. </targets>
  30.  
  31. <rules>
  32. <!-- add your logging rules here -->
  33.  
  34. <!--Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"-->
  35. <logger name="*" minlevel="Debug" writeTo="f" />
  36. </rules>
  37. </nlog>

 

三、使用方法:

  1. private static Logger logger = LogManager.GetCurrentClassLogger();
  2. protected void Application_Error(Object sender, EventArgs e)
  3. {
  4. Exception lastError = Server.GetLastError();
  5. if (lastError != null)
  6. {
  7. logger.Error(lastError);
  8. }
  9. }
  1. logger.Info("Application_Start");

 

简单的异常日志写入完成,看了配置项太多头有点大,先这样了

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号