A simpler solution would be to use the NLog Global Diagnostics Context option.
For example, before recording an event, set a custom value named "IpAddress":
public IActionResult AnAction() { NLog.GlobalDiagnosticsContext.Set("IpAddress", HttpContext.Connection.RemoteIpAddress); _logger.LogInformation("Something happened"); return View(); }
And in your nlog.config file, you can use this value in the layout, for example:
<target xsi:type="File" name="allfile" fileName="c:\nlog.log" layout="${longdate} | ${message} | ${gdc:item=IpAddress}" />
source share