0

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.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 2
    AFAIK the configuration for nlog is in _nlog.config_ or _appsettings.json_ See here https://github.com/NLog/NLog/wiki/Configuration-file – Steve Jul 24 '23 at 08:41
  • @Steve: thanks for your quick reply, but there is no `*.json` file present. There are some `application.exe.config` and `library.dll.config` files present, but those only contain the following `NLog` related entry: ` `. – Dominique Jul 24 '23 at 09:02
  • You need a nlog.config or add the relevant info in the application.exe.config. I think the link above has all the info (config in json is for web NET Core apps) – Steve Jul 24 '23 at 09:04
  • @Steve: I only have an `NLog.xml` file, containing (amongst others) the following entry: ``. I indeed think that this file contains the information I'm looking for, but seen the large numbers of members, how can I know which one to modify in order to alter the `Log.Debug()` output? – Dominique Jul 24 '23 at 09:07
  • [This link](https://stackoverflow.com/questions/26765934/is-there-a-way-to-put-nlog-config-information-inside-of-my-app-config-file) instead for configuration inside the application.exe.config – Steve Jul 24 '23 at 09:08
  • 1
    I don't think that xml file is the one to use for configuration. It seems more like an "intellisense" support file. The configuration is in the nlog,config file (NET Framework and Net Core) or in the appsettings.json (net core) or application.exe.config (net framework) – Steve Jul 24 '23 at 09:10

2 Answers2

1

Add a nlog.config -file to your project. Set the properties of the file to "Copy if Newer" to ensure it is copied to the output directory.

Then copy the some example configuration to your file. For example the one from the configuration file wiki

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

    <targets>
        <target name="logfile" xsi:type="File" fileName="file.txt" />
        <target name="logconsole" xsi:type="Console" />
    </targets>

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

You can then start making changes, like changing the "layout" of the target:

<target 
  layout="${longdate}|${level:uppercase=true}|${logger}|${message}"
  name="logconsole" 
  xsi:type="Console" />

For a list of tags you can use in your layout, see layout renderers.

There are other ways to handle configuration, like putting the entire nlog-tag inside your app.config file. But the app.config can become kind of bloated, so I think it is better to use a separate file.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • Thanks for your answer. You are saying "This is an example of **a** configuration you might use.", while I'm looking for **the** configuration which is **being used**. In order to achieve this, I've added `var test = LogManager.Configuration;` to my application, and I'm checking the values in order to create an `NLog.config` based on the input of your answer. – Dominique Jul 24 '23 at 11:34
  • @Dominique I do not think nlog will log anything if you don't have *any* log configuration. I do not recognize your nlog.xml, it does not look at all like I'm used to, maybe some older format? If that is the case it is probably a good idea to port it. But whatever you have currently might not match what you actually want, so the effort might be better spent asking around how it should work rather than trying to replicate behavior no one is satisfied with. – JonasH Jul 24 '23 at 12:17
  • In the meantime I found out what was going wrong: my application is in fact a DLL, launched by another application, and that application has an `NLog` configuration, written **in source code** (hardcoded). This makes it impossible to modify any `NLog` configuration, but your answer might be useful for other people with a similar problem, hence I accepted it. – Dominique Jul 24 '23 at 12:22
1

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.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70