I want to log my server name in ASP.NET Application, I use multi-servers and load balancing so I need to log the server name.
But what is the difference between these ways to get the server name?
and which one is true or better to log?
any idea?
or any other ways?
System.Environment.MachineName
Server.MachineName
System.Net.Dns.GetHostName()
There is also another ways but not always return correct server name:
Request.ServerVariables["SERVER_NAME"]
System.Net.Dns.GetHostEntry(Request.ServerVariables("SERVER_NAME")).HostName
System.Net.Dns.GetHostEntry(Request.ServerVariables("LOCAL_ADDR")).HostName
I know this is an old question. I was searching for answer to the same issue; here is my solution.
string url = HttpContext.Current.Request.Url.ToString();
string[] tempArray = url.Split('/');
string serverName = tempArray[0] + "/" + tempArray[1] + "/" + tempArray[2] ;
This way you will get the exact server name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With