Nlog: Why custom layout renderer can be displayed in the debug mode in other modes can not be displayed

Created on 22 Apr 2017  路  6Comments  路  Source: NLog/NLog

Why custom layout renderer can be displayed in the debug mode in other modes can not be displayed.

<target xsi:type="Mongo"
            name="mongoDefault"
            connectionString="xxx"
            collectionName="logs"
            includeDefaults ="false"
            cappedCollectionSize="26214400">
      <field name="AppName" layout="${AppName}"/>
    </target>
 LayoutRenderer.Register("AppName", (logEvent) => "vxxxx");
logger.Error("xxxxxxxxxxxx");



md5-d84a076b3292dd46d80b317b83c88944



<logger name="*" minlevel="Error" writeTo="mongoDefault" />
question

Most helpful comment

Probably the order of initialization, need to call LayoutRenderer.Register first before initializing any static variables that calls LogManager.GetCurrentClassLogger().

Alternative you should add the this call after having called the last LayoutRenderer.Register:

LogManager.Configuration = LogManager.Configuration;

All 6 comments

Please elaborate and use the template.

I'm guessing it is an issue with loading the NLog-config file in release mode. Like this issue:

http://stackoverflow.com/questions/43535561/why-does-nlog-logmanager-return-null-for-release-build-but-works-fine-for-debug

But if it is placed correctly, then it is time to activate the internal log (As the issue template specifies).

@304NotModified
This is a complete configuration
This is the usage, written in Global

private readonly Logger logger = LogManager.GetCurrentClassLogger();
 LayoutRenderer.Register("AppName", (logEvent) => "abcd");
                LayoutRenderer.Register("StackTrace", (logEvent) => httpError.StackTrace);
                LayoutRenderer.Register("UserName", (logEvent) => userName);
                LayoutRenderer.Register("Ip", (logEvent) => IpHelper.GetIP());
                logger.Error(httpError.Message);

This is the result of running the wrong report

LayoutRenderer cannot be found: 'AppName'
<?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">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>
  <extensions>
    <add assembly="NLog.Web"/>
    <add assembly="NLog.Mongo"/>
  </extensions>
  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets async="true">
    <target name="logfile" xsi:type="File" fileName="file.txt" layout="${AppName}${UserName}" />
    <target xsi:type="Mongo"
            name="mongoDefault"
            connectionString="mongodb://examda_j2oi3j12k:jfo23i490i2lmfkl[@192.168.1.1:1234/ExamdaDb"
            collectionName="logs"
            includeDefaults ="false"
            cappedCollectionSize="26214400">
      <field name="AppName" layout="${AppName}"/>
      <field name="level" layout="${level}"/>      
      <field name="message" layout="${message}"/>
      <field name="StackTrace" layout="${StackTrace}"/>
      <field name="basedir" layout="${basedir}"/>
      <field name="UserName" layout="${UserName}"/>
      <field name="Ip" layout="${Ip}"/>
      <field name="Url" layout="${aspnet-Request-Url}" />
      <field name="Referrer" layout="${aspnet-Request-Referrer} " />
      <field name="QueryString" layout="${aspnet-Request-QueryString}" />
      <field name="UserAgent" layout="${aspnet-Request-UserAgent}" />
      <field name="Method" layout="${aspnet-Request-Method}" />
      <field name="date" layout="${date}"/>
    </target>

    <target xsi:type="Mail"
          name="sendEmail"
          header="-----"
          footer="-----"
          layout="-----"
          html="true"
          addNewLines="true"
          replaceNewlineWithBrTagInHtml="true"
          encoding="utf-8"
          subject="${AppName}@閿欒鏃ュ織"
          to="[email protected]"
          bcc=""
          cc=""
          from="[email protected]"
          body="${message}##${StackTrace}##${aspnet-Request-Url}"
          smtpUserName="[email protected]"
          enableSsl="false"
          smtpPassword="11"
          smtpServer="mail.11.com"
          smtpPort="25"
          timeout="10000" />

    <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->

    <!--
    Write events to a file with the date in the filename.
    <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="*" minlevel="Error" writeTo="mongoDefault" />
    <logger name="*" minlevel="Error" writeTo="sendEmail" />
    <logger name="*" minlevel="Error" writeTo="logfile" />
    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />
    -->
  </rules>
</nlog>

Probably the order of initialization, need to call LayoutRenderer.Register first before initializing any static variables that calls LogManager.GetCurrentClassLogger().

Alternative you should add the this call after having called the last LayoutRenderer.Register:

LogManager.Configuration = LogManager.Configuration;

@snakefoot
Thank you very much.

Please let us know if this issue could be closed, thank!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haythamabutair picture haythamabutair  路  3Comments

geedsen picture geedsen  路  3Comments

Jerefeny picture Jerefeny  路  3Comments

imanushin picture imanushin  路  3Comments

BobSeu picture BobSeu  路  3Comments