NLog not writing debug messages

左心房为你撑大大i 提交于 2020-06-27 08:13:06

问题


In my code I have some info messages like logger.Log("dwewe") and logger.Debug("ddddf").

The problem is that the Debug messages are not being written even when I debug in VS.

<?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"
      throwExceptions="true"
      internalLogLevel="Trace"
      internalLogFile="c:\nlog-app.log"
      autoReload="false"
      internalLogToConsole="true">
  <!--
  See http://nlog-project.org/wiki/Configuration_file
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <!-- file targets -->
    <target name="asyncFile" xsi:type="AsyncWrapper">
      <target xsi:type="File" name="f" fileName="${basedir}/Logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message} ${event-context:item=error-source} ${event-context:item=error-class} ${event-context:item=error-method} ${event-context:item=error-message} ${event-context:item=inner-error-message} ${event-context:item=stack-trace}"/>
    </target>

    <!-- database targets -->
    <target name="database" xsi:type="Database" keepConnection="true" useTransactions="true"
             dbProvider="System.Data.SqlClient"
             connectionString="data source=XXXXXX.database.windows.net;initial catalog=NLog;integrated security=false;persist security info=True;User ID=XXXXr;Password=BXXXX3"
              commandText="INSERT INTO Logs(EventDateTime, EventLevel, UserName, MachineName, EventMessage, ErrorSource, ErrorClass, ErrorMethod, ErrorMessage, InnerErrorMessage, StackTrace) VALUES (@EventDateTime, @EventLevel, @UserName, @MachineName, @EventMessage, @ErrorSource, @ErrorClass, @ErrorMethod, @ErrorMessage, @InnerErrorMessage, @StackTrace)">
      <!-- parameters for the command -->
      <parameter name="@EventDateTime" layout="${date:s}" />
      <parameter name="@EventLevel" layout="${level}" />
      <parameter name="@UserName" layout="${windows-identity}" />
      <parameter name="@MachineName" layout="${machinename}" />
      <parameter name="@EventMessage" layout="${message}" />
      <parameter name="@ErrorSource" layout="${event-context:item=error-source}" />
      <parameter name="@ErrorClass" layout="${event-context:item=error-class}" />
      <parameter name="@ErrorMethod" layout="${event-context:item=error-method}" />
      <parameter name="@ErrorMessage" layout="${event-context:item=error-message}" />
      <parameter name="@InnerErrorMessage" layout="${event-context:item=inner-error-message}" />
      <parameter name="@StackTrace" layout="${event-context:item=stack-trace}" />

    </target>

    <target name="console" xsi:type="Console" />

  </targets>
  <rules>
    <!-- add your logging rules here -->
    <logger name="*" minlevel="Info" writeTo="database" />
    <logger name="*" minlevel="Error" writeTo="asyncFile" />
    <logger name="*" minlevel="Trace" writeTo="console" />
  </rules>
</

回答1:


The reason that you are not able to get Debug is that debug is the lowest level log level just add following tag in rules tag in nlog.config file.

 <logger name="*" minlevel="Debug" writeTo="console" />



回答2:


You are using 3 different log targets.

Database target is set to Info level, so debug messages are not going there. File target accepts only Error messages (and higher) so there will not be any debug either.

The last target Console is the one where debug mesages should be looged to. But as I see it you didn't set layout of the message. Try to look at this documentation. It says that layout is a required field.

Also I would suggest you temporarily set additional File target and set it to accept debug messages.




回答3:


For anyone else who has this issue, an answer at a similar question just saved me: https://stackoverflow.com/a/8881521/3959735

If you are using a separate NLog.config file, make sure it's set to "copy always" via its file properties. Alternatively, you can include the NLog configuration section in your main App.config.

I think I might have caused this issue for myself either by trying to copy an NLog configuration file from another project manually; or because when adding NLog to my project I got a permissions error (of which I cannot remember any specific details) – just passing on this information in case it helps anyone diagnose their own issue.




回答4:


Add nlog.config file to your project

Example of my config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
      <target name="logfile" xsi:type="File" fileName="${basedir}/Logs/${date:format=yyyy-MM-dd}_log.txt" />
      <target name="logconsole" xsi:type="Console" />
    </targets>

    <rules>
      <logger name="*" minlevel="Info" writeTo="logconsole" />
      <logger name="*" minlevel="Debug" writeTo="logfile" />
    </rules>
  </nlog>
</configuration>


来源:https://stackoverflow.com/questions/21997585/nlog-not-writing-debug-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!