0

I'm working on a C# application, using NLog for logging purposes.

I have just seen the following in a logfile:

2023-06-08 11:15:58.1816 | NameSpace.ClassName | CheckMATCO | Check CheckFixstation ...

This is caused by the following piece of source code:

private static readonly Logger Log = LogManager.GetCurrentClassLogger();

public void CheckFixstation(...)
{
    Log.Debug(string.Format("CheckMATCO | Check CheckFixstation ..."));

This, obviously, is wrong (my colleague wanted to have the methodname in the log, but as he didn't know how to achieve this, he added it himself and did some wrong copy paste). I would like to see the following in the logfile:

2023-06-08 11:15:58.1816 | NameSpace.ClassName | CheckFixStation | Check CheckFixstation ...

In order to obtain this, I only want to use this log command:

Log.Debug("Check CheckFixstation ...");

This means that I need to do the following around my Log declaration:

LogManager.AlterOutputInOrderToAddMethodName();
private static readonly Logger Log = LogManager.GetCurrentClassLogger();

How can I do this?

Thanks in advance

Edit after first comments:
I've found a file, called nlog.config, containing the following target:

<target 
  xsi:type="File"
  layout="${longdate} | ${logger} | ${message} ${onexception:--- ${exception:format=tostring}}" 
  fileName="${basedir}/Logs/${shortdate}.${level}.log" />

How can I update this in order to add the methodname?

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • Method names should be included unless you disabled it in the config. – shingo Jun 08 '23 at 10:30
  • @shingo: thanks for your quick reply. Do you know where and how to disable this, so that I can enable it again? – Dominique Jun 08 '23 at 11:39
  • 1
    In the config file, Idk which one you loaded, its common name is nlog.config or web.nlog. Then search layout or methodName in it. https://github.com/NLog/NLog/wiki/Callsite-Layout-Renderer – shingo Jun 08 '23 at 12:35
  • Notice the NLog CallSite-Logic will have less performance overhead when using `logger.ForLogEvent()` with NLog v5. There is also alternatives like https://github.com/Fody/Anotar or https://github.com/csnemes/tracer – Rolf Kristensen Jun 08 '23 at 16:45

0 Answers0