zoukankan      html  css  js  c++  java
  • C#开源日志Nlog入门

       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对应的文件中查找文件就好了。

      

      

  • 相关阅读:
    Urlrewrite 配置信息写在另外的文件
    maven项目动态替换配置中的值
    搭建一个java博客
    那个不嫌弃你穷的姑娘,如果有一天真的离开了你,那只是因为,你把她弄哭了。
    常规工作流模型
    浅谈https(创建、传输、断开)
    日志相关杂
    主键生成
    自动化部署脚本(windows上传到linux)
    简述IO
  • 原文地址:https://www.cnblogs.com/kq123321/p/6070564.html
Copyright © 2011-2022 走看看