• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#开源日志Nlog入门

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

   c#语言使用的日志比较多,比如:Log4、NLog等,今天我就简单随笔记录哈NLog的使用。

   1.NLog的安装:

      直接在VS编译器中打开程序包管理器,输入Install-Package NLogin添加进行自己的项目;

      VS程序包管理器请查看 http://blog.csdn.net/doris_d/article/details/46558351

   2.安装NLog配置文件:

      同上在程序包管理器中加入Install-Package NLogin.config;

   3.添加完成之后,目录大致如下:

   

    4.打开配置文件进行配置

    

 1 <targets async="true">
 2     <!--
 3     add your targets here
 4     See https://github.com/nlog/NLog/wiki/Targets for possible targets.
 5     See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
 6     -->
 7     <target xsi:type="File" name="file_info"
 8                fileName="${basedir}/Logs/Info/info_${shortdate}.txt"
 9                layout="${message}"
10                archiveAboveSize="10240"
11                archiveNumbering="Sequence"
12                concurrentWrites="true"
13                keepFileOpen="false"
14                encoding="iso-8859-2"
15             />
16     <target xsi:type="File" name="file_debug"
17               fileName="${basedir}/Logs/Debug/debug_${shortdate}.txt"
18               layout="${message}"
19               archiveAboveSize="10240"
20               archiveNumbering="Sequence"
21               concurrentWrites="true"
22               keepFileOpen="false"
23               encoding="iso-8859-2"
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     -->
30   </targets>
31  <rules>
32    <logger name="*" level="Info" writeTo="file_info"></logger>
33    <logger name="*" level="Debug" writeTo="file_debug"></logger>
34 </rules>
View Code

    注意记录日志有2种方式,同步和异步,只需要再配置targets时候,添加async="true"就为异步,默认为同步。

    target备注:type  类型,日志类型有(保存文件、数据库);name  名字和下面role规则对应;fileName 文件保存路径;archiveAboveSize:大小之后会自动添加文件进行保存。

    logger备注:level 日志级别(Info、Debug等) writeTo 同上述名字进行匹配。

   5.进行简单的测试

    

 1 private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
 2         /// <summary>
 3         /// 写日志
 4         /// </summary>
 5         /// <param name="action"></param>
 6         public static void WriteDeBug(string address, string msg, string path)
 7         {
 8             ExpectionMsg Entity = new ExpectionMsg()
 9             {
10                 Address = address,
11                 Msg = msg,
12                 Path = path
13             };
14             logger.Debug(Entity.ToString());
15         }
View Code

   6.在config对应的文件中查找文件就好了。

  

 

 

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#压缩解压7z发布时间:2022-07-18
下一篇:
C#多线程同步发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap