C# NuGet Nlog 日志应用

    软件编写过程中,经常要使用的一些库,如果全部要自己一行一行代码编写的话,不管精力和时间都不允许。NuGet的出现,给我们提供了非常好的解决方案。Nuget不仅有很成熟的库,经过多人不停维护更新的,而且直接嵌入到VS中,使用非常方便。 下面以使用NLog为例,示范NuGet的使用过程。

      1,打开Visual studio,新建一个窗体控制程序,添加一个文本编辑框,用于输入信息,和添加一个按钮控件。

           如图:

    


     2,工具 -》Nuget包管理器 -》管理解决方案的NuGet包


          弹出,搜索框 输入 “NLog”,搜索,然后双击 ,安装,就完成了 NLog日志包的安装。



         安装完成之后,工程目录下,会多一个  packages文件夹;工程目录多一个文件;引用会自定增加NLog的引用。

              

 

        3,调用

        在使用NLog之前,肯定要增加using的,


         然后实例化一个变量,就可以使用了。

         private ILogger _log = LogManager.GetCurrentClassLogger();

      private void btnAnd_Click(object sender, EventArgs e)
        {
            _log.Debug(textBox1.Text);  //写到文件  同时输出到Debug调式器

            _log.Warn("Warning");
            _log.Error("Error");
            _log.Info("Info");
            _log.Fatal("Fatal");

            _log.Trace("写到 debug 调式器");  //只写到Debug调式器,不写到文件

        }


       编译成功,OK,运行,单击。


      然而,什么都没有发生,why????


     原来还少一个步骤,就只需要配置。


   4,配置


    在 App.config 里添加如下内容:


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

  </configSections>

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        autoReload="true">


    <targets>


      <!-- Log in a separate thread, possibly queueing up to messages. When the queue overflows, discard any extra messages-->


      <!-- write logs to file -->
      <target name="file" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
      <target xsi:type="File" fileName="${basedir}/logs/${shortdate}.log"
                layout="${longdate} ${level:uppercase=true} ${event-context:item=Action} ${message}" />
      </target>

      <target name="Vision" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
      <target xsi:type="File" fileName="${basedir}/logs/Vision/${shortdate}.log"
                layout="${longdate} ${level:uppercase=true} ${event-context:item=Action} ${message}" />

      </target>


      <!-- write log message to database -->
      <!--  
      <target name="db" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
        <target type="Database" dbProvider="mssql" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=EFinance;Persist Security Info=True;User ID=sa;Password=123456;">


          <commandText>
            INSERT INTO Log(Timestamp,Level,Message,Action,Amount,StackTrace) VALUES(@time_stamp, @level, @message, @action, @amount, @stacktrace);
          </commandText>


          <parameter name="@time_stamp" layout="${date}" />
          <parameter name="@level" layout="${level:uppercase=true}" />
          <parameter name="@message" layout="${message}" />
          <parameter name="@action" layout="${event-context:item=Action}" />
          <parameter name="@amount" layout="${event-context:item=Amount}" />
          <parameter name="@stacktrace" layout="${stacktrace}" />
        </target>
      </target>
      -->


      <!--write log message to Visual Studio Output-->     
      <target name="debugger" xsi:type="Debugger"
              layout="NLog: ${date:format=HH\:mm\:ss\:fff} | ${level:uppercase=true:padding=-5} | ${message}" />
     </targets>


    <rules>
      <!--TRACE,DEBUG,INFO,WARN,ERROR,FATAL-->
      <logger name="*" minlevel="Trace" writeTo="debugger" />
      <!--INFO,WARN,ERROR,FATAL-->
      <!--<logger name="*" minlevel="Info" writeTo="db" />-->
      <!--DEBUG,INFO,WARN,ERROR,FATAL-->

      <logger name="*" minlevel="Debug" writeTo="file" />

<logger name="AsmVision" minlevel="Debug" writeTo="Vision" enabled="true" final="true" />

    </rules>

  </nlog>


      重新编译,运行,单击。

      


    debug输出窗口,有信息输出。实行文件目录下有Log文件。成功。



    5,高级应用 ----分类应用。

      然后实例化新变量,这种在分线程分模块写log,非常使用。

       private ILogger _VisionLog = LogManager.GetLogger("AsmVision");









    

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值