使用Nlog Methodcall目标的asp.net MVC 5项目没有任何效果

时间:2018-03-18 15:29:37

标签: asp.net-mvc nlog

在ASP.net MVC 5项目中,使用Nuget安装Nlog,Nlog.config,Nlog.schema Packages。 在NLog.config文件中定位xsi:type =“MethodCall”

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="Off" internalLogFile="c:\temp\nlog-internal.log">
  <targets>
    <target name="mc" xsi:type="MethodCall" className="NlogMethodCallWebApp.Models.NLogHelper, NlogMethodCallWebApp" methodName="LogMethod">
      <parameter layout="${longdate}" />
      <parameter layout="${uppercase:${level}}" />
      <parameter layout="${message}" />
    </target>

  </targets>

  <rules>
    <logger name="*" minlevel="Trace,Debug,Info,Warn,Error,Fatal" writeTo="mc" />
  </rules>
</nlog>

执行Debug HomeController索引操作方法

HomeController.cs

using NLog;
using System.Web.Mvc;

namespace NlogMethodCallWebApp.Controllers
{
    public class HomeController : Controller
    {
        private static Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public ActionResult Index()
        {
            logger.Trace("This is Trace");
            logger.Debug("This is Debug");
            logger.Info("This is Info");
            logger.Warn("This is Warn");
            logger.Error("This is Error");
            logger.Fatal("This is Fatal");

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

LogMethod在类NLogHelper中设置断点无效

NLogHelper.cs

using System;
using System.Diagnostics;

namespace NlogMethodCallWebApp.Models
{
    public class NLogHelper
    {
        /// <summary>
        /// c - NLog
        /// </summary>
        public static void LogMethod(string longdate, string level, string message)
        {
            Trace.WriteLine(string.Format("D:{0} L:{1} M:{2}", longdate, level, message));
        }
    }
}

NLog不执行LogMethod方法。问题在哪里?

1 个答案:

答案 0 :(得分:0)

你的loglevel是错误的。 有多种可能性:

minlevel="Trace,Debug,Info,Warn,Error,Fatal"&lt; - 你的,错了

更改为:

levels="Trace,Debug,Info,Warn,Error,Fatal"

minlevel="Trace"

或者您也可以使用minlevel和maxlevel

详细说明: https://github.com/NLog/NLog/wiki/Configuration-file#log-levels

同时检查程序集是否在输出目录中(您的项目必须引用此程序集)