第一、首先下载nlog.dll 下载地址:http://pan.baidu.com/s/1i3DQsV7

第二、添加nlog.ll的引用

第三、代码

private static Logger log = LogManager.GetCurrentClassLogger();

        protected void Page_Load(object sender, EventArgs e)
        {

            //记录错误日志
            log.Error("发生了一个异常");
            //记录严重错误
            log.Fatal("发生了一个致命错误");
            //记录一般信息
            log.Info("info");
            //记录调试信息
            log.Debug("debug");
            //记录警告信息
            log.Warn("warn");

            try
            {
                throw new Exception("未将。。。");
            }
            catch (Exception ex)
            {
                log.Error("发生异常{0}", ex);
            }
        }

第四、config配置

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwExceptions="true" internalLogFile="e:\nlog1.txt" internalLogLevel="DEBUG">

  <!-- 
  See http://nlog-project.org/wiki/Configuration_file 
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <!-- add your targets here -->
    <target name="file" xsi:type="File"  fileName="E:\bzylog/${shortdate}.txt"
            layout="${longdate} ${level}:${message} ${stacktrace}" />
    <target name="fi" xsi:type="File" fileName="App_Data/Log ${shortdate}.txt"
           layout="${longdate} ${level}:${message} ${stacktrace}" />
    <!--<target type="Database" name="database" connectionstring="Data Source=.;Initial Catalog=ReportServerTempDB;Integrated Security=True">
      <commandText>
        insert into DevLog ([AppName],[ModuleName],[ProcName],[LogLevel],[LogTitle],[LogMessage],[LogDate],[StackTrace]) values (@appName, @moduleName, @procName, @logLevel, @logTitle, @logMessage,@logDate,@stackTrace);
      </commandText>
      <parameter name="@appName" layout="${event-context:item=appName}"/>
      <parameter name="@moduleName" layout="${event-context:item=moduleName}"/>
      <parameter name="@procName" layout="${event-context:item=procName}"/>
      <parameter name="@logLevel" layout="${event-context:item=logLevel}"/>
      <parameter name="@logTitle" layout="${event-context:item=logTitle}"/>
      <parameter name="@logMessage" layout="${event-context:item=logMessage}"/>
      <parameter name="@logDate" layout="${longdate}"/>
      <parameter name="@stackTrace" layout="${stacktrace}"/>
    </target>-->
    <!--
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->
  </targets>

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

    <logger name="*"   writeTo="file"/>
    <!--<logger name="*"  levels="Debug,Error" writeTo="fi"/>-->
    <!--<logger name="Log" minlevel="Info" appendTo="database"/>-->
    <!--
    <logger name="*" minlevel="Trace" writeTo="f" />
    -->
  </rules>
</nlog>

 

 

posted on 2013-12-24 11:03  王洪洪  阅读(696)  评论(0编辑  收藏  举报