NLog控制台应用程序

时间:2016-01-22 11:53:33

标签: c# .net console-application nlog

我有一个控制台应用程序,并且为了记录其活动,我复制了我在Web应用程序上使用的相同Nlog.config文件。 我还导入了 Nlog.Extended Nlog.Web 。 我还检查了Nlog.config属性( Build Action = Content,copy to output directory = copy Always )并设置了internalLogFile和internalLogLevel。

^(asd|asdf)$

被称为'System.TypeInitializationException',并且毕竟是

private static Logger logger = LogManager.GetCurrentClassLogger();

在我的内部日志文件

上进行比较
Scanning XXXXLayoutRenderer (all parameter in my config)

没有别的

这是我的 NLog.config 文件

Info Found 105 configuration items

这是输出内部日志文件

  <?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="true"
        internalLogFile="c:\Temp\internal_log_file.txt"
        internalLogLevel="Trace"
        internalLogToConsole="true">


    <variable name="brief" value="${longdate} | ${level} | ${message}"/>
    <variable name="verbose" value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} | ${message}"/>
    <variable name="logDirectory" value="${basedir}/Logs/${date:format=yyyyMM}"/>

    <targets>


      <!-- FILE target -->
      <target name="file" xsi:type="File" layout="${brief}" fileName="${logDirectory}/${shortdate}.txt"/>

      <!-- EMAIL target -->
      <target name="email" xsi:type="Mail"
              smtpServer="smtp.gmail.com"
              smtpPort="587"
              smtpAuthentication="Basic"
              smtpUserName="user@gmail.com"
              smtpPassword="password"
              enableSsl="true"
              from="user@gmail.com"
              to="user@gmail.com"
              layout="${verbose}"
            />

      <!-- DATABASE target -->
      <target name="database" xsi:type="Database">


        <connectionStringName>NLogEntities</connectionStringName>

        <commandText>
          insert into dbo.Log (
          Application, Logged, Level, Message,
          Username,
          ServerName, Port, Url, Https,
          ServerAddress, RemoteAddress,
          Logger, CallSite, Exception
          ) values (
          @Application, @Logged, @Level, @Message,
          @Username,
          @ServerName, @Port, @Url, @Https,
          @ServerAddress, @RemoteAddress,
          @Logger, @Callsite, @Exception
          );
        </commandText>
        <parameter name="@application" layout="${appsetting:key=AppName:default=Unknown}" />
        <parameter name="@logged" layout="${date}" />
        <parameter name="@level" layout="${level}" />
        <parameter name="@message" layout="${message}" />

        <parameter name="@username" layout="${identity}" />

        <parameter name="@serverName" layout="${aspnet-request:serverVariable=SERVER_NAME:default=Unknown}" />
        <parameter name="@port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
        <parameter name="@url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
        <parameter name="@https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />

        <parameter name="@serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
        <parameter name="@remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />

        <parameter name="@logger" layout="${logger}" />
        <parameter name="@callSite" layout="${callsite}" />
        <parameter name="@exception" layout="${exception:tostring}" />
      </target>
    </targets>
    <rules>
      <!-- DATABASE rules -->
      <logger name="*" minlevel="Error" writeTo="database" />
      <!-- EMAIL rules -->
      <logger name="*" minlevel="Error" writeTo="email" />
      <!-- FILE rules -->
      <logger name="*" minlevel="Trace" writeTo="file" />
    </rules>
  </nlog>

问题出在哪里?

1 个答案:

答案 0 :(得分:2)

发现解决方案!!! 我不得不改变这个

<parameter name="@application" layout="${appsetting:key=AppName:default=Unknown}" />

用这个

<parameter name="@application" layout="${appsetting:name=AppName:default=Unknown}" />

字必须替换为 name 字。

解决。