0

this question is similar to How do I log a custom field in NLog to database? , but in my case I would like to log the same value in each log. Typically this will be a hardcoded component identifier e.g. MAC adress, which we read once at start and want to have inserted automatically into all logs.

If I follow the quoted answer, I could do something like: LogEventInfo theEvent = new LogEventInfo(logLevel, "", message); theEvent.Properties["ComponentID"] =MACADDRESS;`

log.Log(theEvent);

but I would like to avoid having to do this for each log. Is there some way to have this value automatically by specifying a layout for the file logger with perhaps an nlog parameter something like:

<parameter name="@ComponentID" layout="${ComponentID}"/>

I would then set the value somewhere in my code

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
shelbypereira
  • 2,097
  • 3
  • 27
  • 50

2 Answers2

1

You could use a NLog variable, but there is a caveat that the value will be reset if NLog configuration file is changed, and the auto-reload feature is enabled:

LogManager.Configuration.Variables["ComponentID"] = "MACADDRESS";

The docs mention it should be used dynamically (${var:ComponentID}) rather that statically (${ComponentID}), if you're going to change the value in runtime.

There is actually a layout renderer called Global Diagnostic Context whose purpose is exactly what you need. The value will persist even in case described above.

GlobalDiagnosticsContext.Set("ComponentID","MACADDRESS");

Use it like this: ${gdc:item=ComponentID}.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • @shelbypereira The correct answer is to use the GlobalDiagnosticsContext. The NLog Config Variables belongs to the active Configuration, and will be reset when update the NLog Configuration (Ex. ``). – Rolf Kristensen May 31 '21 at 16:37
0

You could wrap the NLog class and prepend your MAC address to all logs.

public class MyLogger
{
    public static string MacAddress { get; set; }

    private NLog.Logger _logger;

    public MyLogger() : this(null) { }

    public MyLogger(string name)
    {
        _logger = NLog.LogManager.GetLogger(name ?? "");
    }

    public void Log(LogLevel level, string message, Exception exception = null)
    {
        if (level >= Manager.Instance.MinLevel)
        {
            var info = new NLog.LogEventInfo(level, _logger.Name, "<" + MacAddress + "> " + message);
            if (exception != null)
            {
                info.Exception = exception;
            }
            _logger.Log(typeof(NLogLogger), info);
        }
    }

    public void Trace(string message, Exception exception = null)
    {
        this.Log(LogLevel.Trace, message, exception);
    }

    public void Debug(string message, Exception exception = null)
    {
        this.Log(LogLevel.Debug, message, exception);
    }

    public void Info(string message, Exception exception = null)
    {
        this.Log(LogLevel.Info, message, exception);
    }

    public void Warn(string message, Exception exception = null)
    {
        this.Log(LogLevel.Warn, message, exception);
    }

    public void Error(string message, Exception exception = null)
    {
        this.Log(LogLevel.Error, message, exception);
    }

    public void Fatal(string message, Exception exception = null)
    {
        this.Log(LogLevel.Fatal, message, exception);
    }
}

Then you can set:

MyLogger.MacAddress = "XXX";
laurian
  • 739
  • 6
  • 18