Search code examples
c#loggingconfigurationnlog

How to configure NLog for isolated plugin within host application


I'm working on a C# application, using the NLog library.

Log.Info() generates an output, which looks like the following:

2023-07-24 09:04:19.4423 | Info | Company.Customer.Manager.DoSomething | Some information

I would like to change that, and in the NLog.xml, I have found entries like:

<code>${longdate}|${level:uppercase=true}|${logger}|${message}</code>

... but there are some drawbacks:

  1. I have found over 30 of those entries in NLog.xml, belonging to members with following names: M:NLog.Targets.TargetWithLayout.#ctor, P:NLog.Targets.TargetWithLayout.Layout, M:NLog.Targets.NetworkTarget.#ctor, and so on.
  2. The entries have the following structure:
<member name="...">
    <summary>
        Some information
    </summary>
    <remarks>
        The default value of the layout is: 
        <code>${longdate}|${level:uppercase=true}|${logger}|${message}</code>
    </remarks>
</member>

=> basically it looks "just" like a remark.

So my questions are:

  • Is it correct that the configuration of the NLog is somewhere in the remark of a member?
  • If yes, how can I know which member?
  • If no, where can I find that configuration?

Oh, before I forget: I don't have an NLog.config file.


Solution

  • When creating a plugin that will live as guest in the house of the host-application, then it is not good style for the guest to replace the carpet in the living-room.

    The plugin should not attempt to modify the NLog-configuration, as it can give unwanted side-effects for the host-application. When the plugin uses NLog.LogManager.GetCurrentClassLogger() then its output will depend on the NLog-configuration for the host-application.

    If the plugin wants to have its own isolated NLog-configuration, then the plugin can initialize its own isolated NLog-LogFactory. Instead of using the NLog.LogManager. See also: https://github.com/NLog/NLog/wiki/Configure-component-logging#isolated-logfactory

    The NLog.xml-file only contains XML-docs for Visual Studio Intellisense. NLog.dll doesn't use NLog.xml for anything.