1
votes

I have a unique problem with the NLog. I have a third-party assembly we use into our project. They have used the NLog into their project and referenced NLog configuration into their configuration which I don't have access to. I initially had a problem even adding the NLog to a project since both version of dlls were different. I had to download the source code of NLog and change the assembly name since alias was not working for me (OR I didn't know how to use it.) After giving my own assembly name, NLog started wotrking but.. It started logging third-party log information too with it. Looks like they were not careful enough when defining the logger and they just defined (*). Now my question is, how do I avoid their stuff from logging in to my file? I have also tried setting it to final = true. Here is how my very simple configuration file looks like.

<?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">

  <!-- 
  See https://github.com/nlog/nlog/wiki/Configuration-file 
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <!-- add your targets here -->

    <!--
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->

    <target name="errorLog" xsi:type="File"
            fileName="${basedir}/logs/error/${shortdate}.log" 
            layout="${longdate} ${uppercase:${level}} ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}"

    />
    <target name="generalLog" xsi:type="File"
            fileName="C:/logs/${date:format=yyyy-MM-dd}.log"
            layout="${longdate} ${uppercase:${level}} ${message}"/>   

  </targets>


  <rules>
    <!-- add your logging rules here -->
    <logger name="Errors" writeTo="errorLog" minlevel="Warn"/>
    <logger name="EBusiness.Model.*" writeTo="generalLog" levels="Trace,Debug,Info" final="true"/>

    <!--
    <logger name="*" minlevel="Trace" writeTo="f" />
    -->
  </rules>
</nlog>
1

1 Answers

0
votes

If they are writing to the root logger, you should configure the root logger to not log to anywhere you care (maybe point it at a console logger) and your application can log to a specific logger which does write to your files.

Ugly, but should work.

For example: Add this target:

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

And Change the * rule to be:

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

Then, Add your own logger rule, say something like:

<logger name="MyNameSpace.*" minlevel="Trace" writeTo="logfile" final="true" />

Assuming you have a logfile target defined pointing at your file.