欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

NLog简单使用

程序员文章站 2022-05-03 12:34:14
一、安装 二、安装后会在根目录出现NLog.config配置文件,简单修改配置文件为写入文件记录日志: 三、使用方法: 简单的异常日志写入完成,看了配置项太多头有点大,先这样了 ......

一、安装

NLog简单使用

二、安装后会在根目录出现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");

 

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