How to add methodname in logging using runtime reconfiguration

67 views Asked by At

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?

0

There are 0 answers