Hi, looking at how the current machineName is derived code, I see it reads the ComputerName environment variable which as per this article is by default limited to 15 characters. This is a problem for AWS instance IDs for example or any custom hostname that exceeds 15 characters.
NLog version: (e.g. 4.4.13)
master
Platform:
.NET Framework/Windows
Current NLog config (xml or C#, if relevant)
NA
Machine name logged as: I-03FB71646161E .
I was expecting the machine name should be the same as hostname (in my case, AWS instance ID
i-03fb71646161e8626)
Yes
NA
Not sure
No.
Perhaps, if guided.
While waiting for someone to fix this then you can register your own custom layout renderer:
https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer
And lookup the computer name like this maybe:
```c#
LayoutRenderer.Register("computername", (logEvent) => GetComputerName());
/// <summary>
/// Gets the machine name
/// </summary>
private static string GetComputerName()
{
return TryLookupValue(() => Environment.GetEnvironmentVariable("COMPUTERNAME"), "COMPUTERNAME")
?? TryLookupValue(() => Environment.GetEnvironmentVariable("HOSTNAME"), "HOSTNAME")
?? TryLookupValue(() => System.Net.Dns.GetHostName(), "DnsHostName")
?? TryLookupValue(() => Environment.MachineName, "MachineName");
}
/// <summary>
/// Tries the lookup value.
/// </summary>
/// <param name="lookupFunc">The lookup function.</param>
/// <param name="lookupType">Type of the lookup.</param>
/// <returns></returns>
private static string TryLookupValue(Func<string> lookupFunc, string lookupType)
{
try
{
string lookupValue = lookupFunc()?.Trim();
return string.IsNullOrEmpty(lookupValue) ? null : lookupValue;
}
catch (Exception ex)
{
NLog.Common.InternalLogger.Warn(ex, "Failed to lookup {0}", lookupType);
return null;
}
}
```
Thanks, @snakefoot - will give it a shot. If I get some guidance, I could try creating a PR as well.
Thanks, @snakefoot - will give it a shot. If I get some guidance, I could try creating a PR as well.
See https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer
Need any help on this @amitsaha ?
hi @304NotModified - Plan to take a look at this in the next week.
馃憤
hey @304NotModified - just so we are clear i am planning to work on the fix for this - i.e. updating https://github.com/NLog/NLog/blob/3a2f1214700464e969e92384ea80dcaffec49b4e/src/NLog/Internal/EnvironmentHelper.cs#L64 - that's what we want to do right?
@amitsaha that's what we want to do right?
Please create a new layoutrenderer without touching/replacing the existing logic. When it woks as intended, then we can decide where it belongs.
@snakefoot @304NotModified done. please take a look. I went ahead and implemented a new layout renderer, since it probably makes more sense IMO.
Fixed by #3017. Thanks!
Most helpful comment
@snakefoot @304NotModified done. please take a look. I went ahead and implemented a new layout renderer, since it probably makes more sense IMO.