Files
GX.O3.WMS/APP.O3/Domain/Models/TraceId/TraceUtility.cs
T

31 lines
1.1 KiB
C#
Executable File

using Opsoft.NetCore.O3App.Shared;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace Opsoft.NetCore.O3App.Domain
{
public static class TraceUtility
{
public static string GenerateAgentId()
{
var host = IpHelper.GetIPv4().NullOrEmpty2Default("127.0.0.1");
if (!IPAddress.TryParse(host, out var address))
{
address = Dns.GetHostEntry(host).AddressList.First();
}
var hashValue = new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(Shared.Constants.AppNames.Self));
var builder = new StringBuilder();
for (int index = 0; index < hashValue.Length; index++)
{
builder.Append(hashValue[index].ToString("x").PadLeft(1, '0'));
}
var appName = builder.ToString();
var start = appName.Length > 6 ? appName.Length - 6 : 0;
var length = appName.Length < 6 ? appName.Length : 6;
return $"{address}_{appName.Substring(start, length)}";
}
}
}