经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C# 使用NLog记录日志入门操作
来源:cnblogs  作者:碧水青荷  时间:2018/9/25 20:55:38  对本文有异议

 环境:win7 64位, VS2010

1、首先用VS2010创建命令行工程NLogDemo

2、在程序包管理器控制台中输入:Install-Package NLog -Version 4.4.12

  这句是怎么来的,要是你用过nuget包管理工具,那就可以跳过这里的说明了。

  要使用nuget添加NLog包到项目中。请看下图。

 

然后在程序包管理工具控制台下输入:Install-Package NLog -Version 4.4.12

再看看Install-Package NLog -Version 4.4.12这个怎么找。

打开百度搜索:Nuget

然后在Nuget官网上搜索栏输入:Nlog 回车

选择第一项Nlog

 

然后在 Version History 下选择 4.4.12 这个版本

 

至于为什么选择这个版本,因为这个版本下载的次数多。嘿嘿,没办法,随大流。当然还要看这个版本包的依赖项

这个包的没有什么特殊依赖项,所以可以使用。然后拷贝

 

 

 这里这句话就是要我们要找的。是不是挺简单的。

当我们在包程序包管理器控制台中输入:Install-Package NLog -Version 4.4.12 然后按下回车键,VS IDE 会自动到 nuget.org 这里下载依赖包。

NLog 包添加完之后,还要添加 NLog.config(4.4.12)(Install-Package NLog.Config -Version 4.4.12) 这个包,按道理 NLog 和 NLog.config 应该一起的,

突然从某个版本开始分开了,要单独添加。具体情况可以去官网看介绍:https://nlog-project.org/

 

添加 NLog.config 的方法跟上面添加 NLog 的方法一样。

3、简单封装 Log

  添加类Log.cs到工程中,修改代码如下:

  1. public sealed class Log
  2. {
  3. private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
  4. private Log() { }
  5. public static void Trace(string strMsg)
  6. {
  7. _logger.Trace(strMsg);
  8. }
  9. public static void Debug(string strMsg)
  10. {
  11. _logger.Debug(strMsg);
  12. }
  13. public static void Info(string strMsg)
  14. {
  15. _logger.Info(strMsg);
  16. }
  17. public static void Warn(string strMsg)
  18. {
  19. _logger.Warn(strMsg);
  20. }
  21. public static void Error(string strMsg)
  22. {
  23. _logger.Error(strMsg);
  24. }
  25. public static void Fatal(string strMsg)
  26. {
  27. _logger.Fatal(strMsg);
  28. }
  29. }

4、修改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="Off" internalLogFile="c:\temp\nlog-internal.log">
  8.  
  9. <!-- optional, add some variables
  10. https://github.com/nlog/NLog/wiki/Configuration-file#variables
  11. -->
  12. <!--
  13. <variable name="myvar" value="myvalue"/>
  14. -->
  15. <variable name="fileFormat"
  16. value="
  17. ${newline}date:${date}
  18. ${newline}level:${level}
  19. ${newline}logger:${logger}
  20. ${newline}machinename:${machinename}
  21. ${newline}message:${message}
  22. ${newline}appdomain:${appdomain}
  23. ${newline}assembly-version:${assembly-version}
  24. ${newline}basedir:${basedir}
  25. ${newline}callsite:${callsite}
  26. ${newline}counter:${counter}
  27. ${newline}nlogdir:${nlogdir}
  28. ${newline}processid:${processid}
  29. ${newline}processname:${processname}
  30. ${newline}specialfolder:${specialfolder}
  31. ${newline}stacktrace: ${stacktrace}
  32. ${newline}------------------------------------------------------------" />
  33.  
  34. <!--
  35. See https://github.com/nlog/nlog/wiki/Configuration-file
  36. for information on customizing logging rules and outputs.
  37. -->
  38. <targets>
  39.  
  40. <!--
  41. add your targets here
  42. See https://github.com/nlog/NLog/wiki/Targets for possible targets.
  43. See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
  44. -->
  45.  
  46. <!--
  47. Write events to a file with the date in the filename.
  48. <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
  49. layout="${longdate} ${uppercase:${level}} ${message}" />
  50. -->
  51.  
  52. <target name="file" xsi:type="File"
  53. fileName="${basedir}/Logs/${date:format=yyyy-MM}/${shortdate}.log"
  54. layout="${fileFormat}"
  55. maxArchiveFiles="5"
  56. archiveAboveSize="10240"
  57. archiveEvery="Day"/>
  58. </targets>
  59.  
  60. <rules>
  61. <!-- add your logging rules here -->
  62.  
  63. <!--
  64. Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
  65. <logger name="*" minlevel="Debug" writeTo="f" />
  66. -->
  67.  
  68. <!--
  69. Level Example
  70. Fatal Highest level: important stuff down
  71. Error For example application crashes / exceptions.
  72. Warn Incorrect behavior but the application can continue
  73. Info Normal behavior like mail sent, user updated profile etc.
  74. Debug Executed queries, user authenticated, session expired
  75. Trace Begin method X, end method X etc
  76. -->
  77. <!--
  78. Logging 水平分为以下等级“Trace<<Debug<<Info<<Warn<<Error<<Fatal ”,如果我们选择Info值,则Trace和Debug等级的信息不会被输出。
  79. -->
  80. <logger name="*" minlevel="Trace" writeTo="file"/>
  81. </rules>
  82. </nlog>

简单解释:

  1. variable log文件的内容输出格式
  1. targets 目标文件(要生成的Log文件)的配置(文件名、格式变量、文件个数、文件大小等等)
  1. rules 规则,也就是俗话说的Log输出级别

    以上内容不进行过多解释了,再多解释也不如官网的说明。详细介绍请看官网:https://github.com/nlog/NLog/wiki/Configuration-file#configuration-file-format

5、使用Log输出日志到文件,简单示例代码如下

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. RunTest();
  6. Console.WriteLine("Press a key end ...");
  7. Console.ReadKey(true);
  8. }
  9. static void RunTest()
  10. {
  11. for (int i = 0; i < 1000; i++)
  12. {
  13. Log.Info(string.Format("{0}", i + 1));
  14. System.Threading.Thread.Sleep(10);
  15. }
  16. }
  17. }

输出路径结构

输出文件内容:

以上内容输出格式可以在NLog.config中根据需求进行裁剪。

demo下载

 

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

本站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号