Nlog: Machine name greater than 15 characters truncated

Created on 21 Nov 2018  路  10Comments  路  Source: NLog/NLog

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

  • What is the current result?

Machine name logged as: I-03FB71646161E .

  • What is the expected result?

I was expecting the machine name should be the same as hostname (in my case, AWS instance ID
i-03fb71646161e8626)

Yes

  • Please post full exception details (message, stacktrace, inner exceptions)

NA

  • Are there any workarounds? yes/no

Not sure

  • Is there a version in which it did work?

No.

  • Can you help us by writing an unit test?

Perhaps, if guided.

enhancement

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.

All 10 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haythamabutair picture haythamabutair  路  3Comments

MaximRouiller picture MaximRouiller  路  3Comments

ranjan-2209 picture ranjan-2209  路  3Comments

ErcinDedeoglu picture ErcinDedeoglu  路  3Comments

FaMouZx3 picture FaMouZx3  路  3Comments