选中Server.App项目

1、新建 NLogConsole.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">
  <variable name="appType" value="" />
  <variable name="appId" value="" />
  <targets>
    <target name="all" xsi:type="Console"
                layout="${longdate} ${var:appTypeFormat} ${var:appIdFormat} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message}" >

    </target>
  </targets>

  <targets>
    <target name="trace" xsi:type="Console"
                layout="${longdate} ${var:appTypeFormat} ${var:appIdFormat} ${message} ${newline} ${stacktrace:format=Raw:topFrames=10:skipFrames=0}">

    </target>
  </targets>

  <targets>
    <target name="debug" xsi:type="ColoredConsole"
                layout="${longdate} ${var:appTypeFormat} ${var:appIdFormat} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message}" >
      <highlight-row condition="level == LogLevel.Debug" foregroundColor="DarkGray" />
    </target>
  </targets>

  <targets>
    <target name="info" xsi:type="ColoredConsole"
                layout="${longdate} ${var:appTypeFormat} ${var:appIdFormat} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message}" >
      <highlight-row condition="level == LogLevel.Info" foregroundColor="Gray" />
    </target>
  </targets>

  <targets>
    <target name="error" xsi:type="ColoredConsole"
                layout="${longdate} ${var:appTypeFormat} ${var:appIdFormat} ${message} ${newline} ${stacktrace:format=Raw:topFrames=10:skipFrames=0}" >
      <highlight-row condition="level == LogLevel.Error" foregroundColor="Red" />
    </target>
  </targets>

  <targets>
    <target name="warn" xsi:type="ColoredConsole"
                layout="${longdate} ${var:appTypeFormat} ${var:appIdFormat} ${message}" >
      <highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" />
    </target>
  </targets>

  <targets>
    <target name="fatal" xsi:type="ColoredConsole"
                layout="${longdate} ${var:appTypeFormat} ${var:appIdFormat} ${message} ${newline} ${stacktrace:format=Raw:topFrames=10:skipFrames=0}" >
      <highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" backgroundColor="White" />
    </target>
  </targets>

  <rules>
    <!--<logger name="*" minlevel="Trace" writeTo="all" />-->
    <logger name="*" minlevel="Trace" maxlevel="Trace" writeTo="trace" />
    <logger name="*" minlevel="Debug" maxlevel="Debug" writeTo="debug" />
    <logger name="*" minlevel="Info" maxlevel="Info" writeTo="info" />
    <logger name="*" minlevel="Warn" maxlevel="Warn" writeTo="warn" />
    <logger name="*" minlevel="Error" maxlevel="Error" writeTo="error" />
    <logger name="*" minlevel="Fatal" maxlevel="Fatal" writeTo="fatal" />
  </rules>
</nlog>

2、选中Server.Model项目,修改 NLogAdapter.cs 这个类

using NLog;
using NLog.Config;

namespace ETModel
{
    public class NLogAdapter: ILog
    {
        private readonly Logger logger = LogManager.GetLogger("Logger");

        public NLogAdapter() 
        {

#if DEBUG
            //初始化配置日志
            string path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "NLogConsole.config");
            LogManager.Configuration = new XmlLoggingConfiguration(path);
            Debug("使用控制台输出日志");
#endif

        }

        public void Trace(string message)
        {
            this.logger.Trace(message);
        }

        public void Warning(string message)
        {
            this.logger.Warn(message);
        }

        public void Info(string message)
        {
            this.logger.Info(message);
        }

        public void Debug(string message)
        {
            this.logger.Debug(message);
        }

        public void Error(string message)
        {
            this.logger.Error(message);
        }

        public void Fatal(string message)
        {
            this.logger.Fatal(message);
        }

        public void Trace(string message, params object[] args)
        {
            this.logger.Trace(message, args);
        }

        public void Warning(string message, params object[] args)
        {
            this.logger.Warn(message, args);
        }

        public void Info(string message, params object[] args)
        {
            this.logger.Info(message, args);
        }

        public void Debug(string message, params object[] args)
        {
            this.logger.Debug(message, args);
        }

        public void Error(string message, params object[] args)
        {
            this.logger.Error(message, args);
        }

        public void Fatal(string message, params object[] args)
        {
            this.logger.Fatal(message, args);
        }
    }
}


至此,修改完成!