zoukankan      html  css  js  c++  java
  • nlog学习使用

    最近有不少朋友推荐我用NLog。我以前都是自己写txt的文本输出log,以前别人用log4net的时候看那个配置文件,看得我一阵烦,我比较喜欢约定胜于配置的组件。这次玩了一波NLog,,相当不错。一下就写个使用方法。

    1.使用命令行下载NLog

    Install-Package NLog -Pre
    

    https://www.nuget.org/packages/NLog/

    源码:https://github.com/NLog/NLog

    文档:https://github.com/NLog/NLog/wiki

    2.在web.config或者app.config的地方加入配置(不是很多)

      <configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
      </configSections>
      <nlog configSource="NLog.config"></nlog>  
    

      这里有个NLog.config,就是具体的配置

    3.NLog.config配置

    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <targets>
        <target name="file" xsi:type="File" fileName="${basedir}/log_${shortdate}/${longdate}.log" encoding="utf-8" layout="${longdate} [${level}]: ${message}"/>
        <target name="coloredConsole" xsi:type="ColoredConsole" encoding="utf-8" layout="${longdate} [${level}]:${message}"/>
        <!--<target name="debugger" xsi:type="Debugger" encoding="utf-8" layout="${longdate} [${level}]:${message}"/>-->
        <target name="network" xsi:type="Network" address="tcp4://127.0.0.1:10100" encoding="utf-8" keepConnection="true" onOverflow="Split" newLine="false" maxMessageSize="4096">
          <layout xsi:type="SimpleLayout">
            <text>${longdate} [${level:uppercase=true}] (${logger}): ${message}</text>
          </layout>
        </target>
      </targets>
      <rules>
        <logger name="*"  writeTo="coloredConsole"/>
        <logger name="*"  writeTo="file"/>
        <logger name="*" minlevel="Trace" writeTo="network" />
      <!--<logger name="*"  writeTo="debugger"/>-->
      </rules>
    </nlog>
    <!--文档:https://github.com/NLog/NLog/wiki -->
    

    配置中我设置了File,ColoredConsole,debugger,Network,一般本地调试前三者足够使用,当集群部署系统需要集中log显示(或者也可以用来做监控,嘿嘿)使用Network传递信息。  

    不过我在使用4.4.5的版本,使用的时候遇到一个问题,当我type是Network的时候,type为debugger要注释,不然Nlog会失效

    4.Demo测试如下

    声明

    private static Logger logger = LogManager.GetCurrentClassLogger();
    

    使用

    logger.Debug("1111");
    

      

    DEMO下载地址

  • 相关阅读:
    正则表达式
    kafka Auto offset commit faild reblance
    安装包问题
    身份证头像截取
    web表单
    模板与继承与控制语句
    虚拟环境安装及Hello World
    flask入门脚本解释
    python3 邮件发送
    ASP.NET MVC文件上传简单示例
  • 原文地址:https://www.cnblogs.com/RainbowInTheSky/p/6682076.html
Copyright © 2011-2022 走看看