当前位置: 移动技术网 > IT编程>开发语言>.net > NLog简单使用

NLog简单使用

2018年11月14日  | 移动技术网IT编程  | 我要评论

山楂树之恋大结局,侯卫东官场,德云社岳云鹏专场

一、安装

二、安装后会在根目录出现nlog.config配置文件,简单修改配置文件为写入文件记录日志:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/nlog.xsd"
      xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
      xsi:schemalocation="http://www.nlog-project.org/schemas/nlog.xsd nlog.xsd"
      autoreload="true"
      throwexceptions="false"
      internalloglevel="error" internallogfile="c:\temp\nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/nlog/wiki/configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  see https://github.com/nlog/nlog/wiki/configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets>

    <!--
    add your targets here
    see https://github.com/nlog/nlog/wiki/targets for possible targets.
    see https://github.com/nlog/nlog/wiki/layout-renderers for the possible layout renderers.
    -->

    
    <!--write events to a file with the date in the filename.-->
    <target xsi:type="file" name="f" filename="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    
  </targets>

  <rules>
    <!-- add your logging rules here -->

    <!--write all events with minimal level of debug (so debug, info, warn, error and fatal, but not trace)  to "f"-->
    <logger name="*" minlevel="debug" writeto="f" />
  </rules>
</nlog>

 

三、使用方法:

 private static logger logger = logmanager.getcurrentclasslogger();

 protected void application_error(object sender, eventargs e)
        {
            exception lasterror = server.getlasterror();
            if (lasterror != null)
            {
                logger.error(lasterror);
            }
        }
  logger.info("application_start");

 

简单的异常日志写入完成,看了配置项太多头有点大,先这样了

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网